Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'develop'

  • Loading branch information...
commit b743888f8f05730f8798984b91e4f353d5ff2eda 2 parents 7b8289b + 323bc77
@lorenzo lorenzo authored
View
4 controllers/favorites_controller.php
@@ -85,9 +85,9 @@ public function add($type = null, $foreignKey = null) {
$message = __d('favorites', 'Record was successfully added', true);
} else {
$message = __d('favorites', 'Record was not added.', true);
- }
+ }
} catch (Exception $e) {
- $message = __d('favorites', 'Record was not added. ', true) . $e->getMessage();
+ $message = __d('favorites', 'Record was not added.', true) . ' ' . $e->getMessage();
}
}
}
View
6 locale/favorites.pot
@@ -37,14 +37,10 @@ msgstr ""
msgid "Record was successfully added"
msgstr ""
-#: /controllers/favorites_controller.php:87
+#: /controllers/favorites_controller.php:87;90
msgid "Record was not added."
msgstr ""
-#: /controllers/favorites_controller.php:90
-msgid "Record was not added. "
-msgstr ""
-
#: /controllers/favorites_controller.php:114
msgid "Record removed from list"
msgstr ""
View
92 locale/por/LC_MESSAGES/favorites.po
@@ -0,0 +1,92 @@
+# LANGUAGE translation of the CakePHP Categories plugin
+#
+# Copyright 2010, Cake Development Corporation (http://cakedc.com)
+#
+# Licensed under The MIT License
+# Redistributions of files must retain the above copyright notice.
+#
+# @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
+# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: CakePHP Favorites Plugin\n"
+"POT-Creation-Date: 2010-09-15 15:11+0200\n"
+"PO-Revision-Date: 2010-09-23 16:32-0300\n"
+"Last-Translator: Renan Gonçalves <renan.saddam@gmail.com>\n"
+"Language-Team: CakeDC <renan.saddam@gmail.com>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=2;plural=n>1;\n"
+"X-Poedit-Language: Portuguese\n"
+"X-Poedit-Country: BRAZIL\n"
+
+#: /controllers/favorites_controller.php:59
+msgid "Authentification required"
+msgstr "Autentificação exigida"
+
+#: /controllers/favorites_controller.php:72;132;150
+msgid "Invalid object type."
+msgstr "Tipo de objeto inválido."
+
+#: /controllers/favorites_controller.php:79
+msgid "Invalid identifier"
+msgstr "Identificador inválido"
+
+#: /controllers/favorites_controller.php:85
+msgid "Record was successfully added"
+msgstr "Registro foi adicionado com sucesso"
+
+#: /controllers/favorites_controller.php:87;90
+msgid "Record was not added."
+msgstr "Registro não foi adicionado."
+
+#: /controllers/favorites_controller.php:114
+msgid "Record removed from list"
+msgstr "Registro removido da lista"
+
+#: /controllers/favorites_controller.php:116
+msgid "Unable to delete favorite, please try again"
+msgstr "Não foi possível remover o favorito, por favor tente novamente"
+
+#: /controllers/favorites_controller.php:172
+msgid "Invalid direction"
+msgstr "Direção inválida"
+
+#: /controllers/favorites_controller.php:175
+msgid "Favorite positions updated."
+msgstr "Posições do favorito atualizadas."
+
+#: /controllers/favorites_controller.php:177
+msgid "Unable to change favorite position, please try again"
+msgstr "Não foi possível alterar a posição do favorito, por favor tente novamente"
+
+#: /controllers/favorites_controller.php:226
+msgid "That record does not exist."
+msgstr "Esse registro não existe."
+
+#: /controllers/favorites_controller.php:229
+msgid "That record does not belong to you."
+msgstr "Esse registro não pertence a você."
+
+#: /models/behaviors/favorite.php:130
+msgid "Operation is not allowed"
+msgstr "Operação não permitida"
+
+#: /models/behaviors/favorite.php:141
+msgid "Already added."
+msgstr "Já adicionado."
+
+#: /models/behaviors/favorite.php:151
+msgid "You cannot add more than %s items to this list"
+msgstr "Você não pode adicionar mais que %s itens nesta lista"
+
+#: /views/favorites/list.ctp:17
+msgid "You have no favorites in this category."
+msgstr "Você não possui favoritos nesta categoria."
+
+#: /views/helpers/favorites.php:76
+msgid "Incorrect favorite type \"%s\"."
+msgstr "Tipo incorreto de favorito \"%s\"."
+
View
88 locale/spa/LC_MESSAGES/favorites.po
@@ -0,0 +1,88 @@
+# LANGUAGE translation of the CakePHP Categories plugin
+#
+# Copyright 2010, Cake Development Corporation (http://cakedc.com)
+#
+# Licensed under The MIT License
+# Redistributions of files must retain the above copyright notice.
+#
+# @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
+# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+#
+"Project-Id-Version: Favorites CakePHP plugin\n"
+"POT-Creation-Date: 2010-09-15 15:11+0200\n"
+"PO-Revision-Date: 2010-09-24 13:18-0400\n"
+"Last-Translator: José Lorenzo Rodríguez <jose.lorenzo@cakedc.com>\n"
+"Language-Team: CakeDC <contact@cakedc.com>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"X-Poedit-Language: Spanish\n"
+
+#: /controllers/favorites_controller.php:59
+msgid "Authentification required"
+msgstr "Autenticación Requerida"
+
+#: /controllers/favorites_controller.php:72;132;150
+msgid "Invalid object type."
+msgstr "Tipo de objecto no válido"
+
+#: /controllers/favorites_controller.php:79
+msgid "Invalid identifier"
+msgstr "Identificador no válido"
+
+#: /controllers/favorites_controller.php:85
+msgid "Record was successfully added"
+msgstr "El registro fue agregado exitosamente"
+
+#: /controllers/favorites_controller.php:87;90
+msgid "Record was not added."
+msgstr "El registro no fue agregado"
+
+#: /controllers/favorites_controller.php:114
+msgid "Record removed from list"
+msgstr "Registro eliminado de la lista"
+
+#: /controllers/favorites_controller.php:116
+msgid "Unable to delete favorite, please try again"
+msgstr "No es posible eliminar el favorito, por favor intente nuevamente"
+
+#: /controllers/favorites_controller.php:172
+msgid "Invalid direction"
+msgstr "Dirección no válida"
+
+#: /controllers/favorites_controller.php:175
+msgid "Favorite positions updated."
+msgstr "Posiciones de favoritos actualizadas."
+
+#: /controllers/favorites_controller.php:177
+msgid "Unable to change favorite position, please try again"
+msgstr "No es posible cambiar la posición del favorito, por favor intente de nuevo"
+
+#: /controllers/favorites_controller.php:226
+msgid "That record does not exist."
+msgstr "No existe tal registro."
+
+#: /controllers/favorites_controller.php:229
+msgid "That record does not belong to you."
+msgstr "El registro no te pertenece."
+
+#: /models/behaviors/favorite.php:130
+msgid "Operation is not allowed"
+msgstr "Operación no permitida"
+
+#: /models/behaviors/favorite.php:141
+msgid "Already added."
+msgstr "Ya fue añadido"
+
+#: /models/behaviors/favorite.php:151
+msgid "You cannot add more than %s items to this list"
+msgstr "No se pueden añadir más de %s elementos a esta lista"
+
+#: /views/favorites/list.ctp:17
+msgid "You have no favorites in this category."
+msgstr "No tiene favoritos en esta categoría."
+
+#: /views/helpers/favorites.php:76
+msgid "Incorrect favorite type \"%s\"."
+msgstr "Favorito incorrecto de tipo \"%s\"."
+
View
23 models/behaviors/favorite.php
@@ -1,25 +1,12 @@
<?php
/**
- * Favorite Behavior
+ * Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
*
- * Attach to a Model to create hasMany relationship with Favorite model and
- * include counterCache, also provides some utility methods for favorites, like reordering,
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
*
- * PHP versions 4 and 5
- *
- * Copyright 2007-2008, Cake Development Corporation
- * 1785 E. Sahara Avenue, Suite 490-423
- * Las Vegas, Nevada 89104
- *
- * You may obtain a copy of the License at:
- * License page: http://projects.cakedc.com/licenses/TBD TBD
- * Copyright page: http://converge.cakedc.com/copyright/
- *
- * @filesource
- * @copyright Copyright 2007-2008, Cake Development Corporation
- * @link
- * @package favorites
- * @subpackage favorites.models.behaviors
+ * @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
View
32 models/favorite.php
@@ -111,6 +111,29 @@ public function getFavorites($userId, $options) {
return $favorites;
}
+/**
+ * Returns all the favorites a given User has added
+ *
+ * @param string $id User id
+ * @param array $options query options to be passed to the find method
+ * @return array Favorite list with the favorites keys
+ * Each key will have a value like the following:
+ * array(
+ * 'favorite-id1' => 'foreign-key1',
+ * 'favorite-id2' => 'foreign-key2')
+ * @access public
+ */
+ public function getAllFavorites($id = null, $options = array()) {
+ $keys = array_keys(Configure::read('Favorites.types'));
+ $result = array_fill_keys($keys, array());
+ if (!is_null($id)) {
+ $list = $this->getFavorites($id, array('type' => $keys) + $options);
+ $list = Set::combine($list, '{n}.Favorite.id', '{n}.Favorite.foreign_key', '{n}.Favorite.type');
+ $result = array_merge($result, $list);
+ }
+ return $result;
+ }
+
/**
* Returns the search data
@@ -294,7 +317,7 @@ public function isFavorited($modelName, $type, $foreignKey, $userId) {
*/
public function getFavoriteId($modelName, $type, $foreignKey, $userId) {
$favoriteId = false;
-
+
$record = $this->find('first', array(
'fields' => array($this->alias . '.' . $this->primaryKey),
'conditions' => array(
@@ -308,7 +331,7 @@ public function getFavoriteId($modelName, $type, $foreignKey, $userId) {
if (isset($record[$this->alias][$this->primaryKey])) {
$favoriteId = $record[$this->alias][$this->primaryKey];
}
-
+
return $favoriteId;
}
@@ -361,9 +384,9 @@ public function deleteRecord($id) {
}
$record = $record[$this->alias];
$model = $record['model'];
+ $Model = ClassRegistry::init($model);
$result = $this->delete($id);
if ($result) {
- $Model = ClassRegistry::init($model);
if (method_exists($Model, 'afterDeleteFavorite')) {
$result = $Model->afterDeleteFavorite(array('id' => $record['foreign_key'], 'userId' => $record['user_id'], 'model' => $model, 'type' => $record['type']));
}
@@ -371,5 +394,6 @@ public function deleteRecord($id) {
}
return $result;
}
-
+
}
+
View
109 readme.md
@@ -5,43 +5,51 @@ Favorites plugin allows to associate users to any record in your database throug
## Installation ##
1. Place the favorites folder into any of your plugin directories for your app (for example app/plugins or cake/plugins)
-3. Create database tables using either the schema shell or the migrations plugin:
+2. Create the required database tables using either the schema shell or the migrations plugin:
cake schema create -plugin favorites -name favorites
cake migration run all -plugin favorites
-4. Attach the Favorite behavior to your models via the $actsAs variable or dynamically using the BehaviorsCollection object methods:
- public $actsAs = array('Favorites.Favorite')
- or
- $this->Behaviors->attach('Favorites.Favorite')
-5. This plugin requires to have setup some parameters in global Configure storage:
- Favorites.types contains supported objects that allowed to be stored as favorites.
- Favorites.modelCategories allow to list all models and required contains for it.
- Favorites.defaultTexts sets the default text for the helper toggleFavorite method
-
- Configure::write('Favorites.types', array('post' => 'Blogs.Post', 'link' => 'Link'));
- Configure::write('Favorites.modelCategories', array('Post', 'Link'));
-
- Or you could use the Configure::load() method for a file with the following example content:
-
- $config['Favorites'] = array(
- 'types' => array(
- 'favorite' => 'Post',
- 'watch' => 'Post'),
- 'defaultTexts' => array(
- 'favorite' => __('Favorite it', true),
- 'watch' => __('Watch it', true)),
- 'modelCategories' => array(
- 'Post'));
+3. This plugin requires that you setup some parameters in global Configure storage:
+ 1. `Favorites.types contains supported objects that allowed to be stored as favorites.
+ 2. `Favorites.modelCategories allow to list all models and required contains for it.
+ 3. `Favorites.defaultTexts sets the default text for the helper toggleFavorite method
+
+Example:
+
+ Configure::write('Favorites.types', array('post' => 'Blogs.Post', 'link' => 'Link'));
+ Configure::write('Favorites.defaultTexts', array('favorite' => __('Favorite it', true), 'watch' => __('Watch it', true)));
+ Configure::write('Favorites.modelCategories', array('Post', 'Link'));
+
+Or you could use the Configure::load() method to load a configuration file that has content similar to that below:
+
+ $config['Favorites'] = array(
+ 'types' => array(
+ 'favorite' => 'Post',
+ 'watch' => 'Post'),
+ 'defaultTexts' => array(
+ 'favorite' => __('Favorite it', true),
+ 'watch' => __('Watch it', true)),
+ 'modelCategories' => array(
+ 'Post'));
## Usage ##
-1. Add the Favorites helper to you controller
- public $helpers = array('Favorites.Favorites');
-2. Use the helper in your views to generate links mark a model record as favorite
- <?php echo $this->Favorites->toggleFavorite('favorite-type', $modelId);
+Add the Favorites helper to your controller:
+
+ public $helpers = array('Favorites.Favorites');
+
+Attach the Favorite behavior to your models via the `$actsAs` variable or dynamically using the `BehaviorsCollection` object methods:
+
+ public $actsAs = array('Favorites.Favorite');
+ // Or
+ $this->Behaviors->attach('Favorites.Favorite');
+
+Use the favourites helper in your views to generate links to mark a model record as favorite:
+
+ <?php echo $this->Favorites->toggleFavorite('favorite-type', $modelId);
-This link will toggle the "favorite-type" tag for this user and model record
+This link will toggle the "favorite-type" tag for this user and model record.
-If you want the helper to distinguish whether it needs to activate or deactivate the favorite flag in for the user,you need to pass to the view the variable "userFavorites" containing an associative array of user favorites per favorite type. This structure is needed
+If you want the helper to distinguish whether it needs to activate or deactivate the favorite flag in for the user, you need to pass to the view the variable `userFavorites` containing an associative array of user favorites per favorite type. The following structure is needed:
array(
'favorite-type1' => array(
@@ -56,38 +64,33 @@ If you want the helper to distinguish whether it needs to activate or deactivate
)
);
-You can achieve this result with a method like this one
+You can achieve this result using with method `getAllFavorites` in `Favorite` model:
- public function getFavorites($user = null) {
- $keys = array('favorite', 'want-it', 'own-it');
- $result = array_fill_keys($keys, array());
+ $Favorite = ClassRegistry::init('Favorites.favorite');
+ $this->set('userFavorites', $Favorite->getAllFavorites('user-id'));
- if (!is_null($user)) {
- $Favorite = ClassRegistry::init('Favorites.Favorite');
- $list = $Favorite->getFavorites($user, array('type' => $keys));
- $list = Set::combine($list, '{n}.Favorite.id', '{n}.Favorite.foreign_key', '{n}.Favorite.type');
-
- $result = array_merge($result, $list);
- }
-
- return $result;
- }
+## Configuration Options ##
+The Favorite behavior has some configuration options to adapt to your apps needs.
-## Configuration Options ##
-The Favorite behavior has some configuration option to adapt to your app needs. The configuration array accepts the following keys
+The configuration array accepts the following keys:
-1. favoriteAlias: The name of the association to be created with the model the Behavior is attached to and the favoriteClass model. Default: Favorite
-2. favoriteClass: If you need to extend the Favorite model or override it with your own implementation set this key to the model you want to use
-3. foreignKey: the field in your table that serves as reference for the primary key of the model it is attached to. (Used for own implementations of Favorite model)
-4. counter_cache: the name of the field that will hold the number of times the model record has been favorited
+* `favoriteAlias` - The name of the association to be created with the model the Behavior is attached to and the favoriteClass model. Default: Favorite
+* `favoriteClass` - If you need to extend the Favorite model or override it with your own implementation set this key to the model you want to use
+* `foreignKey` - the field in your table that serves as reference for the primary key of the model it is attached to. (Used for own implementations of Favorite model)
+* `counter_cache` - the name of the field that will hold the number of times the model record has been favorited
## Callbacks ##
Additionally the behavior provides two callbacks to implement in your model:
-1. beforeSaveFavorite - called before save favorite. Should return boolean value.
-2. afterSaveFavorite - called after save favorite.
+* `beforeSaveFavorite` - called before save favorite. Should return boolean value.
+* `afterSaveFavorite` - called after save favorite.
+
+## Requirements ##
+
+* PHP version: PHP 5.2+
+* CakePHP version: 1.3 Stable
## Requirements ##
@@ -113,4 +116,4 @@ Copyright 2009-2010<br/>
[Cake Development Corporation](http://cakedc.com)<br/>
1785 E. Sahara Avenue, Suite 490-423<br/>
Las Vegas, Nevada 89104<br/>
-http://cakedc.com<br/>
+http://cakedc.com<br/>
View
31 tests/cases/models/favorite.test.php
@@ -177,6 +177,24 @@ public function testGetFavorites() {
}
/**
+ * test get all favorites on Favorite model
+ *
+ * @return void
+ */
+ public function testGetAllFavorites() {
+ Configure::write('Favorites.types', array('type' => 'Type', 'anothertype' => 'AnotherType'));
+ $this->Article->saveFavorite(1, 'FavoriteArticle', 'type', 1);
+ $this->Article->saveFavorite(1, 'FavoriteArticle', 'type', 2);
+ $this->Article->saveFavorite(1, 'FavoriteArticle', 'anothertype', 3);
+ $result = $this->FavoriteModel->getAllFavorites(1);
+ $this->assertEqual(count($result), 2);
+ $this->assertEqual(count($result['type']), 2);
+ $this->assertEqual(count($result['anothertype']), 1);
+ $this->assertEqual(array(1, 2), array_values($result['type']));
+ $this->assertEqual(array(3), array_values($result['anothertype']));
+ }
+
+/**
* test get favorites with extra types (models and associations)
*
* @return void
@@ -244,7 +262,7 @@ public function testTypeCounts() {
$this->assertEqual($results['FavoriteArticle'], 2);
$this->assertEqual($results['FavoriteOther'], 2);
-
+
$results = $this->FavoriteModel->typeCounts(1, array('types' => array('OtherModel', 'FavoriteArticle', 'FavoriteOther')));
$this->assertEqual($results['FavoriteArticle'], 2);
@@ -265,7 +283,7 @@ public function testIsFavorited() {
$result = $this->FavoriteModel->isFavorited('FavoriteArticle', 'default', 1, 2);
$this->assertFalse($result, 'User with no favorites, is being shown as having one. %s');
}
-
+
/**
* Test getFavoriteId method
*
@@ -275,11 +293,11 @@ public function testGetFavoriteId() {
$this->assertTrue($this->Article->saveFavorite(1, 'FavoriteArticle', 'default', 1));
$result = $this->FavoriteModel->getFavoriteId('FavoriteArticle', 'default', 1, 1);
$this->assertFalse(empty($result));
-
+
$result = $this->FavoriteModel->getFavoriteId('FavoriteArticle', 'default', 1, 2);
$this->assertFalse($result, 'User with no favorites, is being shown as having one. %s');
}
-
+
/**
* testGetFavoriteLists
*
@@ -289,12 +307,13 @@ public function testGetFavoriteLists() {
$this->Article->saveFavorite(1, 'FavoriteArticle', 'default', 1);
$this->Article->saveFavorite(1, 'FavoriteArticle', 'default', 2);
$this->Article->saveFavorite(1, 'FavoriteArticle', 'default', 3);
- $result = $this->FavoriteModel->getFavoriteLists('default', 1);
+ $result = $this->FavoriteModel->getFavoriteLists('default', 1);
$expected = array(
'FavoriteArticle' => array(
array('id' => '1', 'title' => 'First Article'),
array('id' => '2', 'title' => 'Second Article'),
- array('id' => '3', 'title' => 'Third Article')));
+ array('id' => '3', 'title' => 'Third Article')));
$this->assertEqual($result, $expected);
}
}
+
View
1  views/helpers/favorites.php
@@ -71,7 +71,6 @@ public function toggleFavorite($type, $id, $addText = null, $removeText = null,
$_defaultTexts = Configure::read('Favorites.defaultTexts');
$link = '';
$type = strtolower($type);
-
if (!array_key_exists($type, $_defaultTexts)) {
trigger_error(sprintf(__d('favorites', 'Incorrect favorite type "%s".', true), $type), E_USER_WARNING);
} else {
Please sign in to comment.
Something went wrong with that request. Please try again.