Cahaya CMS community board

Edit Page

Edit Page

Postby Mio » Mon Oct 12, 2009 8:55 am

Version 1.0.1
Saving page HTML layout It's show Ok. But after refresh return to default layout.
In version 1.0 it's not hapened
Mio
 
Posts: 42
Joined: Sat Oct 10, 2009 11:05 am

Re: Edit Page

Postby Matej Šircelj » Mon Oct 12, 2009 10:53 am

Hi and thanks again!

This patch will also be included in 1.0.2

replace in application folder modules->admin->actionHelpers->savePageLayout.php with:

Code: Select all
<?php
/**
* Copyright (c) 2009 S-Media (http://www.s-media.si)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @copyright   2009 S-Media (http://www.s-media.si)
* @package      Cahaya CMS
* @version    1.0.2
* @author      Matej Šircelj
* @link      http://www.cahaya-project.org
*
*/

class admin_actionHelpers_SavePageLayout extends Zend_Controller_Action_Helper_Abstract{
   public function init(){
      $pageId = $_POST['pageId'];
      
      if(isset($_POST['changeAll'])){
         $changeAll = $_POST['changeAll'];
      }else{
         $changeAll = false;
      }
      
      
      $pages = new Pages();
      $pageData = $pages->getPageById($pageId)->toArray();

      $pagesLayouts = new PagesLayouts();
      
      $replaceHtml = "<!-- layout start -->\n".$_POST['html']."\n<!-- layout end -->";
      $newHtml = eregi_replace('<!-- layout start -->(.*)<!-- layout end -->', $replaceHtml, $pagesLayouts->getPageLayoutPhtmlById($pageData['layout']));

      if($changeAll == 'true'){
         $pages->updatePageLayout($pageId, $newHtml);
      }else{
         $pages->newPageLayout($pageId, $newHtml);
      }

      echo Zend_Json::encode(array("success" => true));
   }
}


and models->Pages.php with:

Code: Select all
<?php
/**
* Copyright (c) 2009 S-Media (http://www.s-media.si)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @copyright   2009 S-Media (http://www.s-media.si)
* @package      Cahaya CMS
* @version    1.0.2
* @author      Matej Šircelj
* @link      http://www.cahaya-project.org
*
*/

class Pages extends Zend_Db_Table{
   protected $_name = 'pages';
   
   public function getPages(){
      return $this->fetchAll();
   }
   
   public function getPageById($id){
      return $this->fetchRow($this->select()->where('id = ?', $id));
   }
   
   public function getPageData($section, $page, $action){
      $page = $this->fetchRow(
         $this->select()
            ->where('section = ?', $section)
            ->where('page = ?', $page)
            ->where('action = ?', $action)
      );
      
      if(!$page){
         return false;
      }
      
      return $page->toArray();
   }
   
   public function getPageConfig($pageId){
      $pageData = $this->getPageById($pageId)->toArray();
      return Zend_Json::decode($pageData['config']);
   }
   
   public function getPageLayoutById($layoutId){
      $pagesLayouts = new PagesLayouts();
      return $pagesLayouts->getPageLayoutPhtmlById($layoutId);
   }
   
   public function getPageStyleById($styleId){
      $pagesStyles = new PagesStyles();
      return $pagesStyles->getPageStyleCssById($styleId);
   }
   
   public function getPagesBySection($section){
      return $this->fetchAll($this->select()->where('section = ?', $section));
   }
   
   public function getActionsBySectionByPage($section, $page){
      return $this->fetchAll($this->select()->where('section = ?', $section)->where('page = ?', $page));
   }
   
   public function getBlocks($pageId){
      $page = $this->getPageById($pageId);
      $config = Zend_Json::decode($page->blocks);
      
      $blocksData = array();
      
      foreach($config as $placeholderName => $placeholder){
         foreach($placeholder as $block){
            $blocksData[] = $block;
         }
      }
      
      return $blocksData;
   }
   
   public function addPage($pageData){
      if(!$this->getPageData($pageData['section'], $pageData['page'], $pageData['action'])){
         $this->insert($pageData);
         return true;
      }else{
         return false;
      }
   }
   
   public function deletePageById($id){
      $blocks = new Blocks();
      $blocksArray = $this->getBlocks($id);

      // Remove blocks
      foreach($blocksArray as $blockId){
         $blocks->deleteBlock($blockId);
      }
      // Remove page
      $where = $this->getAdapter()->quoteInto('id = ?', $id);
      return $this->delete($where);
   }
   
   public function updateBlocks($pageId, $newBlocks){
      return $this->update(array('blocks' => $newBlocks), 'id = '.$pageId);
   }
   
   public function updatePageConfig($pageId, $config){
      $this->update(array('config' => Zend_Json::encode($config)), 'id = '.$pageId);
   }
   
   public function newPageLayout($pageId, $layout, $name = ''){
      $pagesLayouts = new PagesLayouts();
      $pageLayoutData = array(
         "layout" => $layout,
         "name" => $name,
         "modified" => time()
      );
      $rowId = $pagesLayouts->insert($pageLayoutData);
      $pageData = array(
         'layout' => $rowId
      );
      $this->update($pageData, 'id = '.$pageId);
   }
   
   public function updatePageLayout($id, $phtml){
      $pageData = $this->getPageById($id)->toArray();
      $pagesLayouts = new PagesLayouts();
      $pagesLayouts->savePageLayoutById($pageData['layout'], $phtml);
   }
   
   public function newPageStyle($pageId, $style, $name = ''){
      $pagesStyles = new PagesStyles();
      $pageStyleData = array(
         'name' => $name,
         'style' => $style,
         'modified' => time()
      );
      $rowId = $pagesStyles->insert($pageStyleData);
      
      $pageData = array(
         'style' => $rowId
      );
      $this->update($pageData, 'id = '.$pageId);
   }
   
   public function updatePageStyle($id, $style){
      $pageData = $this->getPageById($id)->toArray();
      $pagesStyles = new PagesStyles();
      $pagesStyles->savePageStyleById($pageData['style'], $style);
   }
   
   public function copyPage($pageId, $newPath){
      $page = $this->getPageById($pageId)->toArray();
      
      $blocks = new Blocks();
      $blocksData = Zend_Json::decode($page['blocks']);

      foreach($blocksData as $placeholder => $blocksIds){
         foreach($blocksIds as $key => $block){
            $blocksData[$placeholder][$key] = $blocks->copyBlock($block);
         }
      }
      
      $page['section'] = $newPath['section'];
      $page['page'] = $newPath['page'];
      $page['action'] = $newPath['action'];
      $page['blocks'] = Zend_Json::encode($blocksData);
      
      unset($page['id']);
      if(!$this->getPageData($newPath['section'], $newPath['page'], $newPath['action'])){
         $this->insert($page);
         return true;
      }else{
         return false;
      }
      
   }
   
   public function createPageFromTemplate($templateId, $path){
      $pagesTemplates = new PagesTemplates();
      $pagesTemplatesLayouts = new PagesTemplatesLayouts();
      $pagesTemplatesStyles = new PagesTemplatesStyles();
      
      $templateData = $pagesTemplates->getPageTemplateById($templateId)->toArray();
      
      // layout
      $layoutTemplateAlreadyInUse = $this->pagesWithThisLayoutTemplateAlreadyExist($templateData['layout']);
      
      if($layoutTemplateAlreadyInUse === false){
         $templateData['layout'] = $pagesTemplatesLayouts->copyTemplateLayoutToPagesLayouts($templateData['layout']);
      }else{
         $templateData['layout'] = $layoutTemplateAlreadyInUse;
      }
      
      // style
      $styleTemplateAlreadyInUse = $this->pagesWithThisStyleTemplateAlreadyExist($templateData['style']);
      
      if($styleTemplateAlreadyInUse === false){
         $templateData['style'] = $pagesTemplatesStyles->copyTemplateStyleToPagesStyles($templateData['style']);
      }else{
         $templateData['style'] = $styleTemplateAlreadyInUse;
      }
      
      $templateConfig = Zend_Json::decode($templateData['blocks']);

      $blocksData = array();
      
      $blocks = new Blocks();
      
      foreach($templateConfig as $placeholder => $blocksIds){
         foreach($blocksIds as $key => $block){
            $blocksData[$placeholder][$key] = $blocks->makeBlockFromTemplate($block);
         }
      }
      
      $pathData = $this->getSectionPageActionFromPath($path);
      
      $pageData = $templateData;
      unset($pageData['id']);
      unset($pageData['name']);
      $pageData['section'] = $pathData['section'];
      $pageData['page'] = $pathData['page'];
      $pageData['action'] = $pathData['action'];
      
      $pageData['blocks'] = Zend_Json::encode($blocksData);
      
      if(!$this->getPageData($pageData['section'], $pageData['page'], $pageData['action'])){
         $this->insert($pageData);
         return true;
      }else{
         return false;
      }
      $this->insert($template);
   }
   
   public function pagesWithThisLayoutTemplateAlreadyExist($templateLayoutId){
      $pagesLayouts = new PagesLayouts();
      $pagesTemplatesLayouts = new PagesTemplatesLayouts();
      
      $pageTemplateLayout = $pagesTemplatesLayouts->getPageTemplateLayoutById($templateLayoutId);
      
      $pagesLayoutsArray = $pagesLayouts->getPagesLayoutsByName($pageTemplateLayout['name'])->toArray();
      
      foreach($pagesLayoutsArray as $pageLayout){
         if($pageLayout['modified'] == $pageTemplateLayout['modified']){
            return $pageLayout['id'];
         }
      }
      return false;
   }
      
   public function pagesWithThisStyleTemplateAlreadyExist($templateStyleId){
      $pagesStyles = new PagesStyles();
      $pagesTemplatesStyles = new PagesTemplatesStyles();
      
      $pageTemplateStyle = $pagesTemplatesStyles->getPageTemplateStyleById($templateStyleId);
      
      $pagesStylesArray = $pagesStyles->getPagesStylesByName($pageTemplateStyle['name'])->toArray();
      
      foreach($pagesStylesArray as $pageStyle){
         if($pageStyle['modified'] == $pageTemplateStyle['modified']){
            return $pageStyle['id'];
         }
      }
      return false;
   }
   
   public function makeTemplate($templateName, $pageId){
      $page = $this->getPageById($pageId)->toArray();
      $blocksData = Zend_Json::decode($page['blocks']);

      $templateBlocks = array();
      
      $blocksTemplates = new BlocksTemplates();
      $blocks = new Blocks();
      
      foreach($blocksData as $placeholderKey => $placeholderValue){
         foreach($placeholderValue as $key => $block){
            $blockData = $blocks->getBlockById($block)->toArray();
            $blockTemplateId = $blocksTemplates->makeTemplateFromBlock($templateName, $blockData);

            $templateBlocks[$placeholderKey][$key] = $blockTemplateId;
         }
      }
      
      $templateData = $page;
      
      unset($templateData['section']);
      unset($templateData['page']);
      unset($templateData['action']);
      
      $templateData['name'] = $templateName;
      $templateData['blocks'] = Zend_Json::encode($templateBlocks);
      
      $templateData['layout'] = $this->getPageLayoutById($page['layout']);
      $templateData['style'] = $this->getPageStyleById($page['style']);
      
      $pagesTemplates = new PagesTemplates();
      $pagesTemplates->createPageTemplate($templateName, $templateData);
   }
   
   public function removeBlock($blockId, $pageId){
      $pageConfig = $this->getPageById($pageId)->toArray();
      $blocksArray = Zend_Json::decode($pageConfig['blocks']);

      foreach($blocksArray as $placeholder => $blocks){
         foreach($blocks as $key => $block){
            if($block == $blockId){
               unset($blocksArray[$placeholder][$key]);
            }
         }
      }
      
      $blocksArray = Zend_Json::encode($blocksArray);
      $this->updateBlocks($pageId, $blocksArray);
   }
   
   public function getSectionPageActionFromPath($path){
      $sections = new Sections();
      
      $pathArray = explode('/',$path);
      $pathArray = array_values(array_filter($pathArray));
      
      $pathData = array();
      if(!$pathArray){
         $pathData['section'] = 'default';
         $pathData['page'] = 'index';
         $pathData['action'] = 'index';
      }else if($sections->getSectionBySectionName($pathArray[0])){
         $pathData['section'] = $pathArray[0];
         
         if(isset($pathArray[1])){
            $pathData['page'] = $pathArray[1];
         }else{
            $pathData['page'] = 'index';
         }
         
         if(isset($pathArray[2])){
            $pathData['action'] = $pathArray[2];
         }else{
            $pathData['action'] = 'index';
         }
      }else{
         $pathData['section'] = 'default';
         
         if(isset($pathArray[0])){
            $pathData['page'] = $pathArray[0];
         }else{
            $pathData['page'] = 'index';
         }
         
         if(isset($pathArray[1])){
            $pathData['action'] = $pathArray[1];
         }else{
            $pathData['action'] = 'index';
         }
      }
      
      return $pathData;
   }
   
   
}
Matej Šircelj
Site Admin
 
Posts: 62
Joined: Wed Aug 19, 2009 12:49 pm

Re: Edit Page

Postby Mio » Mon Oct 12, 2009 11:41 am

It's work.
Mio
 
Posts: 42
Joined: Sat Oct 10, 2009 11:05 am

Re: Edit Page

Postby reno » Mon Feb 22, 2010 3:46 pm

Hello,
I spend some hours trying to understand this issue :D
When will can test the next Cahaya version ?
reno
 
Posts: 6
Joined: Mon Jan 04, 2010 11:32 am
Location: Lyon - France


Return to Bug reports



Who is online

Users browsing this forum: No registered users and 1 guest

cron