public function materialColumnToField($column, $structure) { // Find first user $user = null; if (dbQuery('user')->first($user)) { } // Create structure for all materials $db_structure = null; if (!dbQuery('structure')->Url('__' . $structure)->cond('Active', 1)->first($db_structure)) { $db_structure = new \samson\activerecord\structure(false); $db_structure->Name = $structure; $db_structure->Url = '__' . $structure; $db_structure->Active = 1; $db_structure->UserID = $user->id; $db_structure->system = 1; $db_structure->save(); } $dbField = null; if (!dbQuery('field')->Name($column)->first($dbField)) { $dbField = new \samson\activerecord\field(false); $dbField->Name = $column; $dbField->Type = 8; $dbField->Active = 1; $dbField->system = 1; $dbField->save(); } // Create structure field relations $db_sf = null; if (!dbQuery('structurefield')->FieldID($dbField->id)->StructureID($db_structure->id)->cond('Active', 1)->first($db_sf)) { $db_sf = new \samson\activerecord\structurefield(false); $db_sf->FieldID = $dbField->id; $db_sf->StructureID = $db_structure->id; $db_sf->Active = 1; $db_sf->save(); } // Iterate all existing materials $db_materials = array(); if (dbQuery('material')->cond('Active', '1')->Draft('0')->exec($db_materials)) { trace('Found materials:' . sizeof($db_materials)); foreach ($db_materials as $db_material) { //trace('Updating material:'.$db_material->id); // If current material has no connection with new structure $db_sm = null; if (!dbQuery('structurematerial')->StructureID($db_structure->id)->MaterialID($db_material->id)->first($db_sm)) { // Create this connection $db_sm = new \samson\activerecord\structurematerial(false); $db_sm->StructureID = $db_structure->id; $db_sm->MaterialID = $db_material->id; $db_sm->Active = 1; $db_sm->save(); //trace('Updating structurematerial:'.$db_material->id); } // If this material has no Content field right now $db_mf = null; if (!dbQuery('materialfield')->MaterialID($db_material->id)->FieldID($dbField->id)->cond('Active', 1)->first($db_mf)) { // Create Content additional field $db_mf = new \samson\activerecord\materialfield(false); $db_mf->MaterialID = $db_material->id; $db_mf->FieldID = $dbField->id; $db_mf->Active = 1; $db_mf->Value = $db_material[$column]; $db_mf->save(); //trace('Updating materialfield:'.$db_material->id); } } } db()->query('ALTER TABLE `material` DROP `' . $column . '`'); }
public function createMaterial() { $material = new \samson\activerecord\material(false); $material->Name = $this->Name . ' - material'; $material->Active = 1; $material->Url = generate_password(10); $material->Published = 0; $material->save(); /** @var CMSNav $seoNav */ $seoNav = null; if (dbQuery('structure')->cond('Url', '__seo')->first($seoNav)) { $strmat = new \samson\activerecord\structurematerial(false); $strmat->MaterialID = $material->id; $strmat->StructureID = $seoNav->id; $strmat->Active = 1; $strmat->save(); } return $material->id; }
/** * 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()); }
/** * Recursively create CMSNavigation tree and relations with materials * * @param string $nested_a CMSNav tree * @param array $parents Array of CMSNavs parents chain * @param null $user */ public static function structure_create($nested_a, array $parents = array(), &$user = null) { // Iterate structures array foreach ($nested_a as $k => $v) { // create structure if (is_array($v)) { /** @var \samson\activerecord\Structure $s */ $s = null; $url = utf8_translit($k); // Try to find structure by url if (!dbQuery('structure')->Active(1)->Url($url)->first($s)) { // Create new structure $s = new \samson\activerecord\Structure(false); $s->Name = $k; $s->Active = 1; $s->Url = $url; $s->UserID = $user->id; $s->Created = date('Y-m-d h:i:s'); // Save structure to db $s->save(); } // If parents chain is specified and has data if (sizeof($parents)) { // Get last element from parents chain $parent = end($parents); /** @var \samson\activerecord\structure_relation $str_related */ if ($parent->id != $s->id) { $str_related = new \samson\activerecord\structure_relation(false); $str_related->parent_id = $parent->id; $str_related->child_id = $s->id; $str_related->save(); } } // Add new created structure object to parents chain array_push($parents, $s); // Recursion self::structure_create($v, $parents, $user); // Remove added element from parents chain array_pop($parents); } else { // save structure material foreach ($parents as $parent) { if (!dbQuery('\\samson\\activerecord\\structurematerial')->MaterialID($v->MaterialID)->StructureID($parent['StructureID'])->first($sm)) { $sm = new \samson\activerecord\structurematerial(false); } $sm->MaterialID = $v->MaterialID; $sm->StructureID = $parent['StructureID']; $sm->Active = 1; $sm->save(); } } } }
/** * Assign nested material to the structure * @param $material * @param $structure */ public function assignNestedMaterial($material, $structure) { // Assign material to structure $structureMaterial = new \samson\activerecord\structurematerial(false); $structureMaterial->MaterialID = $material->MaterialID; $structureMaterial->StructureID = $structure->StructureID; $structureMaterial->Active = 1; $structureMaterial->save(); // Update structure field $structure->MaterialID = $material->MaterialID; $structure->save(); }
public function __async_movestructure($childID, $parentID) { $child = $this->query->entity('\\samson\\cms\\Navigation')->id($childID)->first(); $child->ParentID = $parentID; $child->save(); $strIds = array(); $cmsnav = $child->parent(); while ($cmsnav) { $strIds[] = $cmsnav->id; if ($cmsnav->id == $this->catalogID) { break; } $cmsnav = $cmsnav->parent(); } if ($this->query->entity('\\samson\\activerecord\\structure_relation')->where('child_id', $childID)->exec($strRelations)) { foreach ($strRelations as $strRelation) { $strRelation->delete(); } } // Create new relation with new parent $strRelation = new \samson\activerecord\structure_relation(false); $strRelation->child_id = $childID; $strRelation->parent_id = $parentID; $strRelation->save(); // Create array of structure ids which we need to use to create structurematerial relations $relIds = array($parentID); // Get relations of new parent $stRel = $this->query->entity('\\samson\\activerecord\\structure_relation')->child_id($parentID)->exec(); while ($stRel) { // Break flag $break = false; foreach ($stRel as $strR) { // Save parent $relIds[] = $strR->parent_id; if ($strR->parent_id == $this->catalogID) { $break = true; break; } } if ($break) { break; } else { // Get next relations $stRel = $this->query->entity('\\samson\\activerecord\\structure_relation')->child_id($relIds)->exec(); } } // Get materials of current category if (\samson\cms\CMS::getMaterialsByStructures($childID, $materials)) { // Create new structurematerial relations foreach ($materials as $material) { // Delete old structurematerial relations foreach ($this->query->entity('\\samson\\activerecord\\structurematerial')->where('MaterialID', $material->id)->where('StructureID', $strIds)->exec() as $relation) { $relation->delete(); } // Create new relations foreach ($relIds as $relId) { $strMat = new \samson\activerecord\structurematerial(false); $strMat->Active = 1; $strMat->StructureID = $relId; $strMat->MaterialID = $material->id; $strMat->save(); } } } return array('status' => 1); }