Beispiel #1
0
 public function resolve($value)
 {
     $table = $this->router->resolved('table');
     $this->tables->setTable($table->table);
     $this->tables->setRecordClass(DatabaseRecord::class);
     if ($table->repository) {
         $this->tables->setRepository(context()->get(Repository::class . '.' . $table->repository));
     }
     $this->dynamic->joinTranslationsIfTranslatable($this->tables);
     $this->dynamic->joinPermissionsIfPermissionable($this->tables);
     return $this->tables->where('id', $value)->oneOrFail(function () {
         $this->response->unauthorized('Record not found');
     });
 }
Beispiel #2
0
 public function resolve($value)
 {
     $this->dynamic->joinTranslationsIfTranslatable($this->tables);
     $this->dynamic->joinPermissionsIfPermissionable($this->tables);
     return $this->tables->where('id', $value)->withFields(function (HasMany $fields) {
         $fields->joinTranslation();
         $fields->joinFallbackTranslation();
         $fields->withFieldType();
         $fields->withSettings();
     })->withTabs(function (HasMany $tabs) {
         $tabs->joinTranslation();
         $tabs->joinFallbackTranslation();
     })->oneOrFail(function () {
         $this->response->unauthorized('Table not found');
     });
 }
Beispiel #3
0
 public function getExportTableAction(Table $table, Strategy $strategy, Dynamic $dynamicService)
 {
     $entity = $table->createEntity();
     $this->dynamic->setTable($table);
     $this->dynamic->applyOnEntity($entity, false);
     $dynamicService->joinTranslationsIfTranslatable($entity);
     $dynamicService->joinPermissionsIfPermissionable($entity);
     /**
      * Get all relations for fields with type (select).
      */
     $relations = (new Relations())->where('on_table_id', $table->id)->where('dynamic_relation_type_id', 1)->all();
     foreach ($relations as $relation) {
         /**
          * Right table entity is created here.
          */
         $relationEntity = $relation->showTable->createEntity();
         /**
          * We need to add relations to select.
          * $tableRecord is for example users.
          * So entity is entity with table users.
          * We will fetch all users and related user_group_id and language_id
          * as user.relation_user_group_id and user.relation_language_id.
          */
         $entity->with((new BelongsTo($entity, $relationEntity))->foreignKey($relation->onField->field)->fill('relation_' . $relation->onField->field)->after(function ($record) use($relation) {
             $record->setRelation('select_relation_' . $relation->onField->field, $relation);
         }));
     }
     $strategy->input($entity);
     /**
      * @T00D00 - hackish ...
      */
     $tabelize = (new Tabelize($entity))->setRecords($entity->all())->setEntity($entity)->setFields($table->listableFields->reduce(function (Field $field) use($table) {
         $fields = $_SESSION['pckg']['dynamic']['view']['table_' . $table->id]['view']['fields'] ?? [];
         return !$fields || in_array($field->field, $fields);
     }));
     $tabelize->setDataOnly();
     $strategy->setData($tabelize->transformRecords());
     $strategy->setFileName($table->table . '-' . date('Ymd-his'));
     $strategy->prepare();
     $strategy->output();
     $this->response()->respond();
 }
Beispiel #4
0
 /**
  * List table records.
  *
  * @param Table  $tableRecord
  * @param Entity $entity
  *
  * @return $this
  */
 public function getViewTableAction(Table $tableRecord, DynamicService $dynamicService, DatabaseEntity $entity = null, $viewType = 'full')
 {
     /**
      * Set table.
      */
     $this->dynamic->setTable($tableRecord);
     if (!$entity) {
         $entity = $tableRecord->createEntity();
         $dir = path('app_src') . implode(path('ds'), array_slice(explode('\\', get_class($entity)), 0, -2)) . path('ds') . 'View' . path('ds');
         Twig::addDir($dir);
         /**
          * This is needed for table actions.
          */
         Twig::addDir($dir . 'tabelize' . path('ds') . 'recordActions' . path('ds'));
         Twig::addDir($dir . 'tabelize' . path('ds') . 'entityActions' . path('ds'));
     }
     /**
      * Apply entity extension.
      */
     $this->dynamic->applyOnEntity($entity);
     /**
      * This is used for URLs.
      */
     $entity->setStaticDynamicTable($tableRecord);
     /**
      * Join extensions.
      */
     $dynamicService->joinTranslationsIfTranslatable($entity);
     $dynamicService->joinPermissionsIfPermissionable($entity);
     $dynamicService->removeDeletedIfDeletable($entity);
     /**
      * Get all relations for fields with type (select).
      */
     $relations = (new Relations())->where('on_table_id', $tableRecord->id)->where('dynamic_relation_type_id', 1)->all();
     foreach ($relations as $relation) {
         /**
          * Right table entity is created here.
          */
         $relationEntity = $relation->showTable->createEntity();
         $dynamicService->joinTranslationsIfTranslatable($relationEntity);
         /**
          * We need to add relations to select.
          * $tableRecord is for example users.
          * So entity is entity with table users.
          * We will fetch all users and related user_group_id and language_id
          * as user.relation_user_group_id and user.relation_language_id.
          */
         $entity->with((new BelongsTo($entity, $relationEntity))->foreignKey($relation->onField->field)->fill('relation_' . $relation->onField->field)->after(function ($record) use($relation) {
             $record->setRelation('select_relation_' . $relation->onField->field, $relation);
         }));
     }
     /**
      * Filter records by $_GET['search']
      */
     $this->dynamic->getFilterService()->filterByGet($entity);
     $groups = $this->dynamic->getGroupService()->getAppliedGroups();
     $records = $entity->count()->all();
     $total = $records->total();
     foreach ($groups as $group) {
         $records = $records->groupBy($group['field']);
     }
     $fieldTransformations = ['tabelizeClass'];
     if (get_class($entity) == Orders::class) {
         $fieldTransformations['bills_payed'] = function (Order $order) {
             return $order->getPayedBillsSum();
         };
     }
     /**
      * Transform field type = php
      */
     $tableRecord->listableFields(function (HasMany $relation) {
         $relation->withFieldType();
     })->each(function (Field $field) use(&$fieldTransformations) {
         if ($field->fieldType->slug == 'php') {
             $fieldTransformations[$field->field] = function ($record) use($field) {
                 return $record->{$field->field};
             };
         }
     });
     $tabelize = $this->tabelize()->setTable($tableRecord)->setTitle($tableRecord->getListTitle())->setEntity($entity)->setRecords($records)->setFields($tableRecord->listableFields(function (HasMany $relation) {
         $relation->withFieldType();
         $relation->joinTranslations();
     })->reduce(function (Field $field) use($tableRecord) {
         $fields = $_SESSION['pckg']['dynamic']['view']['table_' . $tableRecord->id]['view']['fields'] ?? [];
         return !$fields && $field->visible || in_array($field->field, $fields);
     }))->setPerPage(50)->setPage(1)->setTotal($total)->setGroups($groups ? range(1, count($groups)) : [])->setEntityActions($tableRecord->getEntityActions())->setRecordActions($tableRecord->getRecordActions())->setViews($tableRecord->actions()->keyBy('slug'))->setFieldTransformations($fieldTransformations);
     if ($this->request()->isAjax() && (strpos($_SERVER['REQUEST_URI'], '/tab/') === false || post('search'))) {
         return ['records' => $tabelize->transformRecords(), 'groups' => $groups];
     }
     $tabelize->getView()->addData('dynamic', $this->dynamic);
     $tabelize->getView()->addData('viewType', $viewType);
     $tabelize->getView()->addData('searchUrl', router()->getUri());
     return $tabelize;
 }