/** * Generic material form controller * * @param int|null $materialId Editing material identifier if not null. * If null is passed material creation form will be displayed * @param int|null $navigation Structure material belongs to. */ public function __form($materialId = null, $navigation = null) { // If this is form for a new material with structure relation if ($materialId == 0 && isset($navigation)) { // Create new material db record $material = new \samson\cms\CMSMaterial(false); $material->Active = 1; $material->Created = date('Y-m-d H:m:s'); $user = m('social')->user(); $material->UserID = $user->user_id; $material->save(); // Set new material as current $materialId = $material->id; // Convert parent CMSNavigation to an array $navigationArray = !is_array($navigation) ? array($navigation) : $navigation; // Fill parent CMSNavigation relations for material foreach ($navigationArray as $navigation) { // Create relation with structure $structureMaterial = new \samson\activerecord\structurematerial(false); $structureMaterial->MaterialID = $material->id; $structureMaterial->StructureID = $navigation; $structureMaterial->Active = 1; $structureMaterial->save(); } } // Create form object $form = new Form($materialId, $navigation); if ($materialId == 0) { $this->set('new_material', true); } // Render form $this->html($form->render()); }
/** * Creating new material table row * @param int $materialId Current material identifier * @param int $structureId Current table structure identifier * @param $afterAction * @param $afterMaterialId * @param $afterStructureId * @return array AJAX response */ public function __async_add($materialId, $structureId, $afterAction, $afterMaterialId, $afterStructureId) { /** @var \samson\cms\CMSMaterial $material Current material object */ $material = null; // If there are no such row yet if ($this->query->className('\\samson\\cms\\CMSMaterial')->cond('MaterialID', $materialId)->first($material)) { /** @var array $structures Array of structures of this material */ $structureIds = $this->query->className('structurematerial')->cond('MaterialID', $material->id)->fields('StructureID'); $structures = $this->query->className('structure')->cond('StructureID', $structureIds)->exec(); // If there are some structures if (!empty($structures)) { /** @var \samson\cms\Navigation $structure Table structure */ foreach ($structures as $structure) { // If current material has incoming structure // TODO: Sometimes array has empty values not object - needs to checked if (isset($structure->StructureID) && $structure->StructureID == $structureId) { /** @var \samson\social\Core $socialModule Social module object */ $socialModule = m('social'); /** @var \samson\activerecord\user $user User object */ $user = $socialModule->user(); // Get max priority of this structure $maxPriority = $this->getMaxPriority($materialId, $structureId); /** @var \samson\cms\CMSMaterial $tableMaterial New table material (table row) */ $tableMaterial = new \samson\cms\CMSMaterial(false); $tableMaterial->type = 3; $tableMaterial->Name = $structure->Name; $tableMaterial->Url = $structure->Name . '-' . md5(date('Y-m-d-h-i-s')); $tableMaterial->parent_id = $material->MaterialID; $tableMaterial->Published = 1; $tableMaterial->Active = 1; $tableMaterial->priority = $maxPriority + 1; $tableMaterial->UserID = $user->id; $tableMaterial->Created = date('Y-m-d H:m:s'); $tableMaterial->Modyfied = $tableMaterial->Created; $tableMaterial->save(); /** @var \samson\cms\CMSNavMaterial $structureMaterial Relation between created table material * and table structure */ $structureMaterial = new \samson\cms\CMSNavMaterial(false); $structureMaterial->StructureID = $structureId; $structureMaterial->MaterialID = $tableMaterial->MaterialID; $structureMaterial->Active = 1; $structureMaterial->save(); } } $result = $this->__async_table($afterMaterialId, $afterStructureId); Event::fire('samson.cms.web.materialtable.add', array($material->id, $structureId)); // Set success status and return result // return array('status' => 1); return $result; } } // Set fail status and return result return array('status' => 0); }