Пример #1
0
 public function update($event, $extra = array())
 {
     // TODO: add some sort of handle->method handling system
     switch ($event->type()) {
         case 'get.type.update.form':
             return $this->meta('name');
             break;
         case 'get.type.update.config':
             ob_start();
             include $this->directory() . '/templates/entity.type.update.phtml';
             return ob_get_clean();
             break;
         case 'get.entity.update':
             ob_start();
             include $this->directory() . '/templates/entity.update.phtml';
             return ob_get_clean();
             break;
         case 'component.save':
             // TODO: Fix this, tight coupling is aweful.  going agile though.
             $model = new Blerby_Entity_Model_Component();
             // collect appropriate rows for config
             $componentTable = new Blerby_Entity_Model_Component();
             $componentTableMeta = $componentTable->info();
             $componentCols = array_keys($componentTableMeta['metadata']);
             // collect type config keys
             $toStore = array();
             foreach ($componentCols as $name) {
                 if ($this->{$name}) {
                     // databases dont like objects, store only strings.
                     $toStore[$name] = (string) $this->{$name};
                 }
             }
             $toStore['entity_id'] = (int) $this->parent()->id;
             // TODO: more rigorous checks
             // insert
             if ($this->get('id', false) === false) {
                 $componentTable->insert($toStore);
                 $this->id = $componentTable->getAdapter()->lastInsertId();
             } else {
                 $model->update($toStore, $model->getAdapter()->quoteInto("id=?", array($this->id)));
             }
             // TODO: Error checking
             foreach ($this->parent()->type->components->toArray() as $template) {
                 if ($template->part == $this->part) {
                     // get all of the configuration for this component that is not set
                     // in the type.
                     $componentConfig = array_diff($this->toArray(), $template->toArray());
                     // TODO: this needs to be reworked. at somepoint in the future, we may really need to map to another entity id
                     //       and this functionality kills it of if the config name is 'entity_id'
                     foreach (array('id', 'entity_id') as $name) {
                         if (isset($componentConfig[$name])) {
                             unset($componentConfig[$name]);
                         }
                     }
                     $componentConfigTable = new Blerby_Entity_Model_Component_Config();
                     // TODO with auto-incrementing fields this is aweful, add indexes to table
                     $where = $componentConfigTable->getAdapter()->quoteInto("entity_component_id = ?", array((int) $this->id));
                     $componentConfigTable->delete($where);
                     // store each piece of config
                     foreach ($componentConfig as $name => $value) {
                         // no need to store extra data
                         if (!isset($componentCols[$name])) {
                             $toStore = array('entity_component_id' => (int) $this->id, 'name' => $name, 'value' => $value);
                             $componentConfigTable->insert($toStore);
                         }
                     }
                 }
             }
     }
     return parent::update($event);
 }