Skip to content

Commit

Permalink
Now the pictures will be retrieved automatically for objects with gal…
Browse files Browse the repository at this point in the history
…leries
  • Loading branch information
hugodias committed Jan 29, 2015
1 parent de1c8c9 commit c7eed95
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Model/Album.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
App::uses('Folder', 'Utility');
App::uses('GalleryAppModel', 'Gallery.Model');

class Album extends GalleryAppModel
{
Expand Down Expand Up @@ -183,4 +184,4 @@ private function createFolder($folder_name)
}
}

?>
?>
32 changes: 24 additions & 8 deletions Model/Behavior/GalleryBehavior.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
<?php
App::uses('ModelBehavior', 'Model');
App::uses('Gallery.Album', 'Model');
App::uses('Gallery.Picture', 'Model');
App::uses('Album', 'Gallery.Model');
App::uses('Picture', 'Gallery.Model');

class GalleryBehavior extends ModelBehavior
{
public function afterFind(Model $Model, $results, $primary = false)
{
foreach ($results as $key => $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(
Expand All @@ -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
)
)
);
}

}
51 changes: 39 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
Expand All @@ -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
));
```
Expand All @@ -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
));
Expand Down Expand Up @@ -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.

0 comments on commit c7eed95

Please sign in to comment.