/** * Add value with id of structure to material field * @param null $materialId * @param null $fieldId * @param null $structureId * @return array */ public function __async_add($materialId = null, $fieldId = null, $structureId = null) { // If passed structure don't exists then create it if (dbQuery('materialfield')->cond('MaterialID', $materialId)->cond('FieldID', $fieldId)->cond('Value', $structureId)->cond('Active', 1)->count() == 0) { $value = (int) $structureId; // If structure is valid if (!empty($value)) { $mf = new materialfield(false); $mf->Active = 1; $mf->Value = $structureId; $mf->MaterialID = $materialId; $mf->FieldID = $fieldId; $mf->save(); return array('status' => 1); } } return array('status' => 0); }
/** Create new gallery from old one */ public function migrate_23_to_24() { $user = array_shift(db()->fetch('SELECT * from User Limit 1')); // Create field for old gallery $field = new field(false); $field->Name = '_gallery'; $field->Type = 9; $field->local = 0; $field->Description = 'Галерея Материала'; $field->UserID = $user['UserID']; $field->system = 1; $field->Created = date('Y-m-d H:i:s'); $field->Modyfied = $field->Created; $field->save(); /** @var \samson\activerecord\structure $structure Get system material structure */ $structure = null; if (dbQuery('structure')->cond('Url', '__material')->exec($structure)) { $structure = array_shift($structure); } // Create relation between system material structure and gallery field $structureField = new structurefield(false); $structureField->FieldID = $field->FieldID; $structureField->StructureID = $structure->StructureID; $structureField->Active = 1; $structureField->Modified = date('Y-m-d H:i:s'); $structureField->save(); /** @var array $gallery Array of \samson\activerecord\gallery objects */ $gallery = null; if (dbQuery('gallery')->exec($gallery)) { /** @var \samson\activerecord\gallery $image Set gallery as additional field */ foreach ($gallery as $image) { // Create new materialfield for image and save it id in gallery table if (!dbQuery('materialfield')->cond('MaterialID', $image->MaterialID)->cond('FieldID', $field->FieldID)->first($materialField)) { $materialField = new materialfield(false); $materialField->MaterialID = $image->MaterialID; $materialField->FieldID = $field->FieldID; } $materialField->Active = 1; $materialField->save(); if (!dbQuery('structurematerial')->cond('MaterialID', $image->MaterialID)->cond('StructureID', $structure->StructureID)->first($structureMaterial)) { $structureMaterial = new structurematerial(false); $structureMaterial->MaterialID = $image->MaterialID; $structureMaterial->StructureID = $structure->StructureID; $structureMaterial->Modified = date('Y-m-d H:i:s'); } $structureMaterial->Active = 1; $structureMaterial->save(); $image->materialFieldId = $materialField->MaterialFieldID; $image->save(); } } }
/** * Overload default, render fields as SamsonCMS input fields * @param mixed $item Item to render * @return string Rendered collection item block */ public function renderItem($item) { // Iterate all entity fields $fieldsHTML = ''; foreach ($this->fields as $inputField) { // TODO: Maybe we can optimize this requests // Find additional field by Name /** @var \samsoncms\api\MaterialField $field */ $field = null; /**@var \samsoncms\api\Field $field */ if ($this->query->entity(\samsoncms\api\Field::class)->where(\samsoncms\api\Field::F_IDENTIFIER, $inputField->name)->first($field)) { // Create material field query to get additional field record $query = $this->query->entity(\samsoncms\api\MaterialField::class)->where(\samsoncms\api\MaterialField::F_MATERIALID, $item->id)->where(\samsoncms\api\MaterialField::F_FIELDID, $field->id); // If additional field is localizable add locale condition if ($field->local === 1) { $query->where('locale', $this->locale); } // Try to find materialfield record for this item and its field $materialfield = null; if (!$query->first($materialfield)) { // Create record if it does not exists $materialfield = new materialfield(false); $materialfield->MaterialID = $item->id; $materialfield->locale = $this->locale; $materialfield->FieldID = $field->id; $materialfield->Active = 1; $materialfield->save(); } // Render input field view $fieldsHTML .= $inputField->render($this->renderer, $this->query, $materialfield); } else { // Render input field view $fieldsHTML .= $inputField->render($this->renderer, $this->query, $item); } } // Render fields row view return $this->renderer->view($this->rowView)->set($fieldsHTML, 'cols')->output(); }
/** * Overload default, render fields as SamsonCMS input fields * @param mixed $item Item to render * @return string Rendered collection item block */ public function renderItem($item) { // Iterate all entity fields $fieldsHTML = ''; foreach ($this->fields as $inputField) { // TODO: Maybe we can optimize this requests // Find additional field by Name $field = null; if (dbQuery('field')->cond('Name', $inputField->name)->first($field)) { // Create material field query to get additional field record $query = dbQuery('materialfield')->cond('MaterialID', $item->id)->cond('FieldID', $field->id); // If additional field is localizable add locale condition if ($field->local == 1) { $query->cond('locale', $this->locale); } // Try to find materialfield record for this item and its field $materialfield = null; if (!$query->first($materialfield)) { // Create record if it does not exists $materialfield = new materialfield(false); $materialfield->MaterialID = $item->id; $materialfield->locale = $this->locale; $materialfield->FieldID = $field->id; $materialfield->Active = 1; $materialfield->save(); } // Render input field view $fieldsHTML .= $inputField->render($this->renderer, $this->query, $materialfield); } else { // Render input field view $fieldsHTML .= $inputField->render($this->renderer, $this->query, $item); } } // Render fields row view return $this->renderer->view($this->rowView)->set('cols', $fieldsHTML)->output(); }