Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • Entry
  • Hooks
  • MetasHandler
  • Model
  • Module
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace Icybee\Modules\Registry;
 13 
 14 use ICanBoogie\ActiveRecord;
 15 use ICanBoogie\Exception;
 16 
 17 class Hooks
 18 {
 19     /*
 20      * Events
 21      */
 22 
 23     /**
 24      * This callback alters the edit block of the "nodes", "users" and "sites" modules, adding
 25      * support for metadatas by loading the metadatas associated with the edited object and
 26      * merging them with the current properties.
 27      *
 28      * @param Event $event
 29      *
 30      * @throws Exception
 31      */
 32     static public function on_editblock_alter_values(\Icybee\EditBlock\AlterValuesEvent $event, \Icybee\EditBlock $target)
 33     {
 34         if (!$event->key)
 35         {
 36             return;
 37         }
 38 
 39         $module = $event->module;
 40 
 41         if ($module instanceof \Icybee\Modules\Nodes\Module)
 42         {
 43             $type = 'node';
 44         }
 45         else if ($module instanceof \Icybee\Modules\Users\Module)
 46         {
 47             $type = 'user';
 48         }
 49         else if ($module instanceof \Icybee\Modules\Sites\Module)
 50         {
 51             $type = 'site';
 52         }
 53         else
 54         {
 55             throw new Exception('Metadatas are not supported for instances of the given class: %class', [ '%class' => get_class($target) ]);
 56         }
 57 
 58         $metas = ActiveRecord\get_model('registry/' . $type)
 59         ->select('name, value')
 60         ->filter_by_targetid($event->key)
 61         ->pairs;
 62 
 63         $values = &$event->values;
 64 
 65         if (isset($values['metas']))
 66         {
 67             if ($values['metas'] instanceof MetasHandler)
 68             {
 69                 $values['metas'] = $values['metas']->to_aray();
 70             }
 71 
 72             $values['metas'] += $metas;
 73         }
 74         else
 75         {
 76             $values['metas'] = $metas;
 77         }
 78     }
 79 
 80     /**
 81      * This callback saves the metadatas associated with the object targeted by the operation.
 82      *
 83      * @param Event $event
 84      *
 85      * @throws Exception
 86      */
 87     static public function on_operation_save(\ICanBoogie\Operation\ProcessEvent $event, \ICanBoogie\SaveOperation $sender)
 88     {
 89         $params = $event->request->params;
 90 
 91         if (!array_key_exists('metas', $params))
 92         {
 93             return;
 94         }
 95 
 96         $targetid = $event->rc['key'];
 97 
 98         if ($sender instanceof \Icybee\Modules\Nodes\SaveOperation)
 99         {
100             $type = 'node';
101         }
102         else if ($sender instanceof \Icybee\Modules\Users\SaveOperation)
103         {
104             $type = 'user';
105         }
106         else if ($sender instanceof \Icybee\Modules\Sites\SaveOperation)
107         {
108             $type = 'site';
109         }
110         else
111         {
112             throw new Exception('Metadatas are not supported for instances of the given class: %class', [ '%class' => get_class($sender) ]);
113         }
114 
115         $model = ActiveRecord\get_model('registry/' . $type);
116         $driver_name = $model->connection->driver_name;
117         $delete_statement = '';
118         $update_groups = [];
119         $delete_args = [];
120 
121         foreach ($params['metas'] as $name => $value)
122         {
123             if (is_array($value))
124             {
125                 $value = serialize($value);
126             }
127             else if (!strlen($value))
128             {
129                 $value = null;
130 
131                 $delete_statement .= ', ?';
132                 $delete_args[] = $name;
133 
134                 continue;
135             }
136 
137             if ($driver_name == 'sqlite')
138             {
139                 $update_groups[] = [ $targetid, $name, $value ];
140             }
141             else
142             {
143                 $update_groups[] = [ $targetid, $name, $value, $value ];
144             }
145         }
146 
147         $model->connection->begin();
148 
149         if ($delete_statement)
150         {
151             array_unshift($delete_args, $targetid);
152 
153             $delete_statement = 'DELETE FROM {self} WHERE targetid = ? AND name IN (' . substr($delete_statement, 2) . ')';
154 
155             $model->execute($delete_statement, $delete_args);
156         }
157 
158         if ($update_groups)
159         {
160             if ($driver_name == 'sqlite')
161             {
162                 $update = $model->prepare('INSERT OR REPLACE INTO {self} (targetid, name, value) VALUES(?,?,?)');
163             }
164             else
165             {
166                 $update = $model->prepare('INSERT INTO {self} (targetid, name, value) VALUES(?,?,?) ON DUPLICATE KEY UPDATE value = ?');
167             }
168 
169             foreach ($update_groups as $values)
170             {
171                 $update->execute($values);
172             }
173         }
174 
175         $model->connection->commit();
176     }
177 
178     /**
179      * Deletes the metadatas associated with a record when it is deleted.
180      *
181      * @param \ICanBoogie\Operation\ProcessEvent $event
182      * @param \ICanBoogie\DeleteOperation $operation
183      *
184      * @throws Exception
185      */
186     static public function on_operation_delete(\ICanBoogie\Operation\ProcessEvent $event, \ICanBoogie\DeleteOperation $operation)
187     {
188         $module = $operation->module;
189 
190         if ($module instanceof \Icybee\Modules\Nodes\Module)
191         {
192             $type = 'node';
193         }
194         else if ($module instanceof \Icybee\Modules\Users\Module)
195         {
196             $type = 'user';
197         }
198         else if ($module instanceof \Icybee\Modules\Sites\Module)
199         {
200             $type = 'site';
201         }
202         else
203         {
204             throw new Exception('Metadatas are not supported for instances of the given class: %class', [ '%class' => get_class($module) ]);
205         }
206 
207         ActiveRecord\get_model('registry/' . $type)
208         ->filter_by_targetid($operation->key)
209         ->delete();
210     }
211 
212     /*
213      * Prototypes
214      */
215 
216     /**
217      * This is the callback for the `metas` virtual property added to the "nodes", "users" and
218      * "sites" active records.
219      *
220      * @param \Icybee\Modules\Nodes\Node|\Icybee\Modules\Users\User|\Icybee\Modules\Sites\Site $target
221      *
222      * @return MetasHandler A {@link MetasHandler} instance.
223      */
224     static public function get_metas(\ICanBoogie\ActiveRecord $target)
225     {
226         return new MetasHandler($target);
227     }
228 
229     /**
230      * This si the callback for the `registry` virtual property added to the core object.
231      *
232      * @param \ICanBoogie\Core $target The core object.
233      *
234      * @return Module The "registry" model.
235      */
236 
237     static public function get_registry(\ICanBoogie\Core $target)
238     {
239         return $target->models['registry'];
240     }
241 }
Autodoc API documentation generated by ApiGen 2.8.0