Example #1
0
    /**
     * @param \yii\db\ActiveRecord $model
     * @param string $relation
     * @param \yii\db\ActiveQuery $activeRelation
     * @param bool|false $multiple
     *
     * @return array
     * @throws Exception
     * @throws InvalidConfigException
     */
    public static function getRelationWidgetOptions($model, $relation, $activeRelation, $multiple = false)
    {
        /** @var \netis\crud\db\ActiveRecord $relModel */
        $relModel = new $activeRelation->modelClass();
        list($createRoute, $searchRoute, $indexRoute) = FormBuilder::getRelationRoutes($model, $relModel, $activeRelation);
        if ($indexRoute === null) {
            return self::getRelationWidgetStaticOptions($model, $relation, $activeRelation, $multiple);
        }
        $isMany = $activeRelation->multiple;
        $foreignKeys = array_values($activeRelation->link);
        $foreignKey = reset($foreignKeys);
        $dbColumns = $model->getTableSchema()->columns;
        $primaryKey = $relModel::primaryKey();
        if (($labelAttributes = $relModel->getBehavior('labels')->attributes) !== null) {
            $fields = array_merge($primaryKey, $labelAttributes);
        } else {
            $fields = $primaryKey;
        }
        $value = self::getRelationValue($model, $relation, $activeRelation);
        $allowClear = $multiple || $isMany ? true : !$model->isAttributeRequired($foreignKey) && (!isset($dbColumns[$foreignKey]) || $dbColumns[$foreignKey]->allowNull);
        $jsPrimaryKey = json_encode($primaryKey);
        $jsSeparator = \netis\crud\crud\Action::COMPOSITE_KEY_SEPARATOR;
        $jsId = <<<JavaScript
function(object){
    var keys = {$jsPrimaryKey}, values = [];
    for (var i = 0; i < keys.length; i++) {
         values.push(object[keys[i]]);
    }
    return netis.implodeEscaped('{$jsSeparator}', values);
}
JavaScript;
        // check if only one option is available and if yes - set it as selected value
        if (!$allowClear && trim($value) === '') {
            $checkedRelations = $relModel->getCheckedRelations(Yii::$app->user->id, $activeRelation->modelClass . '.read');
            $relQuery = $relModel::find()->select($primaryKey)->from($relModel::tableName() . ' t')->authorized($relModel, $checkedRelations, Yii::$app->user->getIdentity())->asArray();
            if ($relQuery->count() === 1) {
                $value = $relQuery->one();
                $value = Action::implodeEscaped(Action::KEYS_SEPARATOR, $value);
            }
        }
        $label = null;
        if ($model instanceof \netis\crud\db\ActiveRecord) {
            $label = $model->getRelationLabel($activeRelation, Html::getAttributeName($relation));
        }
        $ajaxResults = new JsExpression('s2helper.results');
        $clientEvents = null;
        if ($indexRoute !== null && ($searchRoute !== null || $createRoute !== null)) {
            list($ajaxResults, $clientEvents) = self::getRelationAjaxOptions($searchRoute, $createRoute, $jsPrimaryKey, $label, $relation);
        }
        //we get prefix from $relation because it could be in format [3]relation and we need to have [3]foreign_key here
        $relationName = Html::getAttributeName($relation);
        $prefixedFk = str_replace($relation, $foreignKey, $relationName);
        return ['class' => 'maddoger\\widgets\\Select2', 'model' => $model, 'attribute' => $isMany ? $relation : $prefixedFk, 'clientOptions' => array_merge(['formatResult' => new JsExpression('s2helper.formatResult'), 'formatSelection' => new JsExpression('s2helper.formatSelection'), 'id' => new JsExpression($jsId), 'width' => '100%', 'allowClear' => $allowClear, 'closeOnSelect' => true, 'initSelection' => new JsExpression($multiple ? 's2helper.initMulti' : 's2helper.initSingle'), 'ajax' => ['url' => Url::toRoute(array_merge($indexRoute, ['_format' => 'json', 'fields' => implode(',', $fields)])), 'dataFormat' => 'json', 'quietMillis' => 300, 'data' => new JsExpression('s2helper.data'), 'results' => $ajaxResults]], $multiple ? ['multiple' => true] : []), 'clientEvents' => $clientEvents, 'options' => ['class' => 'select2', 'value' => $value, 'placeholder' => self::getPrompt(), 'data-relation-pk' => count($primaryKey) === 1 ? reset($primaryKey) : null]];
    }
Example #2
0
"
               role="tab" data-toggle="tab">
                <?php 
    echo $model->getRelationLabel($data['dataProvider']->query, $relationName);
    ?>
            </a>
        </li>
<?php 
}
?>
    </ul>
    <div class="tab-content">
<?php 
foreach ($relations as $relationName => $data) {
    echo Html::beginTag('div', ['role' => 'tabpanel', 'id' => 'tab_' . $relationName, 'class' => 'tab-pane fade' . ($relationName === $activeRelation ? ' in active' : '')]);
    if (!isset($renderKeyInputs) || $renderKeyInputs) {
        /** @var \yii\db\ActiveRecord $relationModel */
        $relationModel = $data['model'];
        /** @var \yii\db\ActiveQuery $query */
        $query = clone $data['dataProvider']->query;
        $keys = Action::implodeEscaped(Action::KEYS_SEPARATOR, array_map('\\netis\\crud\\crud\\Action::exportKey', $query->select($relationModel->getTableSchema()->primaryKey)->asArray()->all()));
        echo Html::activeHiddenInput($model, $relationName . '[add]', ['value' => $keys]);
        echo Html::activeHiddenInput($model, $relationName . '[remove]');
    }
    echo $this->render('_relation_widget', ['model' => $model, 'relations' => $relations, 'relationName' => $relationName, 'isActive' => $relationName === $activeRelation], $this->context);
    echo Html::endTag('div');
}
?>
    </div>
</div>