public function run()
 {
     $cmslayouts = Yii::getAlias('@app/views/cmslayouts');
     $layoutFiles = [];
     if (file_exists($cmslayouts)) {
         $files = FileHelper::findFiles($cmslayouts, ['recursive' => false, 'filter' => function ($path) {
             return !in_array(substr(basename($path), 0, 1), $this->ignorePrefix);
         }]);
         foreach ($files as $file) {
             $fileinfo = FileHelper::getFileInfo($file);
             $fileBaseName = $fileinfo->name . '.' . $fileinfo->extension;
             $readableFileName = $this->generateReadableName($fileinfo->name);
             $oldTwigName = $fileinfo->name . '.twig';
             if ($fileinfo->extension !== 'php') {
                 throw new Exception("layout file '{$file}': Since 1.0.0-beta6, cms layouts must be a php file with '<?= \$placeholders['content']; ?>' instead of a twig '{{placeholders.content}}'");
             }
             $layoutFiles[] = $fileBaseName;
             $layoutFiles[] = $oldTwigName;
             $content = file_get_contents($file);
             preg_match_all("/placeholders\\[[\\'\"](.*?)[\\'\"]\\]/", $content, $results);
             $_placeholders = [];
             foreach (array_unique($results[1]) as $holderName) {
                 if (!$this->verifyVariable($holderName)) {
                     throw new Exception("Wrong variable name detected '" . $holderName . "'. Special chars are not allowed in placeholder variables, allowed chars are a-zA-Z0-9");
                 }
                 $_placeholders[] = ['label' => $this->generateReadableName($holderName), 'var' => $holderName];
             }
             $_placeholders = ['placeholders' => $_placeholders];
             $layoutItem = Layout::find()->where(['or', ['view_file' => $fileBaseName], ['view_file' => $oldTwigName]])->one();
             if ($layoutItem) {
                 $match = $this->comparePlaceholders($_placeholders, json_decode($layoutItem->json_config, true));
                 if ($match) {
                     $layoutItem->updateAttributes(['name' => $readableFileName, 'view_file' => $fileBaseName]);
                 } else {
                     $layoutItem->updateAttributes(['name' => $readableFileName, 'view_file' => $fileBaseName, 'json_config' => json_encode($_placeholders)]);
                     $this->addLog('existing cmslayout ' . $readableFileName . ' updated');
                 }
             } else {
                 // add item into the database table
                 $data = new Layout();
                 $data->scenario = 'restcreate';
                 $data->setAttributes(['name' => $readableFileName, 'view_file' => $fileBaseName, 'json_config' => json_encode($_placeholders)]);
                 $data->save(false);
                 $this->addLog('new cmslayout ' . $readableFileName . ' found and added to database.');
             }
         }
         foreach (Layout::find()->where(['not in', 'view_file', $layoutFiles])->all() as $layoutItem) {
             $layoutItem->delete();
         }
     }
 }
 public function actionDataLayouts()
 {
     return ArrayHelper::typeCast(Layout::find()->asArray()->all());
 }
示例#3
0
 public function getLayout()
 {
     return $this->hasOne(Layout::className(), ['id' => 'layout_id']);
 }
 /**
  * returns all the PAGE type specific informations.
  */
 public function actionTypePageContainer($navItemId)
 {
     $navItem = NavItem::findOne($navItemId);
     $type = $navItem->getType();
     $layout = Layout::findOne($type->layout_id);
     if (!empty($layout)) {
         $layout->json_config = json_decode($layout->json_config, true);
     }
     return ['layout' => $layout, 'type_container' => $type];
 }