public static function addRowKey($repeaterId, $rowId)
 {
     $repeaterRows = static::preloadRepeater($repeaterId);
     if (!isset($repeaterRows[$rowId])) {
         $repeater_row = new PageBlockRepeaterRows();
         $repeater_row->repeater_id = $repeaterId;
         $repeater_row->row_id = $rowId;
         $repeater_row->save();
         $repeaterRows[$rowId] = $repeater_row;
         static::_preloadAdd('repeater' . $repeaterId, $rowId, $repeater_row);
     }
     return $repeaterRows[$rowId]->id;
 }
 public static function loadRepeaterData($repeaterId, $version = 0, $randomOrder = false)
 {
     $repeaterRowsById = [];
     if ($rowIds = PageBlockRepeaterRows::getRowIds($repeaterId)) {
         $repeaterRowsData = Block::getDataForVersion(new static(), $version, ['row_key' => array_keys($rowIds)]) ?: [];
         foreach ($repeaterRowsData as $repeaterRowData) {
             $repeaterRowId = $rowIds[$repeaterRowData->row_key];
             if (!isset($repeaterRowsById[$repeaterRowId])) {
                 $repeaterRowsById[$repeaterRowId] = [];
             }
             $repeaterRowsById[$repeaterRowId][$repeaterRowData->block_id] = $repeaterRowData->content;
         }
         if ($randomOrder === false) {
             uasort($repeaterRowsById, ['self', '_order']);
         } else {
             shuffle($repeaterRowsById);
             $repeaterRowsById = array_slice($repeaterRowsById, 0, $randomOrder);
         }
     }
     return $repeaterRowsById;
 }
Exemple #3
0
 public static function _pageImportData($theme)
 {
     $importPath = $themePath = base_path() . '/resources/views/themes/' . $theme->theme . '/import/';
     if (is_dir($importPath)) {
         // wipe data
         DB::table((new Page())->getTable())->truncate();
         DB::table((new PageLang())->getTable())->truncate();
         DB::table((new PageVersion())->getTable())->truncate();
         DB::table((new PagePublishRequests())->getTable())->truncate();
         DB::table((new PageGroup())->getTable())->truncate();
         DB::table((new PageGroupAttribute())->getTable())->truncate();
         DB::table((new PageGroupPage())->getTable())->truncate();
         DB::table((new Menu())->getTable())->truncate();
         DB::table((new MenuItem())->getTable())->truncate();
         DB::table((new PageBlockDefault())->getTable())->truncate();
         DB::table((new PageBlock())->getTable())->truncate();
         DB::table((new PageBlockRepeaterData())->getTable())->truncate();
         DB::table((new PageBlockRepeaterRows())->getTable())->truncate();
         $templateIds = [];
         $templates = Template::where('theme_id', '=', $theme->id)->get();
         foreach ($templates as $template) {
             $templateIds[$template->template] = $template->id;
         }
         $blockIds = [];
         $blocks = Block::all();
         foreach ($blocks as $block) {
             $blockIds[$block->name] = $block->id;
         }
         $pagesCsv = $importPath . 'pages.csv';
         $groupsCsv = $importPath . 'pages/groups.csv';
         $groupAttributesCsv = $importPath . 'pages/group_attributes.csv';
         $menusCsv = $importPath . 'pages/menus.csv';
         $menuItemsCsv = $importPath . 'pages/menu_items.csv';
         $pageBlocksCsv = $importPath . 'pages/page_blocks.csv';
         $repeaterBlocksCsv = $importPath . 'pages/repeater_blocks.csv';
         // checks
         $error = 'pages data not imported, invalid columns in: ';
         if (!($pagesFileHandle = Csv::check($pagesCsv, 14))) {
             if (file_exists($pagesCsv)) {
                 throw new \Exception($error . $pagesCsv);
             }
         }
         if (!($groupsHandle = Csv::check($groupsCsv, 5))) {
             if (file_exists($groupsCsv)) {
                 throw new \Exception($error . $groupsCsv);
             }
         }
         if (!($groupAttributesHandle = Csv::check($groupAttributesCsv, 6))) {
             if (file_exists($groupAttributesCsv)) {
                 throw new \Exception($error . $groupAttributesCsv);
             }
         }
         if (!($menusHandle = Csv::check($menusCsv, 3))) {
             if (file_exists($menusCsv)) {
                 throw new \Exception($error . $menusCsv);
             }
         }
         if (!($menuItemsCsvHandle = Csv::check($menuItemsCsv, 5))) {
             if (file_exists($menuItemsCsv)) {
                 throw new \Exception($error . $menuItemsCsv);
             }
         }
         if (!($pageBlocksCsvHandle = Csv::check($pageBlocksCsv, 3))) {
             if (file_exists($pageBlocksCsv)) {
                 throw new \Exception($error . $pageBlocksCsv);
             }
         }
         if (!($repeaterBlocksCsvHandle = Csv::check($repeaterBlocksCsv, 4))) {
             if (file_exists($repeaterBlocksCsv)) {
                 throw new \Exception($error . $repeaterBlocksCsv);
             }
         }
         // add pages
         if ($pagesFileHandle) {
             $row = 0;
             while (($data = fgetcsv($pagesFileHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Page Id') {
                     continue;
                 }
                 list($pageId, $pageName, $pageUrl, $templateName, $parentId, $defaultChildTemplateName, $order, $link, $live, $sitemap, $groupContainer, $groupContainerUrlPriority, $canonicalParentPageId, $groupIds) = $data;
                 $newPage = new Page();
                 $newPage->id = $pageId;
                 $newPage->template = !empty($templateIds[$templateName]) ? $templateIds[$templateName] : 0;
                 $newPage->parent = $parentId ?: 0;
                 $newPage->child_template = !empty($templateIds[$defaultChildTemplateName]) ? $templateIds[$defaultChildTemplateName] : 0;
                 $newPage->order = $order;
                 $newPage->link = $link;
                 $newPage->live = $live;
                 $newPage->sitemap = $sitemap;
                 $newPage->group_container = $groupContainer ?: 0;
                 $newPage->group_container_url_priority = $groupContainerUrlPriority ?: 0;
                 $newPage->canonical_parent = $canonicalParentPageId ?: 0;
                 $newPage->save();
                 $newPageLang = new PageLang();
                 $newPageLang->page_id = $pageId;
                 $newPageLang->language_id = Language::current();
                 $newPageLang->name = $pageName;
                 $newPageLang->url = $pageUrl;
                 $newPageLang->live_version = 1;
                 $newPageLang->save();
                 PageVersion::add_new($pageId);
                 $groupIds = trim($groupIds);
                 $groupIds = $groupIds ? explode(',', $groupIds) : [];
                 foreach ($groupIds as $groupId) {
                     $newPageGroupPage = new PageGroupPage();
                     $newPageGroupPage->page_id = $pageId;
                     $newPageGroupPage->group_id = $groupId;
                     $newPageGroupPage->save();
                 }
             }
         }
         // add page groups
         if ($groupsHandle) {
             $row = 0;
             while (($data = fgetcsv($groupsHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Group Id') {
                     continue;
                 }
                 list($groupId, $groupName, $itemName, $defaultContainerPageId, $defaultTemplate) = $data;
                 $newGroup = new PageGroup();
                 $newGroup->id = $groupId;
                 $newGroup->name = $groupName;
                 $newGroup->item_name = $itemName;
                 $newGroup->url_priority = $defaultContainerPageId;
                 $newGroup->default_template = !empty($templateIds[$defaultTemplate]) ? $templateIds[$defaultTemplate] : 0;
                 $newGroup->save();
             }
         }
         if ($groupAttributesHandle) {
             $row = 0;
             while (($data = fgetcsv($groupAttributesHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Attribute Id') {
                     continue;
                 }
                 list($attributeId, $groupId, $blockName, $orderPriority, $orderDir, $filerByBlockName) = $data;
                 $newGroupAttribute = new PageGroupAttribute();
                 $newGroupAttribute->id = $attributeId;
                 $newGroupAttribute->group_id = $groupId;
                 $newGroupAttribute->item_block_id = !empty($blockIds[$blockName]) ? $blockIds[$blockName] : 0;
                 $newGroupAttribute->item_block_order_priority = $orderPriority;
                 $newGroupAttribute->item_block_order_dir = $orderDir == 'desc' ? $orderDir : 'asc';
                 $newGroupAttribute->filter_by_block_id = !empty($blockIds[$filerByBlockName]) ? $blockIds[$filerByBlockName] : 0;
                 $newGroupAttribute->save();
             }
         }
         // add menus
         if ($menusHandle) {
             $menuIds = [];
             $row = 0;
             while (($data = fgetcsv($menusHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Menu Identifier') {
                     continue;
                 }
                 list($name, $label, $maxSublevel) = $data;
                 $newMenu = new Menu();
                 $newMenu->label = $label;
                 $newMenu->name = $name;
                 $newMenu->max_sublevel = $maxSublevel;
                 $newMenu->save();
                 $menuIds[$name] = $newMenu->id;
             }
         }
         if ($menuItemsCsvHandle) {
             $row = 0;
             while (($data = fgetcsv($menuItemsCsvHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Menu Identifier') {
                     continue;
                 }
                 list($menuIdentifier, $pageId, $order, $subLevels, $customName) = $data;
                 if (!empty($menuIds[$menuIdentifier])) {
                     $newMenuItem = new MenuItem();
                     $newMenuItem->menu_id = $menuIds[$menuIdentifier];
                     $newMenuItem->page_id = $pageId;
                     $newMenuItem->order = $order;
                     $newMenuItem->sub_levels = $subLevels;
                     $newMenuItem->custom_name = $customName;
                     $newMenuItem->save();
                 }
             }
         }
         // add page content
         if ($pageBlocksCsvHandle) {
             $row = 0;
             while (($data = fgetcsv($pageBlocksCsvHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Page Id') {
                     continue;
                 }
                 list($pageId, $blockName, $content) = $data;
                 if (!empty($blockIds[$blockName])) {
                     if ($pageId) {
                         $newPageBlock = new PageBlock();
                         $newPageBlock->page_id = $pageId;
                     } else {
                         $newPageBlock = new PageBlockDefault();
                     }
                     $newPageBlock->block_id = $blockIds[$blockName];
                     $newPageBlock->version = 1;
                     $newPageBlock->content = $content;
                     $newPageBlock->save();
                 }
             }
         }
         if ($repeaterBlocksCsvHandle) {
             $row = 0;
             $existingRepeaterRowKeys = [];
             while (($data = fgetcsv($repeaterBlocksCsvHandle)) !== false) {
                 if ($row++ == 0 && $data[0] == 'Repeater Id') {
                     continue;
                 }
                 list($repeaterId, $repeaterRowId, $blockName, $content) = $data;
                 if (!empty($blockIds[$blockName])) {
                     if ($decodedContent = json_decode($content)) {
                         if (!is_string($decodedContent)) {
                             $content = serialize($decodedContent);
                         }
                     }
                     if (!isset($existingRepeaterRowKeys[$repeaterId . '-' . $repeaterRowId])) {
                         $newRepeaterRow = new PageBlockRepeaterRows();
                         $newRepeaterRow->repeater_id = $repeaterId;
                         $newRepeaterRow->row_id = $repeaterRowId;
                         $newRepeaterRow->save();
                         $existingRepeaterRowKeys[$repeaterId . '-' . $repeaterRowId] = $newRepeaterRow->id;
                         $newRepeaterData = new PageBlockRepeaterData();
                         $newRepeaterData->row_key = $existingRepeaterRowKeys[$repeaterId . '-' . $repeaterRowId];
                         $newRepeaterData->block_id = 0;
                         $newRepeaterData->version = 1;
                         $newRepeaterData->content = $repeaterRowId;
                         $newRepeaterData->save();
                     }
                     $newRepeaterData = new PageBlockRepeaterData();
                     $newRepeaterData->row_key = $existingRepeaterRowKeys[$repeaterId . '-' . $repeaterRowId];
                     $newRepeaterData->block_id = $blockIds[$blockName];
                     $newRepeaterData->version = 1;
                     $newRepeaterData->content = $content;
                     $newRepeaterData->save();
                 }
             }
         }
         PageSearchData::updateAllSearchData();
     }
 }
Exemple #4
0
 public function delete()
 {
     $page_name = PageLang::getName($this->id);
     $log_id = AdminLog::new_log('Page \'' . $page_name . '\' deleted (Page ID ' . $this->id . ')');
     // make backups
     $page_versions = PageVersion::where('page_id', '=', $this->id);
     $page_langs = PageLang::where('page_id', '=', $this->id);
     $page_blocks = PageBlock::where('page_id', '=', $this->id);
     $menu_items = MenuItem::where('page_id', '=', $this->id)->orWhere('page_id', 'LIKE', $this->id . ',%');
     $user_role_page_actions = UserRolePageAction::where('page_id', '=', $this->id);
     $page_groups = PageGroupPage::where('page_id', '=', $this->id);
     $publish_request_ids = [];
     foreach ($page_versions as $page_version) {
         $publish_request_ids[] = $page_version->id;
     }
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\Page', $this);
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\PageVersion', $page_versions->get());
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\PageLang', $page_langs->get());
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\PageBlock', $page_blocks->get());
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\MenuItem', $menu_items->get());
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\UserRolePageAction', $user_role_page_actions->get());
     Backup::new_backup($log_id, '\\CoasterCms\\Models\\PageGroupPage', $page_groups->get());
     // publish requests
     if (!empty($publish_request_ids)) {
         $page_publish_requests = PagePublishRequests::where('page_version_id', '=', $publish_request_ids);
         Backup::new_backup($log_id, '\\CoasterCms\\Models\\PagePublishRequests', $page_publish_requests->get());
         $page_publish_requests->delete();
     }
     // repeater data
     $repeater_block_ids = Block::getBlockIdsOfType('repeater');
     if (!empty($repeater_block_ids)) {
         $repeater_blocks = PageBlock::whereIn('block_id', $repeater_block_ids)->where('page_id', $this->id)->get();
         if (!$repeater_blocks->isEmpty()) {
             $repeater_ids = [];
             foreach ($repeater_blocks as $repeater_block) {
                 $repeater_ids[] = $repeater_block->content;
             }
             $repeater_row_keys = PageBlockRepeaterRows::whereIn('repeater_id', $repeater_ids);
             $repeater_row_keys_get = $repeater_row_keys->get();
             if (!$repeater_row_keys_get->isEmpty()) {
                 $row_keys = [];
                 foreach ($repeater_row_keys_get as $repeater_row_key) {
                     $row_keys[] = $repeater_row_key->id;
                 }
                 $repeater_data = PageBlockRepeaterData::whereIn('row_key', $row_keys);
                 Backup::new_backup($log_id, '\\CoasterCms\\Models\\PageBlockRepeaterRows', $repeater_row_keys->get());
                 Backup::new_backup($log_id, '\\CoasterCms\\Models\\PageBlockRepeaterData', $repeater_data->get());
                 $repeater_data->delete();
                 $repeater_row_keys->delete();
             }
         }
     }
     // delete data
     $this->groups()->detach();
     $page_versions->delete();
     $page_langs->delete();
     $page_blocks->delete();
     $menu_items->delete();
     $user_role_page_actions->delete();
     PageSearchData::where('page_id', '=', $this->id)->delete();
     parent::delete();
     $return_log_ids = array($log_id);
     $child_pages = self::where('parent', '=', $this->id)->get();
     if (!empty($child_pages)) {
         foreach ($child_pages as $child_page) {
             $log_ids = $child_page->delete();
             $return_log_ids = array_merge($log_ids, $return_log_ids);
         }
     }
     sort($return_log_ids);
     return $return_log_ids;
 }
 /**
  * Add new repeater row with passed block contents onto end of repeater rows (or create repeater and set as first row)
  * @param array $repeaterBlockContents
  */
 public function insertRow($repeaterBlockContents)
 {
     if (!($repeaterId = $this->_block->getContent())) {
         $repeaterId = PageBlockRepeaterRows::nextFreeRepeaterId();
         $this->save($repeaterId);
         $currentRepeaterRows = [];
     } else {
         $currentRepeaterRows = PageBlockRepeaterData::loadRepeaterData($repeaterId);
     }
     $repeaterRowId = PageBlockRepeaterRows::nextFreeRepeaterRowId($repeaterId);
     if (!array_key_exists(0, $repeaterBlockContents)) {
         if (!empty($currentRepeaterRows)) {
             $rowOrders = array_map(function ($row) {
                 return !empty($row[0]) ? $row[0] : 0;
             }, $currentRepeaterRows);
             $repeaterBlockContents[0] = max($rowOrders) + 1;
         } else {
             $repeaterBlockContents[0] = 1;
         }
     }
     foreach ($repeaterBlockContents as $blockName => $content) {
         $block = Block::preloadClone($blockName);
         if ($block->exists || $blockName == 0) {
             $block->id = $blockName === 0 ? 0 : $block->id;
             $block->setVersionId($this->_block->getVersionId())->setRepeaterData($repeaterId, $repeaterRowId)->setPageId($this->_block->getPageId())->getTypeObject()->save($content);
         }
     }
 }