/** * Calls delete for each Content. * * @access public * @param boolean $cascade * @return boolean Returns true, if the delation was ok and false, otherwise. */ function beforeDelete($cascade) { $this->contain(); $item = $this->read(); App::import('Lib', 'ContentStream.CsConfigurator'); $config = CsConfigurator::getConfig(); $stream_config = $config['streams'][$item[$this->alias]['type']]; $Model = ClassRegistry::init($stream_config['model']); return $Model->delete($item[$this->alias]['foreign_key']); }
/** * Invoke the normalization method and populates the $settings property. * * After populates the settings, with all used values (with default values when not set), * this method create one belongsTo relationship with CsContentStream model for each * ContentStream. * * ### The options: * - * * After this method executed, the settings should be like: * {{{ * [ModelAlias] => array( * [streams] => array * [one_foreign_key] => array( * [assocName] => array( * [allowedContents] => array( * [content_type_one] => array( * 'model' => 'ContentPlugin.ContentModel', * 'title' => 'Content title', * ), * [content_type_two] => array( ... ), * . * . * . * ) * ), * [another_foreign_key] => array( * . * . * . * ) * ) * ) * }}} * * @param model $Model The model where this behavior is attached * @param array $options Configuration options. * @access public */ function setup(&$Model, $options) { if (!isset($options['streams']) || empty($options['streams']) || !is_array($options['streams']) || Set::numeric(array_keys($options['streams']))) { return !trigger_error('CsContentStreamHolderBehavior::setup - The `streams` parameter must be set on format: \'foreign_key\' => \'type\''); } $config = CsConfigurator::getConfig(); $type = $have = $callbacks = $assocName = null; foreach ($options['streams'] as $fk => &$stream) { if (!is_array($stream)) { $stream = array('type' => $stream); } if (isset($options['allowedContents'])) { $stream['allowedContents'] = $options['allowedContents']; } elseif (isset($stream['type']) && is_string($stream['type']) && isset($config['types'][$stream['type']])) { $stream['allowedContents'] = $config['types'][$stream['type']]; } else { return !trigger_error('CsContentStreamHolderBehavior - It must be set `allowedContents`(array) or `type`(string). In case of `type` set, it must be configured on content_stream plugin.'); } foreach ($stream['allowedContents'] as $k => $allowedContent) { if (!is_string($allowedContent)) { return !trigger_error('CsContentStreamHolderBehavior - Parameter allowed content must be an string.'); } elseif (!isset($config['streams'][$allowedContent])) { return !trigger_error('CsContentStreamHolderBehavior - Content type `' . $allowedContent . '` not known.'); } $stream['allowedContents'][$allowedContent] = $config['streams'][$allowedContent]; unset($stream['allowedContents'][$k]); } $stream['assocName'] = empty($stream['type']) ? Inflector::camelize($fk) : Inflector::camelize('cs_' . $stream['type']); $Model->belongsTo[$stream['assocName']] = array('className' => 'ContentStream.CsContentStream', 'foreignKey' => $fk); } $this->settings[$Model->alias] = $options; $Model->__createLinks(); $this->runtime[$Model->alias] = array(); }
/** * Content stream input. * * Most of configuraiton must be done on behavior attching and on content_stream configuration. * This method assumes that all configuration errors were corrected by behavior and needs only * the foreignKey column name where the content stream ID is stored. * * ### The options: * - baseID - Well know already. (optional) * - foreignKey - The column name. (Defaults to `cs_content_stream_id`) * - texts - Very similar to manyChilrend input format. (Defaults similiar too) * - callbaks - An array in burocratas callback format. (Defaults to nothing) * * @access public * @return string All stuff to make it works */ public function inputContentStream($options = array()) { $input_options = $options; $options = $options['options']; $options += $defaults = array('baseID' => $this->baseID(), 'foreignKey' => 'cs_content_stream_id', 'callbacks' => array(), 'texts' => array('confirm' => array()), 'url' => array('plugin' => 'burocrata', 'controller' => 'buro_burocrata', 'action' => 'list_of_items')); if ($this->_readFormAttribute('language')) { $options['url']['language'] = $this->_readFormAttribute('language'); } extract($options); // Usually the Cake core will display a "missing table" or "missing connection" // if something went wrong on registering the model $ParentModel =& ClassRegistry::init($this->modelPlugin . '.' . $this->modelAlias); // But won't hurt test if went ok if (!$ParentModel) { trigger_error('BuroBurocrataHelper::inputContentStream - Parent model could not be found.'); return false; } if ($ParentModel->Behaviors->attached('TradTradutore')) { $RealModel =& $ParentModel->{$ParentModel->Behaviors->TradTradutore->settings[$ParentModel->alias]['className']}; } else { $RealModel =& $ParentModel; } // Test if the parent model is ContentStreamed if (!$RealModel->Behaviors->attached('CsContentStreamHolder')) { trigger_error('BuroBurocrataHelper::inputContentStream - The model need to be attached with ContentStream.CsContentStreamHolder behavior.'); return false; } // Loads configuration, settings and data App::import('Lib', 'ContentStream.CsConfigurator'); $config = CsConfigurator::getConfig(); $settings = $RealModel->Behaviors->CsContentStreamHolder->settings[$RealModel->alias]; if (isset($settings['streams'][$options['foreignKey']])) { $settings = $settings['streams'][$options['foreignKey']]; $allowed_content = $settings['allowedContents']; } if (empty($this->data[$ParentModel->alias][$options['foreignKey']])) { trigger_error('BuroBurocrataHelper::inputContentStream - It is mandatory that the $this->data[' . $ParentModel->alias . '][' . $options['foreignKey'] . '] field be filled.'); return false; } $ContentStream =& $RealModel->{$settings['assocName']}; $content_stream_id = $this->data[$ParentModel->alias][$options['foreignKey']]; $data = $ContentStream->findById($content_stream_id); // Label if (empty($input_options['label'])) { $input_options['label'] = Inflector::humanize($settings['assocName']); } $out = $this->Bl->h6(array(), array('escape' => false), $input_options['label']); unset($input_options['label']); // Instructions if (isset($input_options['instructions'])) { $out .= $this->instructions(array(), array('close_me' => false), $input_options['instructions']); unset($input_options['instructions']); } // Hidden input $out .= $this->input(array(), array('type' => 'hidden', 'fieldName' => $options['foreignKey'])); // Configuration for _orderedItens method call $model_class_name = 'ContentStream.CsItem'; $parameters['fkBounding'] = array($this->_name('CsItem.cs_content_stream_id') => $content_stream_id); $parameters['contentType'] = array($this->_name('CsItem.type') => '#{content_type}'); if ($ContentStream->CsItem->Behaviors->attached('Ordered')) { $fieldName = $this->_name('CsItem.' . $ContentStream->CsItem->Behaviors->Ordered->settings['CsItem']['field']); $parameters['orderField'] = array($fieldName => '#{order}'); } $this->sform(array(), array('model' => $settings['assocName'], 'data' => $data)); $out .= $this->_orderedItens(compact('url', 'texts', 'model_class_name', 'foreign_key', 'parameters', 'allowed_content', 'baseID', 'callbacks', 'auto_order')); $this->eform(); return $this->Bl->div(array('id' => 'div' . $baseID, 'class' => 'content_stream'), array(), $out); }