/**
  * @param string|null $template
  *
  * @return \Illuminate\View\View
  */
 public function render($template = null)
 {
     if (is_null($template)) {
         if (method_exists($this->section, 'getToolbarTemplate')) {
             $template = $this->section->getToolbarTemplate();
         } else {
             $template = 'datasource::section.toolbar';
         }
     }
     return view($template, ['section' => $this->section]);
 }
 /**
  * @param SectionInterface $section
  * @param FieldInterface   $field
  *
  * @return bool
  */
 public function addFieldToSectionTable(SectionInterface $section, FieldInterface $field)
 {
     if (!$field->hasDatabaseColumn()) {
         return true;
     }
     Schema::table($section->getSectionTableName(), function ($table) use($field) {
         $field->onDatabaseCreate($table);
         $field->setDatabaseFieldType($table);
     });
     return true;
 }
 /**
  * @param bool|array|null $fields
  * @param array           $orderRules
  * @param array           $filterRules
  *
  * @return Builder
  */
 protected function buildQueryForWidget($fields = true, array $orderRules = [], array $filterRules = [])
 {
     $query = $this->newQuery();
     $t = [$this->section->getId() => true];
     $selectFields = [];
     if (is_array($fields)) {
         foreach ($fields as $fieldKey) {
             if ($this->hasField($fieldKey)) {
                 continue;
             }
             $selectFields[] = $this->getFieldsCollection()->getByKey($fieldKey);
         }
     } elseif ($fields === true) {
         $selectFields = $this->getFieldsCollection();
     } elseif ($fields === false) {
         $query->selectRaw('COUNT(*) as total_docs');
     }
     // TODO: предусмотреть relation поля
     if ($fields !== false) {
         foreach ($selectFields as $field) {
             $field->querySelectColumn($query, $this);
         }
     }
     if (!empty($orderRules)) {
         $this->buildQueryOrdering($query, $orderRules, $t);
     }
     if (!empty($filterRules)) {
         $this->buildQueryFilters($query, $filterRules, $t);
     }
     return $query;
 }
 /**
  * @param string|null $template
  *
  * @return \Illuminate\View\View
  */
 public function render($template = null)
 {
     if (is_null($template)) {
         if (method_exists($this->section, 'getHeadlineTemplate')) {
             $template = $this->section->getHeadlineTemplate();
         } else {
             $template = $this->template;
         }
     }
     return view($template, ['fieldParams' => $this->getHeadlineFields(), 'section' => $this->section]);
 }
 /**
  * @param DocumentInterface     $document
  * @param SectionInterface|null $relatedSection
  * @param FieldInterface|null   $relatedField
  *
  * @return HasManyRelation
  */
 public function getDocumentRelation(DocumentInterface $document, SectionInterface $relatedSection = null, FieldInterface $relatedField = null)
 {
     $instance = $relatedSection->getEmptyDocument()->newQuery();
     $foreignKey = $this->getRelatedField()->getDBKey();
     $localKey = $relatedSection->getDocumentPrimaryKey();
     return new HasManyRelation($instance, $document, $foreignKey, $localKey);
 }
 /**
  * @param DocumentInterface     $document
  * @param SectionInterface|null $relatedSection
  * @param FieldInterface|null   $relatedField
  *
  * @return BelongsToRelation
  */
 public function getDocumentRelation(DocumentInterface $document, SectionInterface $relatedSection = null, FieldInterface $relatedField = null)
 {
     return new BelongsToRelation($relatedSection->getEmptyDocument()->newQuery(), $document, $this->getRelatedField()->getRelatedDBKey(), $this->getSection()->getDocumentPrimaryKey(), $this->getRelationName());
 }
 /**
  * @return array
  */
 public function getAliases()
 {
     return [route('backend.datasource.edit', [$this->section->getId()]), route('backend.datasource.field.create', [$this->section->getId()]), route('backend.datasource.document.create', [$this->section->getId()]), route('backend.datasource.document.edit', [$this->section->getId(), '*'])];
 }
 /**
  * @param SectionInterface $section
  * @param FieldInterface   $field
  */
 public function addNewField(SectionInterface $section, FieldInterface $field)
 {
     if ($field = $section->fields()->save($field)) {
         FieldManager::addFieldToSectionTable($section, $field);
     }
 }
 /**
  * @param DocumentInterface   $document
  * @param SectionInterface    $relatedSection
  * @param FieldInterface|null $relatedField
  *
  * @return \Illuminate\Database\Eloquent\Relations\ManyToMany
  */
 public function getDocumentRelation(DocumentInterface $document, SectionInterface $relatedSection = null, FieldInterface $relatedField = null)
 {
     $relatedDocument = $relatedSection->getEmptyDocument();
     $builder = $relatedDocument->newQuery();
     return new BelongsToManyRelation($builder, $document, $this->getRelatedTable(), $this->getDBKey(), $relatedField->getDBKey(), $this->getRelationName());
 }