/** 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(); } } }
/** * Add dynamic tags into specific material * @param $materialId * @param $structureId */ public function addDynamicTags($materialId, $structureId) { $dynamicSchema = new Dynamic(); if ($structureId == $dynamicSchema->getStructureId()) { // If binding material and structure don't exists then create it if ($this->query->className('structurematerial')->cond('StructureID', $structureId)->cond('MaterialID', $materialId)->count() == 0) { // Create relations $sm = new structurematerial(false); $sm->StructureID = $structureId; $sm->MaterialID = $materialId; $sm->save(); // Add row m('material_table')->__async_add($materialId, $structureId); } } }
/** * Add new row to table of entity * @param $row */ public function addTableRow(Row $row) { // Get user $user = m('socialemail')->user(); $tableMaterial = new Material(); $tableMaterial->parent_id = $this->id; $tableMaterial->type = 3; $tableMaterial->Name = $this->Url . '-' . md5(date('Y-m-d-h-i-s')); $tableMaterial->Url = $this->Url . '-' . md5(date('Y-m-d-h-i-s')); $tableMaterial->Published = 1; $tableMaterial->Active = 1; $tableMaterial->priority = 0; $tableMaterial->UserID = $user->id; $tableMaterial->Created = date('Y-m-d H:m:s'); $tableMaterial->Modyfied = date('Y-m-d H:m:s'); $tableMaterial->save(); // TODO: Ugly way to retrieve static var $class = new \ReflectionClass(preg_replace('/Row$/', '', get_class($row))); $structureId = $class->getConstant('IDENTIFIER'); $structureMaterial = new structurematerial(); $structureMaterial->Active = 1; $structureMaterial->MaterialID = $tableMaterial->id; $structureMaterial->StructureID = $structureId; $structureMaterial->save(); // TODO: Ugly way to retrieve static var $class = new \ReflectionClass(get_class($row)); $fieldIDs = $class->getStaticPropertyValue('fieldIDs'); // Iterate and set all fields of row foreach ($row as $id => $value) { /** * Go next if it primary key because its public * TODO Fix it */ if ($id === 'primary') { continue; } // Get field id $fieldId = $fieldIDs[$id]; // Add additional field to created material $tableMaterial->setFieldByID($fieldId, $value); } // Save material $tableMaterial->save(); }
/** * Assign structure to passed material * @param $material * @param $structure * @return mixed|structurematerial */ public function assignMaterialToStructure($material, $structure) { $sm = $this->query->className('structurematerial')->cond('MaterialID', $material->MaterialID)->cond('StructureID', $structure->StructureID)->first(); // Create new relation if its not exists if (empty($sm)) { $sm = new structurematerial(false); $sm->StructureID = $structure->StructureID; $sm->MaterialID = $material->MaterialID; $sm->Active = 1; $sm->save(); } return $sm; }