Example #1
0
 public function import(\luya\commands\ExecutableController $exec)
 {
     $log = ['filters' => []];
     foreach ($exec->getFilesNamespace('filters') as $filterClassName) {
         if (!class_exists($filterClassName)) {
             continue;
         }
         $object = new $filterClassName();
         $log['filters'][$filterClassName] = $object->save();
     }
     return $log;
 }
Example #2
0
 /**
  * @todo do not only import, also update changes in the template
  * @todo how do we send back values into the executblae controller for output purposes?
  */
 public function import(ExecutableController $exec)
 {
     $_log = ['blocks' => [], 'layouts' => []];
     $allblocks = Block::find()->all();
     $exists = [];
     foreach ($exec->getFilesNamespace('blocks') as $ns) {
         $model = Block::find()->where(['class' => $ns])->asArray()->one();
         if (!$model) {
             $block = new Block();
             $block->scenario = 'commandinsert';
             $block->setAttributes(['group_id' => 1, 'system_block' => 0, 'class' => $ns]);
             $block->insert();
             $_log['blocks'][$ns] = 'new block has been added to database.';
         } else {
             $_log['blocks'][$ns] = 'already in the database.';
             $exists[] = $model['id'];
         }
     }
     foreach ($allblocks as $block) {
         if (!in_array($block->id, $exists)) {
             $block->delete();
         }
     }
     /* import project specific layouts */
     $cmslayouts = Yii::getAlias('@app/views/cmslayouts');
     $layoutFiles = [];
     if (file_exists($cmslayouts)) {
         foreach (scandir($cmslayouts) as $file) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             $layoutFiles[] = $file;
             $layoutItem = Layout::find()->where(['view_file' => $file])->one();
             $content = file_get_contents($cmslayouts . DIRECTORY_SEPARATOR . $file);
             // find all twig brackets
             preg_match_all("/\\{\\{(.*?)\\}\\}/", $content, $results);
             // local vars
             $_placeholders = [];
             $_vars = [];
             // explode the specific vars for each type
             foreach ($results[1] as $match) {
                 $parts = explode('.', trim($match));
                 switch ($parts[0]) {
                     case 'placeholders':
                         $_placeholders[] = ['label' => $parts[1], 'var' => $parts[1]];
                         break;
                     case 'vars':
                         $_vars = $parts[1];
                         break;
                 }
             }
             $_placeholders = ['placeholders' => $_placeholders];
             if ($layoutItem) {
                 $match = $this->comparePlaceholders($_placeholders, json_decode($layoutItem->json_config, true));
                 if ($match) {
                     continue;
                 }
                 $layoutItem->scenario = 'restupdate';
                 $layoutItem->setAttributes(['name' => ucfirst($file), 'view_file' => $file, 'json_config' => json_encode($_placeholders)]);
                 $layoutItem->save();
                 $_log['layouts'][$file] = "existing cmslayout {$file} updated.";
             } else {
                 // add item into the database table
                 $data = new Layout();
                 $data->scenario = 'restcreate';
                 $data->setAttributes(['name' => ucfirst($file), 'view_file' => $file, 'json_config' => json_encode($_placeholders)]);
                 $data->save();
                 $_log['layouts'][$file] = "new cmslayout {$file} found and inserted.";
             }
         }
         foreach (Layout::find()->where(['not in', 'view_file', $layoutFiles])->all() as $layoutItem) {
             $layoutItem->delete();
         }
     }
     return $_log;
 }