/** * @throws ScaffoldActionException * @return CmfDbModel */ protected function loadModel() { $config = CmfConfig::getInstance(); $baseDbModelClass = $config->base_db_model_class(); $className = str_replace($config->scaffold_config_class_suffix(), call_user_func([$baseDbModelClass, 'getModelClassSuffix']), get_class($this)); return call_user_func([$baseDbModelClass, 'getModelByClassName'], $className); }
/** * Handle an incoming request. * * @param Request $request * @param \Closure $next * @return mixed */ public function handle(Request $request, Closure $next) { //get the admin check closure that should be supplied in the config /** @var CmfConfig $configs */ $configs = CmfConfig::getInstance(); //if this is a simple false value, send the user to the login redirect $response = \Auth::guard()->check() && $configs->isAuthorised($request); if (!$response) { $loginUrl = route($configs->login_route()); $redirectUri = $request->url(); if ($request->ajax()) { \Session::set(CmfConfig::getInstance()->session_redirect_key(), $redirectUri); return response()->json(['redirect_with_reload' => $loginUrl], HttpCode::UNAUTHORISED); } else { return redirect()->guest($loginUrl)->with(CmfConfig::getInstance()->session_redirect_key(), $redirectUri); } } else { if (is_a($response, 'Illuminate\\Http\\JsonResponse') || is_a($response, 'Illuminate\\Http\\Response')) { return $response; } else { if (is_a($response, 'Illuminate\\Http\\RedirectResponse')) { $redirectUri = $request->url(); /** @var RedirectResponse $response */ return $response->with(CmfConfig::getInstance()->session_redirect_key(), $redirectUri); } } } return $next($request); }
/** * @return callable|null */ public function getValueConverter() { if (empty(parent::getValueConverter())) { switch ($this->getType()) { case self::TYPE_BOOL: $this->setValueConverter(function ($value) { return CmfConfig::transBase('.item_details.field.bool.' . ($value ? 'yes' : 'no')); }); break; case self::TYPE_IMAGE: $this->setValueConverter(function ($value, DbColumnConfig $columnConfig, array $record) { if (!empty($value) && is_array($value) && !empty($value['url']) && is_array($value['url'])) { if (count($value['url']) > 0) { unset($value['url']['source']); } $images = []; $translationPath = CmfConfig::getInstance()->custom_dictionary_name() . '.' . $columnConfig->getDbTableConfig()->getName() . '.item_details.field.' . $columnConfig->getName() . '_version.'; foreach ($value['url'] as $key => $url) { $images[] = ['label' => trans($translationPath . $key), 'url' => $url]; } return $images; } else { return []; } }); break; } } return parent::getValueConverter(); }
public function __construct(CmfDbModel $model, ScaffoldSectionConfig $scaffoldSectionConfig) { parent::__construct($model, $scaffoldSectionConfig); $this->limit = CmfConfig::getInstance()->rows_per_page(); if ($model->getOrderField()) { $this->setOrderBy($model->getOrderField(), $model->getOrderDirection()); } }
/** * @param string $type * @return $this */ public function setType($type) { parent::setType($type); switch ($this->type) { case self::TYPE_BOOL: $this->setValueConverter(function ($value) { return CmfConfig::transBase('.datagrid.field.bool.' . ($value ? 'yes' : 'no')); }); break; } return $this; }
/** * Handle an incoming request. * * @param Request $request * @param \Closure $next * @return mixed */ public function handle(Request $request, Closure $next) { $tableName = $request->route()->parameter('table_name'); if (!empty($tableName)) { try { /** @var CmfDbModel $model */ $model = call_user_func([CmfConfig::getInstance()->base_db_model_class(), 'getModelByTableName'], $tableName); CmfScaffoldApiController::setModel($model); } catch (DbUtilsException $exc) { dpr($exc->getMessage()); } } // if the model doesn't exist at all, redirect to 404 if (empty($model)) { abort(404, 'Page not found'); } return $next($request); }
/** * Handle an incoming request. * * @param Request $request * @param \Closure $next * @return mixed */ public function handle(Request $request, Closure $next) { if (!$request->ajax()) { abort(HttpCode::FORBIDDEN, 'Only ajax requests'); } try { return $next($request); } catch (DbObjectValidationException $exc) { return new JsonResponse(['_message' => trans(CmfConfig::transBase('.error.invalid_data_received')), 'errors' => $exc->getValidationErrors()], HttpCode::INVALID); } catch (HttpException $exc) { if ($exc->getStatusCode() === HttpCode::INVALID) { $data = json_decode($exc->getMessage(), true); if (!empty($data) && !empty($data['_message'])) { return new JsonResponse(['_message' => $data['_message'], 'errors' => empty($data['errors']) ? [] : $data['errors']], HttpCode::INVALID); } } throw $exc; } }
protected function convertSomeExceptionsToJsonResponses(\Exception $exc) { switch (get_class($exc)) { case DbObjectValidationException::class: /** @var DbObjectValidationException $exc */ return new JsonResponse(['_message' => CmfConfig::transBase('.error.invalid_data_received'), 'errors' => $exc->getValidationErrors()], HttpCode::INVALID); case NotFoundHttpException::class: return new JsonResponse(['_message' => CmfConfig::transBase('.error.http404')], HttpCode::NOT_FOUND); case HttpException::class: /** @var HttpException $exc */ $data = json_decode($exc->getMessage(), true); if ($data === false) { $data = ['_message' => $exc->getMessage()]; } return new JsonResponse($data, $exc->getStatusCode()); default: return $this->defaultConvertExceptionToResponse($exc); } }
/** * Request must be done via ajax * You can specify a fallback url OR 'route' with optional 'params' via 'fallback' key in route config: * Example: * Route::get('forgot_password/{param}', [ * 'middleware' => AjaxOnly::class, * 'fallback' => '/some/url' * // or * 'fallback' => '/some/url/{param}' * // or * 'fallback' => [ * 'route' => 'cmf_login', * 'params' => [] //< optional, can be array or boolean (by default === true: pass params from original url) * ], * ... * ] * If 'params' === true - all params retrieved from original URL will be passed to fallback route * If 'params' === false - all params retrieved from original URL will be passed to fallback route * * @param Request $request * @param \Closure $next * @return mixed * @throws \InvalidArgumentException */ public function handle(Request $request, Closure $next) { if (!$request->ajax()) { // maybe there is a fallback? $fallback = array_get($request->route()->getAction(), 'fallback', []); if (!empty($fallback) && is_string($fallback)) { return new RedirectResponse(StringUtils::insert($fallback, $request->route()->parameters(), ['before' => '{', 'after' => '}'])); } else { if (!empty($fallback['route'])) { $params = array_get($fallback, 'params', true); if ($params === true) { $params = $request->route()->parameters(); } else { if ($params instanceof \Closure) { $params = call_user_func($params, $request->route()->parameters()); } else { if ($params === false || !is_array($params)) { $params = []; } } } return new RedirectResponse(route($fallback['route'], $params)); } else { abort(HttpCode::FORBIDDEN, 'Only ajax requests'); } } } try { return $next($request); } catch (DbObjectValidationException $exc) { return new JsonResponse(['_message' => trans(CmfConfig::transBase('.error.invalid_data_received')), 'errors' => $exc->getValidationErrors()], HttpCode::INVALID); } catch (HttpException $exc) { if ($exc->getStatusCode() === HttpCode::INVALID) { $data = json_decode($exc->getMessage(), true); if (!empty($data) && !empty($data['_message'])) { return new JsonResponse(['_message' => $data['_message'], 'errors' => empty($data['errors']) ? [] : $data['errors']], HttpCode::INVALID); } } throw $exc; } }
protected function preapareAndGetDataForViews() { $tableName = $this->argument('table_name'); $modelClass = call_user_func([$this->modelParentClass, 'getFullModelClassByTableName'], $tableName); $folder = $this->getFolderAndValidate($tableName, $modelClass); if (empty($folder)) { return false; } $tableSchema = $this->getDbSchema(); $columns = $this->getColumns($tableName, $tableSchema); if (empty($columns)) { return false; } $dataForViews = ['folder' => $folder, 'table' => $tableName, 'schema' => $tableSchema, 'columns' => $columns, 'modelParentClass' => $this->modelParentClass, 'objectParentClass' => $this->objectParentClass, 'tableConfigParentClass' => $this->tableConfigParentClass, 'scaffoldConfigParentClass' => $this->scaffoldConfigParentClass, 'modelClassName' => call_user_func([$this->modelParentClass, 'getModelNameByTableName'], $tableName), 'objectClassName' => call_user_func([$this->modelParentClass, 'getObjectNameByTableName'], $tableName), 'tableConfigClassName' => call_user_func([$this->modelParentClass, 'getTableConfigNameByTableName'], $tableName), 'scaffoldConfigClassName' => CmfConfig::getInstance()->getScaffoldConfigNameByTableName($tableName), 'traitsForTableConfig' => $this->getTraitsForTableConfig()]; $dataForViews['modelAlias'] = $dataForViews['objectClassName']; $dataForViews['namespace'] = call_user_func([$this->modelParentClass, 'getRootNamespace']) . '\\' . $dataForViews['objectClassName']; $dataForViews['files']['model'] = $folder . DIRECTORY_SEPARATOR . $dataForViews['modelClassName'] . '.php'; $dataForViews['files']['object'] = $folder . DIRECTORY_SEPARATOR . $dataForViews['objectClassName'] . '.php'; $dataForViews['files']['table_config'] = $folder . DIRECTORY_SEPARATOR . $dataForViews['tableConfigClassName'] . '.php'; $dataForViews['files']['scaffold_config'] = $folder . DIRECTORY_SEPARATOR . $dataForViews['scaffoldConfigClassName'] . '.php'; Folder::load($folder, true, 0775); return $dataForViews; }
/** * @param CmfDbModel $model * @param null|string $message * @return $this */ protected function sendItemNotFoundResponse(CmfDbModel $model, $message = null) { if (empty($message)) { $message = CmfConfig::transBase('.error.resource_item_not_found'); } return cmfJsonResponseForHttp404(route('cmf_items_table', [$this->getTableNameForRoutes()]), $message); }
<?php /** * @var \PeskyCMF\Scaffold\Form\InputRendererConfig $rendererConfig * @var \PeskyCMF\Scaffold\ItemDetails\ItemDetailsConfig $actionConfig * @var \PeskyCMF\Scaffold\ItemDetails\ItemDetailsFieldConfig $fieldConfig * @var array|null $options * @var \PeskyCMF\Db\CmfDbModel $model * @var string $translationPrefix */ if (empty($options)) { $options = []; } $yes = empty($options['yes']) ? \PeskyCMF\Config\CmfConfig::transBase('.item_details.field.bool.yes') : $options['yes']; $no = empty($options['no']) ? \PeskyCMF\Config\CmfConfig::transBase('.item_details.field.bool.no') : $options['no']; echo '{{? !!it.' . $fieldConfig->getName() . ' }}' . $yes . '{{??}}' . $no . '{{?}}';
<?php /** * @var \PeskyCMF\Db\CmfDbModel $model * @var \PeskyCMF\Scaffold\DataGrid\DataGridConfig $dataGridConfig * @var \PeskyCMF\Scaffold\DataGrid\DataGridFilterConfig $dataGridFilterConfig * @var \PeskyCMF\Scaffold\ItemDetails\ItemDetailsConfig $itemDetailsConfig * @var \PeskyCMF\Scaffold\Form\FormConfig $formConfig */ $data = ['model' => $model, 'translationPrefix' => \PeskyCMF\Config\CmfConfig::getInstance()->custom_dictionary_name() . ".{$model->getTableName()}", 'idSuffix' => preg_replace('%[^a-z0-9]%is', '-', strtolower($model->getTableName()))]; ?> <?php echo view($dataGridConfig->getView(), $data + $dataGridConfig->getAdditionalDataForView(), ['dataGridConfig' => $dataGridConfig, 'dataGridFilterConfig' => $dataGridFilterConfig])->render(); ?> <?php echo view($formConfig->getView(), $data + $formConfig->getAdditionalDataForView(), ['formConfig' => $formConfig])->render(); ?> <?php echo view($itemDetailsConfig->getView(), $data + $itemDetailsConfig->getAdditionalDataForView(), ['itemDetailsConfig' => $itemDetailsConfig])->render(); ?>
protected function getValidationErrorsResponseMessage() { return CmfConfig::transBase('.error.invalid_data_received'); }
<form role="form" method="post" action="{{ route('cmf_profile', [], false) }}" id="admin-profile-form"> <input type="hidden" name="_method" value="PUT"> <!-- disable chrome email & password autofill --> <input type="email" class="hidden"> <input type="text" class="hidden"> <input type="password" class="hidden"> <!-- end of autofill disabler --> <div class="box-body"> @if ($admin->_hasField('email')) <div class="form-group"> <label for="email-input">{{ \PeskyCMF\Config\CmfConfig::transCustom('.page.profile.input.email') }}*</label> <input class="form-control" value="{{ $admin->email }}" name="email" id="email-input" type="email" required="required"> </div> @endif <?php $loginColumn = \PeskyCMF\Config\CmfConfig::getInstance()->user_login_column(); ?> @if ($loginColumn !== 'email') <div class="form-group"> <label for="{{ $loginColumn }}-input">{{ \PeskyCMF\Config\CmfConfig::transCustom('.page.profile.input.' . $loginColumn) }}*</label> <input class="form-control" value="{{ $admin->$loginColumn }}" name="{{ $loginColumn }}" id="{{ $loginColumn }}-input" type="text" required="required"> </div> @endif <div class="form-group"> <label for="new-password-input">{{ \PeskyCMF\Config\CmfConfig::transCustom('.page.profile.input.new_password') }}</label> <input class="form-control" value="" name="new_password" id="new-password-input" type="password" autocomplete="off"> </div> @if ($admin->_hasField('name')) <div class="form-group"> <label for="name-input">{{ \PeskyCMF\Config\CmfConfig::transCustom('.page.profile.input.name') }}</label> <input class="form-control" value="{{ $admin->name }}" name="name" id="name-input" type="text">
public static function getScaffoldConfigClassSuffix() { /** @var CmfDbModel $calledClass */ $calledClass = get_called_class(); return CmfConfig::getInstance()->scaffold_config_class_suffix(); }
/** * @return DataGridFieldConfig */ protected function getDataGridFieldConfigForRowActions() { return DataGridFieldConfig::create()->setIsDbField(false)->setName(static::ROW_ACTIONS_COLUMN_NAME)->setLabel(CmfConfig::transBase('.datagrid.actions.column_label'))->setType(DataGridFieldConfig::TYPE_STRING); }
<?php $dictionary = \PeskyCMF\Config\CmfConfig::getInstance()->custom_dictionary_name(); ?> <div class="login-box"> <div class="login-logo"> {!! \PeskyCMF\Config\CmfConfig::getInstance()->login_logo() !!} <b>{!! trans("{$dictionary}.forgot_password.header") !!}</b> </div> <div class="login-box-body" id="forgot-password-form-container"> <form action="{{ route('cmf_forgot_password', [], false) }}" method="post" id="forgot-password-form"> <div class="form-group has-feedback"> <input type="email" name="email" required class="form-control" placeholder="{{ trans("{$dictionary}.forgot_password.email_label") }}"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> <div class="login-submit text-right"> <button type="submit" class="btn btn-primary btn-flat"> {{ trans("{$dictionary}.forgot_password.button_label") }} </button> </div> </form> </div> </div>
<?php echo \PeskyCMF\Config\CmfConfig::transBase('.form.toolbar.delete'); ?> </a> {{?}} <?php } ?> <?php echo $endIf; ?> </div> <div class="col-xs-3 text-right"> <button type="submit" class="btn btn-success"> <?php echo \PeskyCMF\Config\CmfConfig::transBase('.form.toolbar.submit'); ?> </button> </div> </div> <?php $toolbarItems = $formConfig->getToolbarItems(); ?> <?php if (count($toolbarItems) > 0) { ?> <div class="mt10 text-center"> <?php foreach ($toolbarItems as $toolbarItem) { echo ' ' . preg_replace('%(:|\\%3A)([a-zA-Z0-9_]+)\\1%is', '{{= it.$2 }}', $toolbarItem) . ' '; }
protected function getUserLoginField() { return CmfConfig::getInstance()->user_login_column(); }
<?php $dictionary = \PeskyCMF\Config\CmfConfig::getInstance()->custom_dictionary_name(); $loginInputName = \PeskyCMF\Config\CmfConfig::getInstance()->user_login_column(); ?> <div class="login-box"> <div class="login-logo"> {!! \PeskyCMF\Config\CmfConfig::getInstance()->login_logo() !!} <b>{!! trans("{$dictionary}.login_form.header") !!}</b> </div> <div class="login-box-body" id="login-form-container"> <form action="{{ route(\PeskyCMF\Config\CmfConfig::getInstance()->login_route(), [], false) }}" method="post" id="login-form"> <div class="form-group has-feedback"> <input type="{{ $loginInputName === 'email' ? 'email' : 'text' }}" name="{{ $loginInputName }}" required class="form-control" placeholder="{{ trans("{$dictionary}.login_form.{$loginInputName}_label") }}"> <span class="glyphicon glyphicon-{{ $loginInputName === 'email' ? 'envelope' : 'user' }} form-control-feedback"></span> </div> <div class="form-group has-feedback"> <input type="password" name="password" required class="form-control" placeholder="{{ trans("{$dictionary}.login_form.password_label") }}"> <span class="glyphicon glyphicon-lock form-control-feedback"></span> </div> <div class="row login-submit"> <div class="col-xs-8 forgot-password"> @if (\PeskyCMF\Config\CmfConfig::getInstance()->is_password_restore_allowed()) <a href="{{ route('cmf_forgot_password') }}">{{ trans("{$dictionary}.login_form.forgot_password_label") }}</a> @endif </div> <div class="col-xs-4"> <button type="submit" class="btn btn-primary btn-block btn-flat"> {{ trans("{$dictionary}.login_form.button_label") }}
</section> </aside> <header class="main-header"> <a href="{{ route('cmf_start_page', [], false, '/') }}" class="logo"> <span class="logo-lg"> {!! \PeskyCMF\Config\CmfConfig::getInstance()->sidebar_logo() !!} </span> </a> </header> <div class="content-wrapper" id="section-content"> </div> @include(\PeskyCMF\Config\CmfConfig::getInstance()->footer_view()) <script type="application/javascript"> GlobalVars.setLocalizationStrings(<?php echo json_encode(\PeskyCMF\Config\CmfConfig::transBase('.ui.js_component'), JSON_UNESCAPED_UNICODE); ?> ); $(document).ready(function () { $.AdminLTE.tree('.sidebar'); setTimeout(function () { // without timeout it works not correctly $.AdminLTE.layout.fix(); $.AdminLTE.layout.fixSidebar(); }, 1); }) </script>
/** * Abort with HTTP code 404 */ protected function sendRecordNotFoundResponse() { abort(HttpCode::NOT_FOUND, CmfConfig::transBase('.error.db_record_not_exists')); }
public static function sendItemNotFoundResponse(CmfDbModel $model) { return response()->json(['_message' => CmfConfig::transBase('.error.resource_item_not_found'), 'redirect' => 'back', 'redirect_fallback' => route('cmf_items_table', ['table_name' => $model->getTableName()])], 404); }
function __cmf_scaffold_api_controller_class() { return \PeskyCMF\Config\CmfConfig::getInstance()->cmf_scaffold_api_controller_class(); }
<div id="user-panel"> <div class="info"> </div> <div class="actions"> <a href="{{ route('cmf_profile', [], false) }}"> <i class="fa fa-fw fa-user"></i>{{ \PeskyCMF\Config\CmfConfig::transCustom('.user.profile_label') }} </a> <a href="{{ route(\PeskyCMF\Config\CmfConfig::getInstance()->logout_route(), [], false) }}"> <i class="fa fa-fw fa-sign-out"></i>{{ \PeskyCMF\Config\CmfConfig::transCustom('.user.logout_label') }} </a> </div> </div> <script type="text/html" id="user-panel-tpl"> <div class="user-name">@{{? it.name.length }}@{{= it.name }}@{{??}}@{{= it.role }}@{{?}}</div> <div class="user-{{ \PeskyCMF\Config\CmfConfig::getInstance()->user_login_column() }}"> <?php echo '{{= it.' . \PeskyCMF\Config\CmfConfig::getInstance()->user_login_column() . ' }}'; ?> </div> </script>
public function getAdminInfo() { $admin = $this->getAdmin(); $adminData = $admin->toPublicArray(); if (!empty($adminData['role'])) { $adminData['_role'] = $admin->role; $role = $admin->is_superadmin ? 'superadmin' : $admin->role; $adminData['role'] = CmfConfig::transCustom('.admins.role.' . $role); } return response()->json($adminData); }
protected function loadCmfRoutes() { foreach ($this->cmfConfig->cmf_routes_cofig_files() as $filePath) { require_once $filePath; } }
/** * @return callable|null */ public function getValueConverter() { if (empty($this->valueConverter)) { switch ($this->getType()) { case self::TYPE_BOOL: return function ($value) { if (!$this->isDbField()) { if (!array_has($value, $this->getName())) { return '-'; } else { $value = (bool) $value[$this->getName()]; } } return CmfConfig::transBase('.datagrid.field.bool.' . ($value ? 'yes' : 'no')); }; } } return $this->valueConverter; }
$filterConfig->setFilterLabel(trans($path)); } $fitlers[] = $filterConfig->buildConfig(); } ?> var queryBuilderConfig = { filters: <?php echo json_encode($fitlers, JSON_UNESCAPED_UNICODE); ?> , is_opened: <?php echo $dataGridConfig->isFilterShownByDefault() ? 'true' : 'false'; ?> }; DataGridSearchHelper.locale = <?php echo json_encode(\PeskyCMF\Config\CmfConfig::transBase('.datagrid.toolbar.filter'), JSON_UNESCAPED_UNICODE); ?> ; <?php if ($dataGridConfig->hasJsInitiator()) { ?> <?php echo $dataGridConfig->getJsInitiator(); ?> .call($('#<?php echo $dataGridId; ?> ')); <?php }