public function migrate_6_to_7() { $db_structures = null; // Convert all old "date" fields to numeric for fixing db requests if (dbQuery('structure')->cond('Active', 1)->exec($db_structures)) { foreach ($db_structures as $db_structure) { $relation = new \samson\activerecord\structure_relation(false); $relation->parent_id = $db_structure->ParentID; $relation->child_id = $db_structure->id; $relation->save(); } } }
/** * Filling the fields and creating relation of structure */ public function fillFields() { // Fill the fields from $_POST array foreach ($_POST as $key => $val) { // Get int value form field parent id if ($key == 'ParentID' && $val == 0) { $this[$key] = null; // Get other fields } elseif ($key != 'StructureID') { $this[$key] = $val; } } // Save data about application $this['applicationGenerate'] = isset($_POST['generate-application']) && $_POST['generate-application'] == true ? 1 : 0; $this['applicationOutput'] = isset($_POST['output-application']) && $_POST['output-application'] == true ? 1 : 0; $this['applicationOutputStructure'] = isset($_POST['output-structure-application']) && $_POST['output-structure-application'] == true ? 1 : 0; $this['applicationRenderMain'] = isset($_POST['render-main-application']) && $_POST['render-main-application'] == true ? 1 : 0; // If application need to generate then set system property to 1 or 0 $this['system'] = isset($_POST['generate-application']) && $_POST['generate-application'] == true ? 1 : 0; // Save icon $icon = isset($_POST['icon-application']) ? filter_var($_POST['icon-application']) : null; if (strlen($icon) > 0) { $this['applicationIcon'] = $icon; } /** TODO: Authenticated user can be not instance of user table */ if (!$this->query->entity('\\samson\\activerecord\\user')->where('user_id', $this->UserID)->first()) { $this->UserID = 1; } // Save object $this->save(); if (isset($_POST['ParentID']) && $_POST['ParentID'] != 0) { // Create new relation $strRelation = new \samson\activerecord\structure_relation(false); $strRelation->parent_id = $_POST['ParentID']; $strRelation->child_id = $this->id; // Save relation $strRelation->save(); } }
/** * Create structure and if isset parent structure assign it to them * @param $name * @param $url * @param int $type * @param int $parentId * @return \samson\activerecord\structure */ public function createStructure($name, $url, $type = self::NESTED_MATERIAL_TYPE_STRUCTURE, $parentId = 0) { $structure = new \samson\activerecord\structure(false); $structure->Name = $name; $structure->Url = $url; $structure->Active = 1; $structure->type = $type; $structure->system = 1; $structure->hidden = 1; $structure->ParentID = $parentId; $structure->save(); // Create structure relation if this structure have to be child if ($parentId != 0) { $structureRelation = new \samson\activerecord\structure_relation(false); $structureRelation->child_id = $structure->StructureID; $structureRelation->parent_id = $parentId; $structureRelation->save(); } return $structure; }
/** * 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(); } } } }
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); }