Exemple #1
0
 public function init()
 {
     $this->id = $this->model->formName();
     $this->action = RequestModule::getPopupUrl(['type' => $this->model->tableName()]);
     $this->fieldConfig = ['template' => '<div class="row">{input}{error}</div>', 'errorOptions' => ['class' => 'errorMessage']];
     parent::init();
 }
 public function init()
 {
     parent::init();
     if (!$this->hasModel() || !isset($this->model->{$this->attribute}) && !is_null($this->model->{$this->attribute}) || is_null($this->data) && empty($this->attributeLabel)) {
         throw new InvalidConfigException("Either 'model' and 'attribute' and 'attributeLabel' properties must be specified.");
     }
     /** @var ActiveRecord[] $models */
     $models = $this->model->{$this->attribute};
     if (!$this->data && is_array($models) && current($models) instanceof ActiveRecord) {
         $primaryKey = $models[0]->primaryKey();
         if (!isset($primaryKey[0])) {
             throw new Exception('"' . get_class($models[0]) . '" must have a primary key.');
         }
         $this->data = ArrayHelper::map($models, $primaryKey[0], $this->attributeLabel);
     }
     $this->scriptOptions['data'] = $this->data;
     if (!isset($this->scriptOptions['templateItem']) || !$this->scriptOptions['templateItem']) {
         $this->scriptOptions['templateItem'] = Html::tag('li', '{text}' . Html::a(Html::tag('span', '', ['class' => 'glyphicon glyphicon-remove']), '#', ['class' => 'checkbox-multiple-remove-item', 'onclick' => 'return false;']), ['class' => 'checkbox-multiple-item']);
     }
     if (!isset($this->scriptOptions['templateCheckbox']) || !$this->scriptOptions['templateCheckbox']) {
         $this->scriptOptions['templateCheckbox'] = Html::checkbox($this->model->formName() . '[' . $this->attribute . '][]', true, ['value' => '{id}']);
     }
     if (!isset($this->scriptOptions['templateResultItem']) || !$this->scriptOptions['templateResultItem']) {
         $this->scriptOptions['templateResultItem'] = Html::tag('li', '{text}', ['class' => 'checkbox-multiple-result-item']);
     }
     if (!isset($this->scriptOptions['templateInput']) || !$this->scriptOptions['templateInput']) {
         $this->scriptOptions['templateInput'] = Html::textInput('checkbox-multiple-input', '', ['class' => 'form-control']);
     }
     if (!isset($this->scriptOptions['errorMessage'])) {
         $this->scriptOptions['errorMessage'] = Yii::t('app', 'Error');
     }
     if (!isset($this->scriptOptions['templateResultError']) || !$this->scriptOptions['templateResultError']) {
         $this->scriptOptions['templateResultError'] = Html::tag('li', '{text}', ['class' => 'checkbox-multiple-result-error']);
     }
     if (!isset($this->scriptOptions['warningMessage'])) {
         $this->scriptOptions['warningMessage'] = Yii::t('app', 'No results');
     }
     if (!isset($this->scriptOptions['templateResultWarning']) || !$this->scriptOptions['templateResultWarning']) {
         $this->scriptOptions['templateResultWarning'] = Html::tag('li', '{text}', ['class' => 'checkbox-multiple-result-warning']);
     }
     if (empty($this->placeholder)) {
         $this->placeholder = Yii::t('app', 'Search ...');
     }
     $this->scriptOptions['templatePlaceholder'] = Html::tag('li', $this->placeholder, ['class' => 'checkbox-multiple-placeholder']);
     Html::addCssClass($this->options, 'checkbox-multiple');
     $this->registerAsset();
     $this->registerSlimScrollAsset();
     $this->registerClientScript();
 }
 public function formName()
 {
     if ($this->scenario == 'search') {
         return '';
     }
     return parent::formName();
 }
 public function formName()
 {
     $class = get_class($this);
     if (!isset(self::$formNameCache[$class])) {
         self::$formNameCache[$class] = parent::formName();
     }
     return self::$formNameCache[$class];
 }
 /**
  * @inheritdoc
  */
 public function formName()
 {
     $name = parent::formName();
     if ($this->language) {
         $name .= '_' . $this->language;
     }
     return $name;
 }
Exemple #6
0
 /**
  * Load data into model. Using setters
  * @see [[Model::load()]]
  * @param array $data
  * @param null $formName
  * @return bool
  */
 public function load($data, $formName = null)
 {
     $scope = $formName === null ? $this->model->formName() : $formName;
     if ($scope != '' && isset($data[$scope])) {
         $data = $data[$scope];
     }
     foreach ($data as $attribute => $value) {
         $this->model->{$attribute} = $value;
     }
     return true;
 }
 /**
  * @param array $data
  * @param string|null $formName
  */
 public function saveTranslations($data = [], $formName = null)
 {
     $scope = $formName === null ? $this->owner->formName() : $formName;
     if ($scope === '' && !empty($data)) {
         $this->saveData($data);
         return;
     }
     if (isset($data[$scope])) {
         $this->saveData($data[$scope]);
         return;
     }
 }
 public function afterSave()
 {
     $data = [];
     if (isset($_POST[$this->owner->formName()])) {
         $data = $_POST[$this->owner->formName()];
     }
     if ($data) {
         foreach ($data as $attribute => $value) {
             if (!in_array($attribute, $this->relations)) {
                 continue;
             }
             if (is_array($value)) {
                 $relation = $this->owner->getRelation($attribute);
                 if ($relation->via !== null) {
                     /** @var ActiveRecord $foreignModel */
                     $foreignModel = $relation->modelClass;
                     $this->owner->unlinkAll($attribute, true);
                     if (is_array(current($value))) {
                         foreach ($value as $data) {
                             if (isset($data[$foreignModel::primaryKey()[0]]) && $data[$foreignModel::primaryKey()[0]] > 0) {
                                 $fm = $foreignModel::findOne($data[$foreignModel::primaryKey()[0]]);
                                 $fm->load($data, '');
                                 $this->owner->link($attribute, $fm);
                             }
                         }
                     } else {
                         foreach ($value as $fk) {
                             if (preg_match('~^\\d+$~', $fk)) {
                                 $this->owner->link($attribute, $foreignModel::findOne($fk));
                             }
                         }
                     }
                 }
             } else {
                 $this->owner->unlinkAll($attribute, true);
             }
         }
     }
 }
Exemple #9
0
 /**
  * ImportLog constructor.
  * @param iImportFile $ImportFile
  * @param ActiveRecord $activeRecordLog
  * @throws \Exception
  */
 public function __construct(iImportFile $ImportFile, ActiveRecord $activeRecordLog)
 {
     $formName = strtolower($activeRecordLog->formName());
     $this->setType($formName . '_type');
     $this->setFilename($formName . '_filename');
     $this->setFilelastdate($formName . '_filelastdate');
     $this->setRownum($formName . '_rownum');
     $this->setMessage($formName . '_message');
     $this->setImportFile($ImportFile);
     $this->setActiveRecordLog($activeRecordLog);
     if (!$this->getActiveRecordLog()->hasAttribute('id_logreport')) {
         throw new \Exception('Отсутствует свойство id_logreport');
     }
     $this->getActiveRecordLog()->id_logreport = $ImportFile->getLogReport()->primaryKey;
     $this->getActiveRecordLog()->{$this->getType()} = 1;
     $this->getActiveRecordLog()->{$this->getFilename()} = $ImportFile->getFileName();
     $this->getActiveRecordLog()->{$this->getFilelastdate()} = $ImportFile->getFileLastDate();
     $this->getActiveRecordLog()->{$this->getRownum()} = $ImportFile->getRow();
     $this->getActiveRecordLog()->{$this->getMessage()} = 'Запись добавлена.';
 }
 /**
  * @param ActiveRecord $model
  * @param null|string $language
  * @param array|null $metaTagsToFetch
  */
 public static function register(ActiveRecord $model, $language = null, $metaTagsToFetch = null)
 {
     $metaTags = MetaTagContent::find()->where([MetaTagContent::tableName() . '.model_id' => $model->id])->andWhere([MetaTagContent::tableName() . '.model_name' => $model->formName()])->joinWith(['metaTag']);
     if (is_array($metaTagsToFetch)) {
         $metaTags->andWhere([MetaTag::tableName() . '.name' => $metaTagsToFetch]);
     } else {
         $metaTags->andWhere([MetaTag::tableName() . '.is_active' => 1]);
     }
     if (!is_string($language)) {
         $language = Yii::$app->language;
     }
     $metaTags->andWhere([MetaTagContent::tableName() . '.language' => $language]);
     /** @var MetaTagContent[] $metaTags */
     $metaTags = $metaTags->all();
     foreach ($metaTags as $metaTag) {
         $content = $metaTag->getMetaTagContent();
         foreach ($model->attributes as $name => $value) {
             $content = str_replace("[{$name}]", $value, $content);
         }
         if (!empty($content)) {
             if (strtolower($metaTag->metaTag->name) === MetaTag::META_TITLE_NAME) {
                 Yii::$app->getView()->title = $content;
             } elseif (strtolower($metaTag->metaTag->name) === MetaTag::META_SEO_TEXT) {
                 self::$seoText .= $content;
             } elseif (strtolower($metaTag->metaTag->name) === MetaTag::META_ROBOTS) {
                 if ($content) {
                     Yii::$app->view->registerMetaTag(['name' => 'robots', 'content' => 'noindex, FOLLOW'], 'robots');
                 }
             } else {
                 if ($metaTag->metaTag->name) {
                     Yii::$app->view->registerMetaTag([$metaTag->metaTag->is_http_equiv ? 'http-equiv' : 'name' => $metaTag->metaTag->name, 'content' => $content], $metaTag->metaTag->name);
                 }
             }
         }
     }
 }
Exemple #11
0
 /**
  * @param \yii\db\ActiveRecord $model
  * @param \yii\db\ActiveRecord $relatedModel
  * @param \yii\db\ActiveQuery $relation
  * @return array array with three arrays: create, search and index routes
  */
 public static function getRelationRoutes($model, $relatedModel, $relation)
 {
     if (($route = Yii::$app->crudModelsMap[$relatedModel::className()]) === null) {
         return [null, null, null];
     }
     $allowCreate = Yii::$app->user->can($relatedModel::className() . '.create');
     if ($allowCreate && $model->isNewRecord && $relation->multiple) {
         foreach ($relation->link as $left => $right) {
             if (!$relatedModel->getTableSchema()->getColumn($left)->allowNull) {
                 $allowCreate = false;
                 break;
             }
         }
     }
     if (!$allowCreate) {
         $createRoute = null;
     } else {
         $createRoute = [$route . '/update'];
         if ($relation->multiple) {
             $createRoute['hide'] = implode(',', array_keys($relation->link));
             $scope = $relatedModel->formName();
             $primaryKey = $model->getPrimaryKey(true);
             foreach ($relation->link as $left => $right) {
                 if (!isset($primaryKey[$right])) {
                     continue;
                 }
                 $createRoute[$scope][$left] = $primaryKey[$right];
             }
         }
     }
     $parts = explode('\\', $relatedModel::className());
     $relatedModelClass = array_pop($parts);
     $relatedSearchModelClass = implode('\\', $parts) . '\\search\\' . $relatedModelClass;
     $searchRoute = !class_exists($relatedSearchModelClass) ? null : [$route . '/relation', 'per-page' => 10, 'relation' => $relation->inverseOf, 'id' => Action::exportKey($model->getPrimaryKey()), 'multiple' => $relation->multiple ? 'true' : 'false'];
     $indexRoute = [$route . '/index'];
     return [$createRoute, $searchRoute, $indexRoute];
 }
 /**
  * @inheritdoc
  */
 public function formName()
 {
     if ($this->scenario == self::SCENARIO_SEARCH) {
         return StringHelper::basename(self::className()) . 'Search';
     }
     return parent::formName();
 }
Exemple #13
0
 public function formName()
 {
     if ($this->scenario == 'newItem') {
         return 'MenuNewItem';
     } elseif ($this->scenario == 'updateMenuItems') {
         return 'menu-items[' . $this->id . ']';
     } else {
         return parent::formName();
     }
 }
Exemple #14
0
 /**
  * Присваивает выбранное значение из справочника модели, в сессии.
  * При выборе значения из справочника, значение присваивается в сессию предыдущей хлебной крошки, для формы, с которой был открыт справочник.
  * @param bool $RedirectPreviousUrl
  * @param ActiveRecord $ActiveRecord Модель к которой присваивается знаечния из справочника.
  * @param string $AttributeForeignID Имя атрибута
  * @return string
  */
 public static function AssignToModelFromGrid($RedirectPreviousUrl = True, $ActiveRecord = NULL, $AttributeForeignID = NULL)
 {
     if (Yii::$app->request->isAjax) {
         $LastBC = Proc::GetLastBreadcrumbsFromSession();
         $assigndata = filter_input(INPUT_POST, 'assigndata');
         $foreign = isset($LastBC['dopparams']['foreign']) ? $LastBC['dopparams']['foreign'] : '';
         if (!empty($foreign) && !empty($assigndata)) {
             $BC = Proc::GetBreadcrumbsFromSession();
             end($BC);
             prev($BC);
             $BC[key($BC)]['dopparams'][$foreign['model']][$foreign['field']] = $assigndata;
             $session = new Session();
             $session->open();
             $session['breadcrumbs'] = $BC;
             $session->close();
             if ($ActiveRecord instanceof ActiveRecord && is_string($AttributeForeignID)) {
                 $field = $LastBC['dopparams']['foreign']['field'];
                 if ($ActiveRecord->formName() === $LastBC['dopparams']['foreign']['model']) {
                     $ActiveRecord->{$field} = $assigndata;
                     $ActiveRecord->{$AttributeForeignID} = $foreign['id'];
                     if ($ActiveRecord->validate()) {
                         $ActiveRecord->save(false);
                     }
                 }
             }
             if ($RedirectPreviousUrl) {
                 Yii::$app->response->redirect(Proc::GetPreviousURLBreadcrumbsFromSession());
             }
         } else {
             return 'error foreign or assigndata empty AssignToModelFromGrid()';
         }
     }
 }