Git Product home page Git Product logo

favorites's Introduction

Favorites Plugin for CakePHP

Version 1.1 for cake 2.x

Favorites plugin allows to associate users to any record in your database through human readable tags or categories.

Installation

  1. Place the favorites folder into any of your plugin directories for your app (for example app/Plugin or root_dir/plugins)

  2. Create the required database tables using either the schema shell or the migrations plugin:

     cake schema create --plugin Favorites --name favorites
     cake Migrations.migration run all --plugin Favorites
    
  3. This plugin requires that you setup some parameters in global Configure storage:

  4. `Favorites.types contains supported objects that allowed to be stored as favorites.

  5. `Favorites.modelCategories allow to list all models and required contains for it.

  6. `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'), 'watch' => __('Watch it')));
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'),
		'watch' => __('Watch it')
	),
	'modelCategories' => array(
		'Post'
	)
);

Usage

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.

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(
		'favorite-id1' => 'model-foreignKey-1',
		'favorite-id2' => 'model-foreignKey-3'
		'favorite-id3' => 'model-foreignKey-2'
	),
	'favorite-type2' => array(
		'favorite-id4' => 'model-foreignKey-1',
		'favorite-id5' => 'model-foreignKey-3'
		'favorite-id6' => 'model-foreignKey-2'
	)
);

You can achieve this result using with method getAllFavorites in Favorite model:

$Favorite = ClassRegistry::init('Favorites.favorite');
$this->set('userFavorites', $Favorite->getAllFavorites('user-id'));

Configuration Options

The Favorite behavior has some configuration options to adapt to your apps needs.

The configuration array accepts the following keys:

  • 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:

  • beforeSaveFavorite - called before save favorite. Should return boolean value.
  • afterSaveFavorite - called after save favorite.

Requirements

  • PHP version: PHP 5.2+
  • CakePHP version: 2.x Stable

Requirements

  • PHP version: PHP 5.2+
  • CakePHP version: Cakephp 2.x Stable

Support

For support and feature request, please visit the Favorites Plugin Support Site.

For more information about our Professional CakePHP Services please visit the Cake Development Corporation website.

Branch strategy

The master branch holds the STABLE latest version of the plugin. Develop branch is UNSTABLE and used to test new features before releasing them.

Previous maintenance versions are named after the CakePHP compatible version, for example, branch 1.3 is the maintenance version compatible with CakePHP 1.3. All versions are updated with security patches.

Contributing to this Plugin

Please feel free to contribute to the plugin with new issues, requests, unit tests and code fixes or new features. If you want to contribute some code, create a feature branch from develop, and send us your pull request. Unit tests for new features and issues detected are mandatory to keep quality high.

License

Copyright 2009-2012, Cake Development Corporation

Licensed under The MIT License
Redistributions of files must retain the above copyright notice.

Copyright

Copyright 2009-2012
Cake Development Corporation
1785 E. Sahara Avenue, Suite 490-423
Las Vegas, Nevada 89104
http://cakedc.com

favorites's People

Contributors

ajibarra avatar dogmatic69 avatar josegonzalez avatar lorenzo avatar predominant avatar real34 avatar renan avatar shama avatar skie avatar steinkel avatar stephane-benoist avatar yeliparra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

favorites's Issues

model controllers missing

Reported by Ryan Snowden | November 18th, 2010 @ 01:03 AM

Are the appmodel/controllers optional or missed from the plugin? It causes a problem when generating permissions and well... they're on the other cakedc plugins ;)

Improve documentation of setup and installation of Favourites plugin

Reported by sibljon | October 13th, 2010 @ 05:27 PM

Hello, I'm not sure if I've reached the right outlet for support, but I will explain my problem. And thank you for any help you may provide!

I'm pretty new to CakePHP, and I'm trying to get the Favorites plugin working on my site. My problem currently is that I can't "toggleFavorite" in my app/views/photos/view.ctp. My "view" helper displays a photo and allows the user to "favorite it" with:

echo $this->Favorites->toggleFavorite('favorite', $photo['Photo']['id']);

The link appears, but I get the following warning in view.ctp right before the link is rendered by the Favorites plugin:

Warning (2): array_key_exists() expects parameter 2 to be array, null given [APP\plugins\favorites\views\helpers\favorites.php, line 88]

Then, when I click the link, it goes to favorites/favorites/add/favorite/94 (94 is the "id" of the Photo):

Missing Controller
Error: FavoritesController could not be found.
Error: Create the class FavoritesController below in file: app\controllers\favorites_controller.php

For some more information on my set-up, I have placed the following in app/config/core.php:

Configure::write('Favorites.types', array('favorite' => 'Photo', 'follow' => 'User'));
Configure::write('Favorites.defaultTexts', array('favorite' => __('Favorite it', true),'watch' => __('Follow it', true)));
Configure::write('Favorites.modelCategories', array('Photo', 'User'));

Other than that, I have copied the files I downloaded from your Github page into my app/plugins/favorites/

Thanks in advance for any help you can provide!

Best,
Jonathan Sibley

Suggestion Needed

Reported by Zuha | March 22nd, 2011 @ 10:14 AM

Thank you for the plugin. When you want to create a link back to the item which was favorited, what is the best practice for getting the plugin of that model that was favorited? For example, I favorite a CatalogItem which is in the Catalogs plugin. Something like this, is where I get stuck...

// added to /favorites/views/list.ctp
$controller = Inflector::tableize( $fav['Favorite']['model']);
$this->Html->link($fav[$key][$name], array(
     'plugin' => [UNKNOWN],
     'controller' => $controller,
     'action' => 'view',
     $fav['Favorite']['foreign_key']));

This is a question related to all modular plugins like this (ie. plugins that use the model/foreign_key fields), and would help in a lot of places for many of the cakedc plugins.

Upgrade for 3.x

Hi all. I know a plugin such as this is not a high priority development, but is there any chance you're going to release a version for 3.x? I want what this plugin does in my project, but don't want to reinvent the wheel (and don't stand much of a chance of being able to do it myself).

If not, no worries, but I thought I'd open an issue nonetheless.

Help with toggle favorite link

In TracksController I have this:

$Favorite = ClassRegistry::init('Favorites.favorite');
$userFavorites = $Favorite->getAllFavorites( $this->Auth->user('id') );
$this->set(compact('tracks', 'userFavorites'));

In View/Tracks/index.php I have this:

<?php echo $this->Favorites->toggleFavorite('favorite', $track['Track']['id']); ?>

It's adding tracks nicely, but when it's already saved the href of it never toggle to 'delete' link.

Am I using the toggleFavorite method wrong?

Missing '$viewFile' in Strict mode.

FYI.

Declaration of FavoritesHelper::beforeRender() should be compatible with Helper::beforeRender($viewFile) [APP/Plugin/Favorites/View/Helper/FavoritesHelper.php, line 107]

Problem using the favorites

Hi,

when using the code into the view this does not work as expected.

Can you explain how the input data needs to look like?

Favorites->toggleFavorite('favorite-type', $modelId); ?>

favorite-type you need to change to desired favorite type, and $modelId you need to provide the id number of the record.

This give me the following error:

Warning (2): array_key_exists() expects parameter 2 to be array, null given [APP/Plugin/Favorites/View/Helper/FavoritesHelper.php, line 86]

List all favorites of a category

Hi everybody,
I try to get the list of favorites of a category but have always the message "You have no favorites in this category. " however my favorites entries are well stored in the database.
I'm using cakePHP 2.4 and i try to access the list by this URL : www.myapp.com/favorites/favorites/list_all/tender
"tender" is the favorite type i configure in "bootstrap.php"

Is this a known problem or i missed some configuration ?

Cordially,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.