Exemplo n.º 1
0
 public function configure($array)
 {
     FlexformField::deleteAll("FK_template = {$this->id} AND name NOT IN ( '" . implode('\', \'', $array) . "' )");
     foreach ($array as $key => $value) {
         if (FlexformField::find()->where(['FK_template' => $this->id, 'name' => $value])->one() == null) {
             $model = new FlexformField();
             $model->name = $value;
             $model->order = $key;
             $model->FK_template = $this->id;
             $model->save();
         }
     }
 }
Exemplo n.º 2
0
 /**
  *
  * @return boolean
  */
 public function beforeDelete()
 {
     if (parent::beforeDelete()) {
         FlexformField::deleteAll(['FK_template' => $this->id]);
         $follower = $this->randomTemplate();
         if ($follower) {
             $follower->setCurrent();
         }
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 3
0
 /**
  * Method saves to template given values 
  * @throws InvalidValueException
  * @return mixed json encoded array with status 
  */
 public function actionSaveTemplate()
 {
     if (!isset($_POST['templateId'])) {
         throw new InvalidValueException("Template id missing.");
     }
     $model = FlexformTemplate::findOne($_POST['templateId']);
     foreach (\Yii::$app->request->post($model->modelClass, null) as $key => $value) {
         $field = FlexformField::find()->where(['name' => $key, 'FK_template' => $_POST['templateId']])->one();
         if (!isset($field)) {
             continue;
         }
         if (is_array($value)) {
             $field->value = implode(',', $value);
         } else {
             $field->value = $value;
         }
         $field->save();
     }
     return json_encode(['status' => 1]);
 }
Exemplo n.º 4
0
 /**
  * Creates and set default template for given $this->formModel.
  * 
  * @throws Exception database exception if creating fails
  * @return Template instance or it's descedant
  */
 private function createDefaultTemplate()
 {
     $class = get_called_class();
     $template;
     switch ($class::TEMPLATE_TYPE) {
         case $class::TEMPLATE_FORM:
             $template = new FlexformTemplate();
             break;
         case $class::TEMPLATE_GRID:
             $template = new FlexgridTemplate();
             break;
     }
     $template->name = 'Default';
     $template->public = 1;
     $template->type = $class::TEMPLATE_TYPE;
     $template->FK_user = \Yii::$app->user->identity->id;
     $template->modelClass = $this->template_name;
     $template->current = 1;
     if (!$template->save()) {
         throw new Exception('Database error during creating deafault template.\\n' . print_r($template->getErrors(), true));
     }
     $template->refresh();
     $template->setCurrent();
     foreach ($this->getDisplayedAttributes() as $key => $value) {
         $tempField = new FlexformField();
         $tempField->FK_template = $template->id;
         $tempField->name = $value;
         $tempField->order = $key;
         $tempField->save();
     }
     return $this->template = $template;
 }