From c7eed956bc249caaa44220e8bdfeee693cef6aaa Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 29 Jan 2015 14:45:16 -0200 Subject: [PATCH] Now the pictures will be retrieved automatically for objects with galleries --- Model/Album.php | 3 +- Model/Behavior/GalleryBehavior.php | 32 ++++++++++++++----- README.md | 51 +++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/Model/Album.php b/Model/Album.php index 3d7893f..2b4eaf6 100755 --- a/Model/Album.php +++ b/Model/Album.php @@ -1,5 +1,6 @@ \ No newline at end of file +?> diff --git a/Model/Behavior/GalleryBehavior.php b/Model/Behavior/GalleryBehavior.php index b1a07c3..8d9663a 100755 --- a/Model/Behavior/GalleryBehavior.php +++ b/Model/Behavior/GalleryBehavior.php @@ -1,18 +1,29 @@ $val) { + + if($this->settings[$Model->alias]['automatic']) { + $gallery = $this->getGallery($Model, $val[$Model->name]['id']); + + $results[$key]['Gallery'] = $gallery; + $results[$key]['Gallery']['numPictures'] = count($gallery['Picture']); + } + } + return $results; + } public function setup(Model $Model, $settings = array()) { if (!isset($this->settings[$Model->alias])) { $this->settings[$Model->alias] = array( - 'option1_key' => 'option1_default_value', - 'option2_key' => 'option2_default_value', - 'option3_key' => 'option3_default_value', + 'automatic' => true ); } $this->settings[$Model->alias] = array_merge( @@ -27,18 +38,23 @@ public function setup(Model $Model, $settings = array()) * @param Model $Model * @return mixed */ - public function getGallery(Model $Model) + public function getGallery(Model $Model, $object_id = null) { $Album = new Album(); + + + if(!$object_id) { + $object_id = $Model->id; + } + return $Album->find( 'first', array( 'conditions' => array( 'model' => $Model->alias, - 'model_id' => $Model->id + 'model_id' => $object_id ) ) ); } - } diff --git a/README.md b/README.md index ff73e63..070c13c 100755 --- a/README.md +++ b/README.md @@ -90,18 +90,46 @@ Integrating Gallery with a model of your application is very simple and takes on Class Product extends AppModel{ public $name = 'Product'; } -``` +``` Now you just need to add the $actsAs attribute in your model: ```php Class Product extends AppModel{ public $name = 'Product'; - public $actsAs = 'Gallery.Gallery'; + public $actsAs = array('Gallery.Gallery'); } ``` -And its done! To list all galleries attached to a Product, you can do something like this: +And its done! Now, when you search for this object in database, its pictures will be automatically retrieved from the plugin: ```php +$product = $this->Product->findById(10); + +// array( +// 'Product' => array( +// 'id' => '1', +// 'name' => 'My Product', +// 'price' => '29.00' +// ), +// 'Gallery' => array( +// 'Album' => array( +// ... +// ), +// 'Picture' => array( +// ... +// ), +// 'numPictures' => (int) 2 +// ) +// ) +``` +If you want to manually call for the pictures you will want to disable the automatic feature and call it yourself: +```php +// Product.php +public $actsAs = array('Gallery.Gallery' => array('automatic' => false)); +... + + +// Anycontroller.php + $this->Product->id = 10; $this->Product->getGallery(); ``` @@ -114,10 +142,10 @@ Every Picture that have Gallery attached at it already have 1 gallery to start u ```php echo $this->Html->link('New gallery', array( - 'controller' => 'gallery', - 'action' => 'upload', - 'plugin' => 'gallery', - 'model' => 'product', + 'controller' => 'albums', + 'action' => 'upload', + 'plugin' => 'gallery', + 'model' => 'product', 'model_id' => $product_id )); ``` @@ -131,9 +159,9 @@ You can create a gallery that don't belongs to any model, a standalone gallery. ```php echo $this->Html->link('New gallery', array( - 'controller' => 'gallery', - 'action' => 'upload', - 'plugin' => 'gallery', + 'controller' => 'albums', + 'action' => 'upload', + 'plugin' => 'gallery', 'model' => null, 'model_id' => null )); @@ -177,9 +205,8 @@ $config = array( ) ) ); - Configure::write('GalleryOptions', $config); + Configure::write('GalleryOptions', $config); ``` You can create more styles on styles array of modify the default size of the defaults PS: don't modify the default names as **medium** or **small**. This files are used by the plugin. -