/** * @param CmfDbModel $model * @param string $relationAlias * @param array $scannedModels * @param int $depth * @return bool|\PeskyCMF\Db\CmfDbModel * @throws \PeskyORM\Exception\DbUtilsException * @throws \PeskyORM\Exception\DbTableConfigException * @throws ScaffoldException * @throws \PeskyORM\Exception\DbModelException */ protected function findRelatedModel(CmfDbModel $model, $relationAlias, array &$scannedModels = [], $depth = 0) { if ($model->hasTableRelation($relationAlias)) { return $model->getRelatedModel($relationAlias); } $scannedModels[] = $model->getTableName(); foreach ($model->getTableRealtaions() as $alias => $relationConfig) { /** @var CmfDbModel $relModel */ $relModel = $model->getRelatedModel($alias); if (!empty($scannedModels[$relModel->getTableName()])) { continue; } $modelFound = $this->findRelatedModel($relModel, $relationAlias, $scannedModels, $depth + 1); if ($modelFound) { return $modelFound; } $scannedModels[] = $relModel->getTableName(); } if (!$depth === 0 && empty($modelFound)) { throw new ScaffoldException("Cannot find relation [{$relationAlias}] in model [{$model->getAlias()}] or among its relations"); } return false; }
/** * @param Request $request * @param CmfDbModel $model * @param string $inputNamePrefix - input name prefix * For example if you use '_ids' instead of 'ids' - use prefix '_' * @return array|Response * @throws \PeskyCMF\Scaffold\ScaffoldException * @throws \PeskyCMF\PeskyCmfException * @throws \PeskyORM\Exception\DbQueryException * @throws \PeskyORM\Exception\DbException * @throws \PeskyORM\Exception\DbTableConfigException * @throws \PeskyORM\Exception\DbUtilsException * @throws \PeskyORM\Exception\DbModelException */ private function getConditionsForBulkActions(Request $request, CmfDbModel $model, $inputNamePrefix = '') { $specialConditions = $this->getScaffoldConfig()->getFormConfig()->getSpecialConditions(); $conditions = $specialConditions; $idsField = $inputNamePrefix . 'ids'; $conditionsField = $inputNamePrefix . 'conditions'; if ($request->has($idsField)) { $this->validate($request->data(), [$idsField => 'required|array', $idsField . '.*' => 'integer|min:1']); $conditions[$model->getPkColumnName()] = $request->data($idsField); } else { if ($request->has($conditionsField)) { $this->validate($request->data(), [$conditionsField => 'string|regex:%^[\\{\\[].*[\\}\\]]$%s']); $encodedConditions = $request->data($conditionsField) !== '' ? json_decode($request->data($conditionsField), true) : []; if ($encodedConditions === false || !is_array($encodedConditions) || empty($encodedConditions['r'])) { return cmfJsonResponseForValidationErrors([$conditionsField => 'JSON expected']); } if (!empty($encodedConditions)) { $dataGridConfig = $this->getScaffoldConfig()->getDataGridConfig(); $filterConditions = $this->getScaffoldConfig()->getDataGridFilterConfig()->buildConditionsFromSearchRules($encodedConditions); if ($dataGridConfig->hasContains()) { $subQueryConditions = array_merge(['CONTAIN' => $dataGridConfig->getContains()], $filterConditions, $specialConditions); $subQuery = $model->builder()->fromOptions($model->resolveContains($subQueryConditions))->fields(['id'])->buildQuery(DbExpr::create("`{$model->getAlias()}`.`id`"), false, false); $conditions = [DbExpr::create("`{$model->getPkColumnName()}` IN ({$subQuery})")]; } else { $conditions = array_merge($filterConditions, $specialConditions); } } } else { return cmfJsonResponseForValidationErrors([$idsField => 'List of items IDs of filtering conditions expected', $conditionsField => 'List of items IDs of filtering conditions expected']); } } return $conditions; }
/** * Get ScaffoldSectionConfig instance * @param CmfDbModel $model - a model to be used in ScaffoldSectionConfig * @param string $tableName - table name passed via route parameter, may differ from $model->getTableName() * and added here to be used in child configs when you need to use scaffolds with fake table names. * It should be used together with static::getModelByTableName() to provide correct model for a fake table name * @return ScaffoldSectionConfig */ public static function getScaffoldConfig(CmfDbModel $model, $tableName) { // $tableName is no tused by default and added here to be used in child configs $className = $model->getNamespace() . $model->getAlias() . static::scaffold_config_class_suffix(); return new $className($model); }