public function run() { $dependency = new CDbCacheDependency('SELECT MAX(last_modified) FROM bloc WHERE parent_id=:parent_id AND unique_id=:unique_id'); $dependency->params = array(':parent_id' => $this->parentId, ':unique_id' => $this->uniqueId); $models = Bloc::model()->cache(60 * 60 * 24 * 30, $dependency)->findAllByAttributes(array('parent_id' => $this->parentId, 'unique_id' => $this->uniqueId), array('order' => 'rank ASC')); $blocs = array(); foreach ($models as $bloc) { $blocs[] = array(); $blocsIndex = count($blocs) - 1; $blocs[$blocsIndex][0] = $bloc; $blocClass = ucfirst($bloc->bloc_type) . 'Bloc'; if (!array_key_exists($bloc->bloc_type, Yii::app()->cms->blocs)) { Yii::import('application.components.blocs.' . $bloc->bloc_type . '.*'); Yii::import('application.components.blocs.' . $bloc->bloc_type . '.models.*'); Yii::app()->cms->blocs[$bloc->bloc_type] = new $blocClass(); } $className = 'Bloc' . ucfirst($bloc->bloc_type); $blocs[$blocsIndex][1] = $className::model()->findByPk($bloc->bloc_id); $blocs[$blocsIndex][2] = $bloc->bloc_type; } if ($this->registerMetaDesc) { $metaDesc = ''; foreach ($blocs as $bloc) { if ($bloc[0]->title_page) { $metaDesc .= $bloc[0]->title . ' - '; } if ($bloc[0]->bloc_type == "editor") { $shortText = strip_tags($bloc[1]->html); if (strlen($shortText) > 200) { $shortText = trim(substr($shortText, 0, 200)) . '…'; } $metaDesc .= $shortText . ' - '; } } if ($metaDesc != '') { Yii::app()->clientScript->registerMetaTag(substr($metaDesc, 0, -3), 'description'); } } $this->render('blocsWidget', array('blocs' => $blocs)); }
/** * Saving related Bloc entry. */ public function afterSave($event) { $owner = $this->getOwner(); if ($owner->isNewRecord) { $contentBloc = new Bloc(); if (Yii::app()->languageManager->multilang) { $contentBloc->multilang(); } $contentBloc->parent_id = $owner->parent_id; $contentBloc->bloc_type = mb_substr($owner->tableName(), 5); $contentBloc->bloc_id = $owner->primaryKey; } else { if (Yii::app()->languageManager->multilang) { $contentBloc = Bloc::model()->multilang()->findByAttributes(array('bloc_id' => $owner->primaryKey, 'bloc_type' => mb_substr($owner->tableName(), 5))); } else { $contentBloc = Bloc::model()->findByAttributes(array('bloc_id' => $owner->primaryKey, 'bloc_type' => mb_substr($owner->tableName(), 5))); } } $contentBloc->rank = $owner->rank; $contentBloc->title_anchor = $owner->title_anchor; $contentBloc->title_page = $owner->title_page; $contentBloc->unique_id = $owner->unique_id; $contentBloc->last_modified = date('Y-m-d H:i:s'); foreach (Yii::app()->languageManager->languages as $l => $fullLanguage) { if ($l === Yii::app()->sourceLanguage) { $suffix = ''; } else { $suffix = '_' . $l; } $contentBloc->{'title' . $suffix} = $owner->{'title' . $suffix}; if ($owner->isNewRecord) { $contentBloc->{'title_url' . $suffix} = AdminHelper::generateUrlStr($owner->{'title' . $suffix}, $contentBloc, 'title_url', null, $l); } else { $contentBloc->{'title_url' . $suffix} = AdminHelper::generateUrlStr($owner->{'title' . $suffix}, $contentBloc, 'title_url', $contentBloc->id, $l); } } $contentBloc->save(false); }
/** * Process and manipulate the settings of one form. This function is called recursively. * * Generates forms for blocs and loads models given as string with actual models. * * @param array $form the form settings to process. Passed by reference. * @param null|CActiveRecord|array $parentModels the models of the parent form. Can be different types depending on the context. Defaults to null. * * @return array the processed form settings. */ protected function processFormSettingsForm(&$form, $parentModels = null) { $this->onBeforeProcessFormSettings(new CEvent($this, array('parentModels' => $parentModels, 'form' => &$form, 'id' => $this->id))); // If the blocs parameter is given, the form is generated here. $blocsUniqueId = null; if (isset($form['blocs'])) { $blocsUniqueId = $form['blocs']; // Initialize blocs and create forms for blocs sub models. $blocsSubModelsFormArr = array(); foreach (Yii::app()->params['blocs'] as $bloc) { $blocClass = ucfirst($bloc) . 'Bloc'; if (!array_key_exists($bloc, Yii::app()->cms->blocs)) { Yii::import('application.components.blocs.' . $bloc . '.*'); Yii::import('application.components.blocs.' . $bloc . '.models.*'); Yii::app()->cms->blocs[$bloc] = new $blocClass(); } $blocSubModelsArr = Yii::app()->cms->blocs[$bloc]->subModels(); if (is_array($blocSubModelsArr)) { foreach ($blocSubModelsArr as $subModel) { $blocsSubModelsFormArr[] = array('id' => '{formId}-{itemId}-' . lcfirst($subModel[0]) . 'Form', 'models' => $subModel[0], 'parentIdAttribute' => $subModel[1], 'varName' => $form['blocs'] . '_' . lcfirst($subModel[0]) . 'Form', 'blocsSubForm' => true); } } } $newForm = array('id' => $form['id'], 'varName' => $form['varName'], 'models' => null, 'parentIdAttribute' => 'parent_id', 'forms' => $blocsSubModelsFormArr, 'onBeforeSetAttributes' => function ($event) use($blocsUniqueId) { $model = $event->params['model']; $paramName = $event->params['paramName']; $paramNameArray = explode('[', $paramName); $formId = mb_substr($paramNameArray[1], 0, -1); $itemId = mb_substr($paramNameArray[2], 0, -1); $postItem = $_POST[get_class($model)][$formId][$itemId]; $model->title_anchor = $postItem['title_anchor']; $model->title_page = $postItem['title_page']; $model->rank = $postItem['rank']; $model->unique_id = $blocsUniqueId; foreach (Yii::app()->languageManager->suffixes as $suffix) { $model->{'title' . $suffix} = $postItem['title' . $suffix]; } }); // Loading of the blocs models. $contentBlocs = Bloc::model()->multilang()->findAllByAttributes(array('parent_id' => $parentModels !== null ? $parentModels->primaryKey : null, 'unique_id' => $form['blocs']), array('index' => 'id', 'order' => 'rank ASC')); $blocsModels = array(); foreach ($contentBlocs as $id => $contentBloc) { $className = 'Bloc' . ucfirst($contentBloc->bloc_type); if (array_key_exists('ml', $className::model()->behaviors())) { $blocsModels[$id] = $className::model()->multilang()->findByPk($contentBloc->bloc_id); } else { $blocsModels[$id] = $className::model()->findByPk($contentBloc->bloc_id); } $blocsModels[$id]->rank = $contentBloc->rank; $blocsModels[$id]->title_anchor = $contentBloc->title_anchor; $blocsModels[$id]->title_page = $contentBloc->title_page; foreach (Yii::app()->languageManager->suffixes as $suffix) { $blocsModels[$id]->{'title' . $suffix} = $contentBloc->{'title' . $suffix}; } } $newForm['models'] = $blocsModels; // Loading of the blocs sub-models. $this->onBeforeProcessFormSettings = function ($event) { $form =& $event->params['form']; if (isset($form['blocsSubForm'])) { unset($form['blocsSubForm']); // parentModels here contains blocs from multiple different models ex: BlocDocument, BlocEditor, etc. $parentModels = $event->params['parentModels']; $model = new $form['models'](); $behaviors = $model->behaviors(); $findOptions = array('index' => $model->tableSchema->primaryKey); if (isset($form['orderAttribute'])) { $findOptions['order'] = $form['orderAttribute'] . ' ASC'; } $form['models'] = array(); if (!empty($parentModels)) { foreach ($parentModels as $id => $parentModel) { $relations = $model->relations(); if (get_class($parentModel) != $relations['bloc'][1]) { continue; } if (isset($behaviors['ml'])) { $form['models'][$id] = $model->multilang()->findAllByAttributes(array($form['parentIdAttribute'] => $parentModel->primaryKey), $findOptions); } else { $form['models'][$id] = $model->findAllByAttributes(array($form['parentIdAttribute'] => $parentModel->primaryKey), $findOptions); } } } } }; $form = $newForm; } // Models given in the form of a string are automatically created / loaded here. if (is_string($form['models'])) { // 1st level models with no parent. A single model is created / loaded. Update loads model from GET id var. // If there are multiple forms like this or if you want an array of unparented models, you must load your models in your controller this won't work. if ($parentModels === null) { if ($this->_action == 'Create') { $form['models'] = new $form['models'](); } elseif ($this->_action == 'Update') { $form['models'] = $this->loadModel($form['models']); } } elseif (is_object($parentModels)) { $parentPk = $parentModels->primaryKey; $model = new $form['models'](); $behaviors = $model->behaviors(); $findOptions = array('index' => $model->tableSchema->primaryKey); if (isset($form['orderAttribute'])) { $findOptions['order'] = $form['orderAttribute'] . ' ASC'; } if (isset($behaviors['ml'])) { $form['models'] = $model->multilang()->findAllByAttributes(array($form['parentIdAttribute'] => $parentPk), $findOptions); } else { $form['models'] = $model->findAllByAttributes(array($form['parentIdAttribute'] => $parentPk), $findOptions); } } else { $model = new $form['models'](); $behaviors = $model->behaviors(); $findOptions = array('index' => $model->tableSchema->primaryKey); if (isset($form['orderAttribute'])) { $findOptions['order'] = $form['orderAttribute'] . ' ASC'; } $form['models'] = array(); foreach ($parentModels as $id => $parentModel) { if (isset($behaviors['ml'])) { $form['models'][$id] = $model->multilang()->findAllByAttributes(array($form['parentIdAttribute'] => $parentModel->primaryKey), $findOptions); } else { $form['models'][$id] = $model->findAllByAttributes(array($form['parentIdAttribute'] => $parentModel->primaryKey), $findOptions); } } } } $this->onAfterProcessFormSettings(new CEvent($this, array('parentModels' => $parentModels, 'form' => &$form, 'id' => $this->id))); $this->_formIds[$form['varName']] = array($form['id'], $blocsUniqueId); unset($form['varName']); // Recursively processing child forms. if (isset($form['forms'])) { if (!isset($form['forms']['id'])) { foreach ($form['forms'] as $key => $subForm) { $form['forms'][$key] = $this->processFormSettingsForm($form['forms'][$key], $form['models']); } } else { $form['forms'] = $this->processFormSettingsForm($form['forms'], $form['models']); } } return $form; }