Exemplo n.º 1
0
 public static function version_table($page_id)
 {
     $versionsQuery = static::with(['user', 'scheduled_versions'])->where('page_id', '=', $page_id)->orderBy('version_id', 'desc');
     $versions = $versionsQuery->paginate(15);
     $pagination = PaginatorRender::admin($versions);
     $page_lang = PageLang::where('page_id', '=', $page_id)->where('language_id', '=', Language::current())->first();
     $live_version = static::where('page_id', '=', $page_id)->where('version_id', '=', $page_lang ? $page_lang->live_version : 0)->first();
     $live_version = $live_version ?: new static();
     $can_publish = Auth::action('pages.version-publish', ['page_id' => $page_id]);
     return View::make('coaster::partials.tabs.versions.table', ['versions' => $versions, 'pagination' => $pagination, 'live_version' => $live_version, 'can_publish' => $can_publish])->render();
 }
Exemplo n.º 2
0
 /**
  * Saves page data
  * @param PageVersion $pageVersion
  * @param array $pagePost
  * @param array $pageLangPost
  * @param array $pageGroupsPost
  * @param array $pageInfoOther
  * @return bool
  */
 public function savePostData($pageVersion, $pagePost, $pageLangPost, $pageGroupsPost, $pageInfoOther = [])
 {
     /*
      * Post data fixes
      */
     foreach ($pagePost as $k => $pagePostField) {
         if (is_array($pagePostField) && array_key_exists('exists', $pagePostField)) {
             $pagePost[$k] = array_key_exists('select', $pagePostField) ? $pagePostField['select'] : 0;
         }
     }
     if (array_key_exists('live_start', $pagePost)) {
         $pagePost['live_start'] = DateTimeHelper::jQueryToMysql($pagePost['live_start']) ?: null;
     }
     if (array_key_exists('live_end', $pagePost)) {
         $pagePost['live_end'] = DateTimeHelper::jQueryToMysql($pagePost['live_end']) ?: null;
     }
     foreach ($pageInfoOther as $k => $pageInfoOtherField) {
         if (is_array($pageInfoOtherField) && array_key_exists('exists', $pageInfoOtherField) && array_key_exists('select', $pageInfoOtherField)) {
             $pageInfoOther[$k] = $pageInfoOtherField['select'];
         }
     }
     /*
      * Overwrite default/existing data with posted data
      */
     $pageDefaults = array_merge(['template' => 0, 'parent' => 0, 'child_template' => 0, 'order' => 0, 'group_container' => 0, 'group_container_url_priority' => 0, 'canonical_parent' => 0, 'link' => 0, 'live' => 0, 'sitemap' => 1, 'live_start' => null, 'live_end' => null], $this->getAttributes());
     foreach ($pageDefaults as $pageAttribute => $pageDefault) {
         $this->{$pageAttribute} = $pageDefault;
         switch ($pageAttribute) {
             case 'template':
                 $pageVersion->{$pageAttribute} = $pagePost[$pageAttribute];
                 break;
             default:
                 $this->{$pageAttribute} = array_key_exists($pageAttribute, $pagePost) ? $pagePost[$pageAttribute] : $this->{$pageAttribute};
         }
     }
     if (!$this->pageCurrentLang) {
         $this->setRelation('pageCurrentLang', ($d = $this->pageDefaultLang) ? $d->replicate() : new PageLang());
         unset($this->pageCurrentLang->language_id);
     }
     $pageLang = $this->pageLang();
     $pageLangDefaults = array_merge(['language_id' => Language::current(), 'url' => '', 'name' => '', 'live_version' => 1], $pageLang->getAttributes());
     foreach ($pageLangDefaults as $pageLangAttribute => $pageLangDefault) {
         $pageLang->{$pageLangAttribute} = array_key_exists($pageLangAttribute, $pageLangPost) ? $pageLangPost[$pageLangAttribute] : $pageLangDefault;
     }
     /*
      * Check page parent exists if set and page limit is not hit
      */
     $parent = static::find($this->parent);
     if ($this->parent > 0 && !$parent) {
         return false;
     }
     if (!$this->id && !$this->link && static::at_limit($this->parent == -1)) {
         return false;
     }
     /*
      * Check page name/url set and does not conflict
      */
     $pageLang->url = trim($pageLang->url);
     if (!$this->link) {
         $pageLang->url = strtolower(str_replace(['/', ' '], '-', $pageLang->url));
         if (preg_match('#^[-]+$#', $pageLang->url)) {
             $pageLang->url = '';
         }
         if ($pageLang->url == '' && !$this->parent) {
             $pageLang->url = '/';
         }
         $siblings = [];
         foreach ($pageGroupsPost as $pageGroupId => $checkedVal) {
             $pageGroup = PageGroup::preload($pageGroupId);
             $siblings = array_merge($pageGroup->exists ? $pageGroup->itemPageIds() : [], $siblings);
         }
         if ($this->parent >= 0) {
             $siblings = array_merge(static::getChildPageIds($this->parent), $siblings);
         }
         $siblings = array_unique($siblings);
     }
     if (!$pageLang->name) {
         FormMessage::add('page_info_lang[name]', 'page name required');
     }
     if (!$pageLang->url) {
         FormMessage::add('page_info_lang[url]', 'page url required');
     }
     if (!empty($siblings)) {
         $same_level = PageLang::where('url', '=', $pageLang->url)->whereIn('page_id', $siblings);
         $same_level = $this->id ? $same_level->where('page_id', '!=', $this->id) : $same_level;
         if (!$same_level->get()->isEmpty()) {
             FormMessage::add('page_info_lang[url]', 'url in use by another page!');
             $pageLang->url = '';
         }
     }
     if (!$pageLang->name || !$pageLang->url) {
         return false;
     }
     /*
      * If adding a page as a group container, create container / check exists
      */
     if ($this->group_container == -1) {
         $groupContainer = new PageGroup();
         $groupContainer->name = $pageLang->name;
         $groupContainer->item_name = 'Page';
         $groupContainer->default_template = 0;
         $groupContainer->save();
         $this->group_container = $groupContainer->id;
     } elseif ($this->group_container) {
         $groupContainer = PageGroup::preload($this->group_container);
         if (!$groupContainer->exists || $pageDefaults['group_container'] != $this->group_container && !$groupContainer->canEditItems()) {
             $this->group_container = 0;
         }
     }
     /*
      * Check if page info can be updated (based on publishing auth action, or allowed if new page)
      */
     $authPageIdCheck = $this->id ?: ($this->parent > 0 ? $this->parent : 0);
     $canPublish = config('coaster::admin.publishing') > 0 && Auth::action('pages.version-publish', ['page_id' => $authPageIdCheck]) || config('coaster::admin.publishing') == 0 && Auth::action('pages.edit', ['page_id' => $authPageIdCheck]);
     $canPublish = $canPublish || isset($groupContainer) && (config('coaster::admin.publishing') > 0 && $groupContainer->canPublishItems() || config('coaster::admin.publishing') == 0 && $groupContainer->canEditItems());
     $willPublish = !$this->id || $canPublish;
     /*
      * Check and save page changes
      */
     if ($willPublish) {
         // if new page set as last ordered page
         if ($this->parent >= 0 && !$this->id) {
             $lastSibling = static::where('parent', '=', $this->parent)->orderBy('order', 'desc')->first();
             $this->order = $lastSibling ? $lastSibling->order + 1 : 1;
         }
         // if new page publish template
         $this->template = $this->id ? $this->template : $pageVersion->template;
         // if link remove live template
         $this->template = $this->link ? 0 : $this->template;
         // set page live between but no dates set set as hidden, or if can't publish set as hidden
         $this->live = $this->live == 2 && is_null($this->live_end) && is_null($this->live_start) ? 0 : $this->live;
         $this->live = $canPublish ? $this->live : 0;
         // save page data
         $this->save();
         $pageLang->page_id = $this->id;
         $pageLang->save();
     }
     $pageVersion->page_id = $this->id;
     $pageVersion->save();
     /*
      * Update title block to the page name is new page
      */
     if (!$this->id && ($titleBlock = Block::where('name', '=', config('coaster::admin.title_block'))->first())) {
         $titleBlock->setVersionId($pageVersion->version_id)->setPageId($this->id)->getTypeObject()->save($pageLang->name);
         PageSearchData::updateText(strip_tags($pageLang->name), 0, $this->id);
     }
     /*
      * Save Page Groups
      */
     $currentGroupIds = $this->groupIds();
     $newGroupIds = array_keys($pageGroupsPost);
     PageGroupPage::where('page_id', '=', $this->id)->whereIn('group_id', array_diff($currentGroupIds, $newGroupIds))->delete();
     foreach (array_diff($newGroupIds, $currentGroupIds) as $addGroupId) {
         $this->groups()->attach($addGroupId);
     }
     /*
      * Save other page info
      */
     if ($willPublish && Auth::action('menus')) {
         MenuItem::set_page_menus($this->id, array_key_exists('menus', $pageInfoOther) ? $pageInfoOther['menus'] : []);
     }
     if ($canPublish && array_key_exists('beacons', $pageInfoOther) && Auth::action('themes.beacons-update')) {
         BlockBeacon::updatePage($this->id, $pageInfoOther['beacons']);
     }
     return true;
 }
Exemplo n.º 3
0
 private static function _pageExportData($theme)
 {
     @mkdir(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages');
     $pagesCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages.csv', 'w');
     $pageBlocksCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages/page_blocks.csv', 'w');
     $repeaterBlocksCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages/repeater_blocks.csv', 'w');
     $menusCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages/menus.csv', 'w');
     $menuItemsCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages/menu_items.csv', 'w');
     $allFilesUsed = [];
     $groupIds = [];
     $groups = PageGroup::all();
     foreach ($groups as $group) {
         $groupIds[$group->id] = $group->id;
     }
     $pagesData = [];
     $pages = Page::all();
     foreach ($pages as $page) {
         $pagesData[$page->id] = $page;
     }
     $pageLangData = [];
     $pageLangs = PageLang::where('language_id', '=', Language::current())->orderBy('page_id')->get();
     foreach ($pageLangs as $pageLang) {
         $pageLangData[$pageLang->page_id] = $pageLang;
     }
     $templatesById = [];
     $templatesByName = [];
     $templates = Template::where('theme_id', '=', $theme->id)->get();
     foreach ($templates as $template) {
         $templatesById[$template->id] = $template->template;
         $templatesByName[$template->template] = $template->id;
     }
     // export pages
     fputcsv($pagesCsv, ['Page Id', 'Page Name', 'Page Url', 'Page Template', 'Parent Page Id', 'Default Child Template', 'Page Order Value', 'Is Link (0 or 1)', 'Is Live (0 or 1)', 'In Sitemap (0 or 1)', 'Container for Group Id', 'Container Url Priority', 'Canonical Parent Page Id', 'Group Ids (Comma Separated)']);
     foreach ($pageLangs as $pageLang) {
         if (!empty($pagesData[$pageLang->page_id])) {
             $page = $pagesData[$pageLang->page_id];
             if ($page->link && (strpos($pageLang->url, URL::to('/')) === 0 || strpos($pageLang->url, '/') === 0)) {
                 $filesUsed = [str_replace(URL::to('/'), '', $pageLang->url)];
                 $allFilesUsed = array_merge($filesUsed, $allFilesUsed);
             }
             fputcsv($pagesCsv, [$pageLang->page_id, $pageLang->name, $pageLang->url, !empty($templatesById[$page->template]) ? $templatesById[$page->template] : '', $page->parent ?: '', !empty($templatesById[$page->child_template]) ? $templatesById[$page->child_template] : '', $page->order, $page->link, $page->live ? 1 : 0, $page->sitemap, $page->group_container && !empty($groupIds[$page->group_container]) ? $page->group_container : '', $page->group_container_url_priority && !empty($groupIds[$page->group_container]) ? $page->group_container_url_priority : '', $page->canonical_parent, implode(',', $page->groupIds())]);
         }
     }
     // export menus
     fputcsv($menusCsv, ['Menu Identifier', 'Menu Name', 'Menu Max Sublevels']);
     $menuIds = [];
     $menuIdentifiers = [];
     $menus = Menu::all();
     foreach ($menus as $menu) {
         $menuIds[] = $menu->id;
         $menuIdentifiers[$menu->id] = $menu->name;
         fputcsv($menusCsv, [$menu->name, $menu->label, $menu->max_sublevel]);
     }
     fputcsv($menuItemsCsv, ['Menu Identifier', 'Item Page Id', 'Item Order', 'Item Sublevels', 'Item Custom Name']);
     $menuItems = MenuItem::whereIn('menu_id', $menuIds)->get()->all();
     usort($menuItems, function ($a, $b) {
         if ($a->menu_id == $b->menu_id) {
             if ($a->order == $b->order) {
                 return 0;
             }
             return $a->order < $b->order ? -1 : 1;
         }
         return $a->menu_id < $b->menu_id ? -1 : 1;
     });
     foreach ($menuItems as $menuItem) {
         fputcsv($menuItemsCsv, [$menuIdentifiers[$menuItem->menu_id], $menuItem->page_id, $menuItem->order, $menuItem->sub_levels, $menuItem->custom_name]);
     }
     // export page groups
     if (!$groups->isEmpty()) {
         $groupsCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages/groups.csv', 'w');
         $groupAttributesCsv = fopen(base_path() . '/resources/views/themes/' . $theme->theme . '/export/pages/group_attributes.csv', 'w');
         fputcsv($groupsCsv, ['Group Id', 'Group Name', 'Group Item Name', 'Url Priority (Default: 50)', 'Default Template']);
         fputcsv($groupAttributesCsv, ['Attribute Id', 'Group Id', 'Block Name', 'Order Priority (0 for no ordering)', 'Order Dir (asc/desc)', 'Container Filter by Block Name']);
         $groupAttributesByGroupId = [];
         $groupAttributes = PageGroupAttribute::orderBy('group_id')->get();
         foreach ($groupAttributes as $groupAttribute) {
             if (!isset($groupAttributesByGroupId[$groupAttribute->group_id])) {
                 $groupAttributesByGroupId[$groupAttribute->group_id] = [];
             }
             $groupAttributesByGroupId[$groupAttribute->group_id][] = $groupAttribute;
         }
         foreach ($groups as $pageGroup) {
             fputcsv($groupsCsv, [$pageGroup->id, $pageGroup->name, $pageGroup->item_name, $pageGroup->url_priority, !empty($templatesById[$pageGroup->default_template]) ? $templatesById[$pageGroup->default_template] : '']);
             foreach ($groupAttributesByGroupId as $groupId => $attributes) {
                 foreach ($attributes as $attribute) {
                     fputcsv($groupAttributesCsv, [$attribute->id, $groupId, Block::preload($attribute->item_block_id)->name, $attribute->item_block_order_priority, $attribute->item_block_order_dir, $attribute->filter_by_block_id ?: '']);
                 }
             }
         }
         fclose($groupsCsv);
         fclose($groupAttributesCsv);
     }
     // export page block data
     fputcsv($pageBlocksCsv, ['Page Id', 'Block Name', 'Content']);
     $blocksById = [];
     $blocksByName = [];
     foreach (BlockUpdater::getDatabaseBlocks($theme->id) as $block) {
         $blocksById[$block->id] = $block;
         $blocksByName[$block->name] = $block;
     }
     $blockTemplatesById = [];
     $templateBlocks = TemplateBlock::whereIn('template_id', $templatesByName)->get();
     if (!$templateBlocks->isEmpty()) {
         foreach ($templateBlocks as $templateBlock) {
             if (!isset($blockTemplatesById[$templateBlock->template_id])) {
                 $blockTemplatesById[$templateBlock->template_id] = [];
             }
             $blockTemplatesById[$templateBlock->template_id][] = $templateBlock->block_id;
         }
     }
     $themeBlocks = ThemeBlock::where('theme_id', '=', $theme->id)->where('show_in_pages', '=', 1)->get();
     if (!$themeBlocks->isEmpty()) {
         foreach ($themeBlocks as $themeBlock) {
             $ignoreTemplates = explode(',', $themeBlock->exclude_templates);
             foreach ($templatesByName as $templateById) {
                 if (!in_array($templateById, $ignoreTemplates)) {
                     if (!isset($blockTemplatesById[$templateById])) {
                         $blockTemplatesById[$templateById] = [];
                     }
                     $blockTemplatesById[$templateById][] = $themeBlock->block_id;
                 }
             }
         }
     }
     $pageBlocks = collect(array_merge(Block::getDataForVersion(new PageBlock(), 0)->all(), Block::getDataForVersion(new PageBlockDefault(), 0)->all()));
     $repeaterBlocks = [];
     $pageBlockArr = [];
     foreach ($pageBlocks as $pageBlock) {
         $blockName = !empty($blocksById[$pageBlock->block_id]) ? $blocksById[$pageBlock->block_id]->name : null;
         if (!empty($blockName) && !empty($pageBlock->content)) {
             // don't add data for blocks that aren't set as template blocks even if data exists (sort of a cleanup on export)
             if (isset($pageBlock->page_id)) {
                 if (empty($pagesData[$pageBlock->page_id]->template) || empty($blockTemplatesById[$pagesData[$pageBlock->page_id]->template])) {
                     continue;
                 } elseif (!in_array($pageBlock->block_id, $blockTemplatesById[$pagesData[$pageBlock->page_id]->template])) {
                     continue;
                 }
             }
             if (strtolower($blocksById[$pageBlock->block_id]->type) == 'repeater') {
                 $repeaterBlocks[$pageBlock->content] = PageBlockRepeaterData::loadRepeaterData($pageBlock->content);
                 $repeaterBlockArr[] = $pageBlock->block_id;
             }
             $filesUsed = $blocksById[$pageBlock->block_id]->getTypeObject()->exportFiles($pageBlock->content);
             $allFilesUsed = array_merge($filesUsed, $allFilesUsed);
             $pageBlockArr[] = [isset($pageBlock->page_id) ? $pageBlock->page_id : 0, $blockName, $pageBlock->content];
         }
     }
     usort($pageBlockArr, function ($a, $b) {
         if ($a[0] == $b[0]) {
             return strcmp($a[1], $b[1]);
         }
         return $a[0] < $b[0] ? -1 : 1;
     });
     foreach ($pageBlockArr as $pageBlock) {
         fputcsv($pageBlocksCsv, $pageBlock);
     }
     fputcsv($repeaterBlocksCsv, ['Repeater Id', 'Repeater Row', 'Block Name', 'Content']);
     ksort($repeaterBlocks);
     foreach ($repeaterBlocks as $repeaterId => $repeaterRows) {
         foreach ($repeaterRows as $repeaterRowId => $repeaterBlocks) {
             foreach ($repeaterBlocks as $repeaterBlockId => $repeaterContent) {
                 $blockName = !empty($blocksById[$repeaterBlockId]) ? $blocksById[$repeaterBlockId]->name : null;
                 if (!empty($blockName) && $repeaterContent) {
                     $filesUsed = $blocksById[$repeaterBlockId]->getTypeObject()->exportFiles($repeaterContent);
                     $allFilesUsed = array_merge($filesUsed, $allFilesUsed);
                     fputcsv($repeaterBlocksCsv, [$repeaterId, $repeaterRowId, $blockName, $repeaterContent]);
                 }
             }
         }
     }
     // backup uploaded files
     $securePaths = [];
     $secureUploadPaths = explode(',', config('coaster::site.secure_folders'));
     foreach ($secureUploadPaths as $secureUploadPath) {
         $securePaths[] = '/uploads/' . trim($secureUploadPath, '/');
     }
     $allFilesUsed = array_unique($allFilesUsed);
     if (!empty($allFilesUsed)) {
         foreach ($allFilesUsed as $file) {
             $base_path = public_path();
             foreach ($securePaths as $securePath) {
                 if (strpos($file, $securePath) === 0) {
                     $base_path = storage_path();
                     break;
                 }
             }
             if (file_exists($base_path . $file)) {
                 self::$_uploadsToAdd[trim($file, '/')] = $base_path . $file;
             }
         }
     }
     fclose($menuItemsCsv);
     fclose($menusCsv);
     fclose($pagesCsv);
     fclose($pageBlocksCsv);
     fclose($repeaterBlocksCsv);
 }
Exemplo n.º 4
0
 public function createPost($data)
 {
     $pageLang = PageLang::where('name', '=', $data->title->rendered)->first();
     $uporc = 'updated';
     if (empty($pageLang)) {
         $uporc = 'created';
         $page = new Page();
         $pageLang = new PageLang();
     } else {
         $page = Page::find($pageLang->page_id);
         $comments = $this->getComments($data, $page);
         $latestVersion = PageVersion::latest_version($page->id, true);
         if (!empty($latestVersion)) {
             $latestVersion->publish();
         }
         if (!empty($data->yoast)) {
             $this->getMetas($data->yoast, $data, $page->id);
         }
         $res = new \stdClass();
         $res->message = 'Post ' . $uporc . ': ' . $pageLang->name;
         $res->oldLink = $data->link;
         $res->newLink = Path::getFullUrl($page->id);
         $res->categories = 'UPDATE RUN';
         $res->tags = 'UPDATE RUN';
         return $res;
     }
     $page->live = 2;
     $page->live_start = $this->carbonDate($data->date)->format("Y-m-d H:i:s");
     $page->created_at = $this->carbonDate($data->date);
     $page->updated_at = $this->carbonDate($data->modified);
     $page->parent = $this->groupPage->id;
     $page->template = $this->group->default_template;
     $page->save();
     $page->groups()->sync([$this->group->id]);
     $comments = $this->getComments($data, $page);
     $categories = $this->getCategory($data->_embedded->{"wp:term"}, $page->id);
     // Page Lang
     $pageLang->live_version = 0;
     $pageLang->page_id = $page->id;
     $pageLang->language_id = Language::current();
     $pageLang->name = $data->title->rendered;
     $pageLang->url = str_slug($pageLang->name);
     $pageLang->save();
     $tags = $this->syncTags($page, $data->_embedded->{"wp:term"});
     $date_block = Block::where('name', '=', 'post_date')->first();
     if (!empty($date_block)) {
         $date_block->setPageId($page->id)->getTypeObject()->save($this->carbonDate($data->date)->format("Y-m-d H:i:s"));
     }
     $title_block = Block::where('name', '=', config('coaster::admin.title_block'))->first();
     if (!empty($title_block)) {
         $title_block->setPageId($page->id)->getTypeObject()->save($pageLang->name);
     }
     $content_block = Block::where('name', '=', 'content')->first();
     if (!empty($content_block)) {
         $content_block->setPageId($page->id)->getTypeObject()->save($this->processContent($data->content->rendered));
     }
     $leadText_block = Block::where('name', '=', 'lead_text')->first();
     if (!empty($leadText_block)) {
         $leadText_block->setPageId($page->id)->getTypeObject()->save($data->excerpt->rendered);
     }
     $latestVersion = PageVersion::latest_version($page->id, true);
     if (!empty($latestVersion)) {
         $latestVersion->publish();
     }
     if (!empty($data->yoast)) {
         $this->getMetas($data->yoast, $data, $page->id);
     }
     $res = new \stdClass();
     $res->message = 'Post ' . $uporc . ': ' . $pageLang->name;
     $res->oldLink = $data->link;
     $res->newLink = Path::getFullUrl($page->id);
     $res->categories = $categories;
     $res->tags = $tags;
     return $res;
 }