public function init() { parent::init(); if (empty($this->model)) { throw new InvalidConfigException("Property {model} cannot be blank"); } $hasFileBehavior = false; foreach ($this->model->getBehaviors() as $behavior) { if (is_a($behavior, FileBehavior::className())) { $hasFileBehavior = true; } } if (!$hasFileBehavior) { throw new InvalidConfigException("The behavior {FileBehavior} has not been attached to the model."); } }
public function run() { if (!$this->model) { return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The model cannot be empty.'), ['class' => 'alert alert-danger']); } $hasFileBehavior = false; foreach ($this->model->getBehaviors() as $behavior) { if (is_a($behavior, FileBehavior::className())) { $hasFileBehavior = true; } } if (!$hasFileBehavior) { return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The behavior FileBehavior has not been attached to the model.'), ['class' => 'alert alert-danger']); } Url::remember(Url::current()); return GridView::widget(['dataProvider' => new ArrayDataProvider(['allModels' => $this->model->getFiles()]), 'layout' => '{items}', 'tableOptions' => $this->tableOptions, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['label' => $this->getModule()->t('attachments', 'File name'), 'format' => 'raw', 'value' => function ($model) { return Html::a("{$model->name}.{$model->type}", $model->getUrl()); }], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{delete}', 'buttons' => ['delete' => function ($url, $model, $key) { return Html::a('<span class="glyphicon glyphicon-trash"></span>', ['/attachments/file/delete', 'id' => $model->id], ['title' => Yii::t('yii', 'Delete'), 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'data-method' => 'post']); }]]]]); }
public function run() { if (!$this->model) { return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The model cannot be empty.'), ['class' => 'alert alert-danger']); } $hasFileBehavior = false; foreach ($this->model->getBehaviors() as $behavior) { if ($behavior instanceof FileBehavior) { $hasFileBehavior = true; break; } } if (!$hasFileBehavior) { return Html::tag('div', Html::tag('b', Yii::t('yii', 'Error')) . ': ' . $this->getModule()->t('attachments', 'The behavior FileBehavior has not been attached to the model.'), ['class' => 'alert alert-danger']); } $confirm = Yii::t('yii', 'Are you sure you want to delete this item?'); $js = <<<JS \$(".delete-button").click(function(){ var tr = this.closest('tr'); var url = \$(this).data('url'); if (confirm("{$confirm}")) { \$.ajax({ method: "POST", url: url, success: function(data) { if (data) { tr.remove(); } } }); } }); JS; Yii::$app->view->registerJs($js); return GridView::widget(['dataProvider' => new ArrayDataProvider(['allModels' => $this->model->getFiles()]), 'layout' => '{items}', 'tableOptions' => $this->tableOptions, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], ['label' => $this->getModule()->t('attachments', 'File name'), 'format' => 'raw', 'value' => function ($model) { return Html::a("{$model->name}.{$model->type}", $model->getUrl(), ['class' => ' group' . $model->itemId, 'onclick' => 'return false;']); }], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{delete}', 'buttons' => ['delete' => function ($url, $model, $key) { return Html::a('<span class="glyphicon glyphicon-trash"></span>', '#', ['class' => 'delete-button', 'title' => Yii::t('yii', 'Delete'), 'data-url' => Url::to(['/attachments/file/delete', 'id' => $model->id])]); }]]]]) . Colorbox::widget(['targets' => ['.group' . $this->model->id => ['rel' => '.group' . $this->model->id, 'photo' => true, 'scalePhotos' => true, 'width' => '100%', 'height' => '100%', 'maxWidth' => 800, 'maxHeight' => 600]], 'coreStyle' => 4]); }
/** * @return bool */ protected function isBehaviorIncluded() { if (isset($this->_behaviorIncluded)) { return $this->_behaviorIncluded; } if (!isset($this->aggregateModel) || !isset($this->userValue)) { foreach ($this->model->getBehaviors() as $behavior) { if ($behavior instanceof VoteBehavior) { return $this->_behaviorIncluded = true; } } } return $this->_behaviorIncluded = false; }
/** * @param ActiveRecord $model * @return TrackableBehavior */ private function getBehavior(ActiveRecord $model) { foreach ($model->getBehaviors() as $behavior) { if ($behavior instanceof TrackableBehavior) { return $behavior; } } return null; }
/** * @param ActiveRecord $model */ protected function excludeField($model, $addition = '') { $this->fieldExclude = ArrayHelper::merge(is_array($this->excludedField) ? $this->excludedField : [$this->excludedField], $this->fieldExclude); if (is_callable($this->excludeFieldCallback)) { $func = $this->excludeFieldCallback; $result = $func($this); $result = $result ? is_array($result) ? $result : [$result] : []; $this->fieldExclude = ArrayHelper::merge($this->fieldExclude, $result); } $this->fieldExclude = ArrayHelper::merge($this->fieldExclude, $model->getTableSchema()->primaryKey); $behaviors = $model->getBehaviors(); foreach ($behaviors as $_next) { $_nextCopy = $_next; $_next = (array) $_next; $_next['class'] = $_nextCopy->className(); if ($_next['class'] === BlameableBehavior::className()) { if (isset($_next['updatedByAttribute'])) { if (!empty($_next['updatedByAttribute'])) { $this->fieldExclude[] = $addition . $_next['updatedByAttribute']; } } else { $this->fieldExclude[] = $addition . 'updated_by'; } if (isset($_next['createdByAttribute'])) { if (!empty($_next['createdByAttribute'])) { $this->fieldExclude[] = $addition . $_next['createdByAttribute']; } } else { $this->fieldExclude[] = $addition . 'created_by'; } } else { if ($_next['class'] === TimestampBehavior::className()) { if (isset($_next['updatedAtAttribute'])) { if (!empty($_next['updatedAtAttribute'])) { $this->fieldExclude[] = $addition . $_next['updatedAtAttribute']; } } else { $this->fieldExclude[] = $addition . 'updated_at'; } if (isset($_next['createdAtAttribute'])) { if (!empty($_next['createdAtAttribute'])) { $this->fieldExclude[] = $addition . $_next['createdAtAttribute']; } } else { $this->fieldExclude[] = $addition . 'created_at'; } } else { if ($_next['class'] === PhoneInputBehavior::className()) { if (!$this->searchColumn($_next['phoneAttribute'])) { $this->columns[] = new WidgetsCrud(['fieldName' => $_next['phoneAttribute'], 'widgetType' => 'phone']); } } else { if ($_next['class'] === UploadBehavior::className()) { if (!is_array($this->hasUploadBehavior)) { $this->hasUploadBehavior = ['attributes' => []]; } $this->hasUploadBehavior['attributes'][] = isset($_next['attribute']) ? $_next['attribute'] : $_next['attribute']; $this->hasUploadBehavior[] = $_next['pathAttribute']; $this->hasUploadBehavior[] = $_next['baseUrlAttribute']; $this->fieldExclude[] = $addition . $_next['pathAttribute']; $this->fieldExclude[] = $addition . $_next['baseUrlAttribute']; if (!$this->searchColumn($this->hasUploadBehavior)) { $this->columns[] = new WidgetsCrud(['fieldName' => $addition . $_next['attribute'], 'widgetType' => 'upload']); } if (isset($this->columnUsed['upload']) && ($uses = $this->columnUsed['upload'])) { if (isset($uses['expression'])) { $this->expressions = ArrayHelper::merge($this->expressions, $uses['expression']); unset($this->columnUsed['expression']); } } $this->used = ArrayHelper::merge($this->used, $uses); } else { if ($_next['class'] === 'omgdef\\multilingual\\MultilingualBehavior') { $this->isMultilingual = true; $this->languageField = $_next['languageField']; $this->translateAttribute = $_next['attributes']; $this->relationField = $_next['langForeignKey']; $class = $_next['langClassName'] . ($_next['dynamicLangClass'] ? isset($_next['langClassSuffix ']) ? $_next['langClassSuffix '] : '' : ''); $this->relationClass = $class; if (class_exists($class)) { /** @var ActiveRecord $class */ $class = new $class(); $this->translateTable = $class::tableName(); } else { $this->translateTable = $_next['tableName']; } $this->excludeField($class, 'translations.'); } } } } } } $this->columnUsed = ArrayHelper::merge($this->columnUsed, $this->widgetsUseClass); }