Example #1
0
 public static function block($place, $isAllow, $blockLogic)
 {
     $key = Content::GetKey($place);
     $data = unserialize(Content::Get($key));
     if (!is_array($data)) {
         $data = [];
     }
     $templateData = [];
     $editfield = '';
     foreach ($blockLogic['rules'] as $field => $rule) {
         if ($rule['type'] != 'editcontrol') {
             $value = isset($data[$field]) && $data[$field] != '' ? $data[$field] : '';
             $templateData[$field] = $value;
             $blockLogic['rules'][$field]['value'] = $value;
         } else {
             $editfield = $field;
         }
     }
     if ($editfield != '') {
         $editcontrol = '';
         if (Content::IsEdit() && $isAllow) {
             $editcontrol = '<i class="edit-element glyphicon glyphicon-pencil" place=\'' . $key . '\' content=\'block\' data-block=\'' . base64_encode(json_encode($blockLogic)) . '\'></i>';
         }
         $templateData[$editfield] = $editcontrol;
     }
     return self::parseBlock($templateData, $blockLogic['template']);
 }
 public function actionBlocksave()
 {
     $return = ['class' => 'danger', 'msg' => Yii::t('app', 'Saving error')];
     $post = Yii::$app->request->post();
     if (isset($post['ContentData'])) {
         $contentData = $post['ContentData'];
         $model = ContentData::place($contentData['place']);
         if (!isset($model)) {
             $model = new ContentData();
             $model->place = $contentData['place'];
         }
         $model->content = isset($post['DynamicModel']) ? serialize($post['DynamicModel']) : '';
     } else {
         $model = new ContentData();
     }
     $model->type = 'block';
     $blockData = $post['data-block'];
     $data = json_decode(base64_decode($blockData));
     $dataModel = $this->createContentFormModel($data);
     foreach ($post['DynamicModel'] as $key => $value) {
         $dataModel->{$key} = $value;
     }
     if ($model->validate() && $dataModel->validate() && $model->save()) {
         Content::Set($model->place, $model->content);
         $return = array('class' => 'success', 'msg' => Yii::t('app', 'Data is saved'));
     } else {
         $errors = $dataModel->getErrors();
         $return = array('class' => 'danger', 'msg' => Yii::t('app', 'Saving error'), 'error_fields' => array_keys($errors));
     }
     echo JSON::encode($return);
 }