/** 
  * Draws the contents' slot when in preview mode.
  * 
  * @param object   A slot object
  * 
  * @return string  The contents that belong to slot formatted as string
  * 
  */
 public function drawSlot($slot)
 {
     $result = '';
     foreach ($slot['contents'] as $content) {
         if ($content != null) {
             $curContent = w3sContentManagerFactory::create($content->getContentTypeId(), $content);
             $result .= $curContent->getDisplayContentForPreviewMode();
         }
     }
     return $result;
 }
 /**
  * Edits the content on the foreign page 
  *  
  * @return int The operation result
  *
  */
 public function updateForeignContent()
 {
     // If the foreign content is empty it means that the template hasn't that slot and
     // assumes that everything was correctly done.
     $result = 1;
     $foreignContent = $this->getForeignContent();
     if ($foreignContent != null) {
         $content = w3sContentManagerFactory::create($this->baseContentAttributes["ContentTypeId"], $foreignContent);
         // Blocks the content's update on foreign page because this operation is alreay called
         // during the update on foreign pages.
         $content->setUpdateForeigns(false);
         $result = $content->delete();
     }
     return $result;
 }
 /**
  * Deletes a content
  *
  */
 public function executeDelete($request)
 {
     if ($request->hasParameter('idContent')) {
         $currentContent = DbFinder::from('W3sContent')->findPK($request->getParameter('idContent'));
         $this->content = w3sContentManagerFactory::create($currentContent->getContentTypeId(), $currentContent);
         $result = $this->content->delete();
         if ($result != 1) {
             $this->getResponse()->setStatusCode(404);
             return $this->renderText($this->content->displayError($result, true));
         }
     } else {
         $this->getResponse()->setStatusCode(404);
         return $this->renderText(w3sCommonFunctions::toI18n('A required parameter misses.'));
     }
 }
 /**
  * Adds the content on the foreign page 
  *  
  * @return int The operation result
  *
  */
 public function updateForeignContent()
 {
     $content = w3sContentManagerFactory::create($this->baseContentAttributes["ContentTypeId"]);
     // Blocks the content's update on foreign page because this operation is alreay called
     // during the update on foreign pages.
     $content->setUpdateForeigns(false);
     $foreignContent = $this->getForeignContent();
     if ($foreignContent == null) {
         $param = $this->baseContentAttributes;
         $param["PageId"] = $this->targetPageId;
     } else {
         $param = $foreignContent;
     }
     return $content->add($param);
 }
 /**
  * Edits the content on the foreign page 
  *  
  * @return int The operation result
  *
  */
 public function updateForeignContent()
 {
     // If the foreign content is empty it means that the template hasn't that slot and
     // assumes that everything was correctly done.
     $result = 1;
     $foreignContent = $this->getForeignContent();
     if ($foreignContent != null) {
         $content = w3sContentManagerFactory::create($this->baseContentAttributes["ContentTypeId"], $foreignContent);
         // First copies the related elements for the new content. This opearation
         // must be made before the content's edit
         w3sContentManagerMenuPeer::copyRelatedElements($this->baseContentAttributes["Id"], $foreignContent->getId());
         // Blocks the content's update on foreign page because this operation is alreay called
         // during the update on foreign pages.
         $content->setUpdateForeigns(false);
         $result = $content->edit($this->contentValues);
     }
     return $result;
 }
 /**
  * Edit the menu on the foreign page 
  *  
  * @return int The operation result
  *
  */
 public function updateForeignContent($params = array())
 {
     // If the foreign content is empty it means that the template hasn't that slot and
     // assumes that everything was correctly done.
     $result = 1;
     $foreignContent = $this->getForeignContent();
     //if($this->targetPageId== 98) print_r($foreignContent);
     if ($foreignContent != null) {
         $content = w3sContentManagerFactory::create(5, $foreignContent);
         // Copies the related contents from the base content to the current foreign content
         if ($foreignContent->getId() != $this->baseContentAttributes["Id"]) {
             w3sContentManagerMenuPeer::copyRelatedElements($this->baseContentAttributes["Id"], $foreignContent->getId());
         }
         // Blocks the content's update on foreign page because this operation is alreay called
         // during the update on foreign pages.
         $content->setUpdateForeigns(false);
         $result = $content->edit(array("Content" => $params, "ContentTypeId" => "5"));
         if ($result != 1) {
             break;
         }
     }
     return $result;
 }
 /**
  * Overrides the default content's edit action 
  */
 public function executeEdit($request)
 {
     if ($request->hasParameter('idContent') && $request->hasParameter('content')) {
         $content = DbFinder::from('W3sContent')->findPK($this->getRequestParameter('idContent'));
         if (is_object($content)) {
             if ($content->getContentTypeId() == 5) {
                 $currentContent = DbFinder::from('W3sContent')->findPK($this->getRequestParameter('idContent'));
                 if (!$request->hasParameter('setClassToPages') || $this->getRequestParameter('setClassToPages') == 0) {
                     $content = w3sContentManagerFactory::create($currentContent->getContentTypeId(), $currentContent);
                     $result = $content->edit(array('Content' => $this->getRequestParameter('content')));
                 } else {
                     $content = w3sContentManagerFactory::create($currentContent->getContentTypeId(), $currentContent);
                     $menu = new w3sGroupForeignContentsMenu($content);
                     $result = $menu->update($this->getRequestParameter('content'));
                 }
                 if ($result == 1) {
                     $currentContent = DbFinder::from('W3sContent')->findPK($this->getRequestParameter('idContent'));
                     $content = w3sContentManagerFactory::create($currentContent->getContentTypeId(), $currentContent);
                     return $this->renderText($content->getDisplayContentForEditorMode());
                 } else {
                     $this->getResponse()->setStatusCode(404);
                     return $this->renderText($content->displayError($result, true));
                 }
             } else {
                 $this->getResponse()->setStatusCode(404);
                 return $this->renderText('The content you tried to edit is not a valid navigation menu.');
             }
         } else {
             $this->getResponse()->setStatusCode(404);
             return $this->renderText('The content you tried to edit doesn\'t exist anymore.');
         }
     } else {
         $this->getResponse()->setStatusCode(404);
         return $this->renderText('A required parameter misses.');
     }
 }
 /**
  * Changes the content's position.
  * 
  * @param array An array with the id of contents that belongs to the new slot
  * @param int optional The content that is leaving the slot. When null the condition
  * 						is skipped
  * 
  */
 protected function changeContentsPosition($newSlotContents, $leavingContentId = null)
 {
     $bResult = true;
     $position = 1;
     foreach ($newSlotContents as $contentId) {
         // Skips the content position change when the slot loses the content.
         // In the other two cases, this operation is always performed
         if ($leavingContentId == null || $contentId != $leavingContentId) {
             $content = DbFinder::from('W3sContent')->findPK($contentId);
             $contentManager = w3sContentManagerFactory::create($content->getContentTypeId(), $content);
             $params = array("ContentPosition" => $position);
             $result = $contentManager->edit($params);
             if ($contentManager->getContent()->isModified() && $result == 0) {
                 $bResult = false;
                 break;
             }
             $contentManager = null;
             $content = null;
             $position++;
         }
     }
     return $bResult;
 }
 /**
  * Deletes the language's contents that belongs to the current language object 
  *
  * @param  int The value related to the operation to perform.
  *                 0 - Restore content
  * 								 1 - Delete content 
  * 
  * @return bool false - The save operation failed
  *              true  - Operation success
  */
 protected function deleteRelatedContents($op = 1)
 {
     $result = true;
     $contents = $this->getReleatedContents($this->language);
     foreach ($contents as $content) {
         $contentManager = w3sContentManagerFactory::create($content->getContentTypeId(), $content);
         $contentManager->setUpdateForeigns(false);
         $result = $contentManager->delete($op);
         if ($result != 1) {
             $bRollBack = false;
             break;
         }
     }
     return $result;
 }
 /**
  * Adds a page and inserts default contents where content is available
  *
  * @param  string The name of the page to save
  * @param  int The id of the related group
  * 
  * @return int 0 - The save operation failed
  *             1 - Success
  *             2 - Page already exists
  */
 public function add($pageName, $idGroup)
 {
     try {
         $pageName = trim($pageName);
         if ($pageName != '') {
             // Checks if page already exists
             $pageName = w3sCommonFunctions::slugify($pageName);
             $oPage = W3sPagePeer::getFromPageName($pageName);
             if ($oPage == null) {
                 if ($idGroup != null) {
                     if (DbFinder::from('W3sGroup')->findPK($idGroup) != null) {
                         $con = Propel::getConnection();
                         // We assure that all the operations W3StudioCMS makes will be successfully done
                         $rollBack = false;
                         $con = w3sPropelWorkaround::beginTransaction($con);
                         // Save page
                         $this->page->setGroupId($idGroup);
                         $this->page->setPageName($pageName);
                         $result = $this->page->save();
                         if ($this->page->isModified() && $result == 0) {
                             $rollBack = true;
                         } else {
                             // Getting the page's id inserted yet
                             $idPage = $this->page->getId();
                             $attributes = w3sTemplateEngine::retrieveTemplateAttributesFromPage($this->page);
                             $templateContents = w3sCommonFunctions::readFileContents(sprintf("%1\$s%2\$s%3\$s%2\$sdata%2\$s%4\$s.yml", sfConfig::get('app_w3s_web_themes_dir'), DIRECTORY_SEPARATOR, $attributes["projectName"], $attributes["templateName"]));
                             $defaultContents = sfYaml::load($templateContents);
                             $defaultContents = $defaultContents["W3sContent"];
                             // Inserts the contents for every language
                             $oLanguages = DbFinder::from('W3sLanguage')->where('ToDelete', '0')->find();
                             foreach ($oLanguages as $language) {
                                 $idLanguage = $language->getId();
                                 // Cycles the slot's three repeated contents status
                                 for ($i = 0; $i <= 2; $i++) {
                                     // Retrieve the slots with the $i status for the page's template
                                     $slots = W3sSlotPeer::getTemplateSlots($attributes["idTemplate"], $i);
                                     foreach ($slots as $slot) {
                                         // Retrieves a content that belongs to the current slot
                                         $baseContent = DbFinder::from('W3sContent')->where('SlotId', $slot->getId())->where('LanguageId', $idLanguage)->findOne();
                                         // When no repeated content, the content is simply copied
                                         if ($slot->getRepeatedContents() == 0 || $baseContent == null) {
                                             $slotName = $slot->getSlotName();
                                             foreach ($defaultContents as $defaultContent) {
                                                 if ($defaultContent['slotId'] == $slotName) {
                                                     $content = w3sContentManagerFactory::create($defaultContent["contentTypeId"]);
                                                     $contentValue = array("PageId" => $idPage, "SlotId" => $slot->getId(), "LanguageId" => $idLanguage, "GroupId" => $idGroup, "Content" => $defaultContent["content"], "ContentTypeId" => $defaultContent["contentTypeId"], "ContentPosition" => $defaultContent["contentPosition"], "Edited" => 1);
                                                     $content->setUpdateForeigns(false);
                                                     $content->add($contentValue);
                                                 }
                                             }
                                         } else {
                                             // Retrive base page, if any, but NOT current one
                                             $pageFinder = DbFinder::from('W3sPage')->whereToDelete(0)->whereIdNot($idPage);
                                             if ($basePage = $pageFinder->findOne()) {
                                                 // Retrieve a content that belongs to the current slot
                                                 $baseContents = DbFinder::from('W3sContent')->whereToDelete(0)->relatedTo($basePage)->relatedTo($slot)->whereLanguageId($idLanguage)->find();
                                                 foreach ($baseContents as $baseContent) {
                                                     // Creates a new content from the base content
                                                     $newContent = w3sContentManagerFactory::create($baseContent->getContentTypeId(), $baseContent);
                                                     // Converts the content in array and changes the page's id and the group's id
                                                     $contentValue = W3sContentPeer::contentToArray($newContent->getContent());
                                                     $contentValue["PageId"] = $idPage;
                                                     $contentValue["GroupId"] = $idGroup;
                                                     // Updates the content and copies the related elements
                                                     $newContent->setUpdateForeigns(false);
                                                     $newContent->add($contentValue);
                                                     w3sContentManagerMenuPeer::copyRelatedElements($baseContent->getId(), $newContent->getContent()->getId());
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         if (!$rollBack) {
                             // Everything was fine so W3StudioCMS commits to database
                             $con->commit();
                             $result = 1;
                         } else {
                             // Something was wrong so W3StudioCMS aborts the operation and restores to previous status
                             w3sPropelWorkaround::rollBack($con);
                             $result = 0;
                         }
                     } else {
                         $result = 16;
                     }
                 } else {
                     $result = 8;
                 }
             } else {
                 $result = 2;
             }
         } else {
             $result = 4;
         }
     } catch (Exception $e) {
         w3sPropelWorkaround::rollBack($con);
         $result = 0;
         sfContext::getInstance()->getLogger()->info("W3StudioCMS - An Error occoured while addin a language. The exception message is:\n" . $e->getMessage() . "\n");
     }
     return $result;
 }
 /** 
  * Draws the contents' slot when in editor contents. 
  * 
  * @param object   A slot object
  * 
  * @return string  The contents that belong to slot formatted as string
  * 
  */
 public function drawSlot($slot)
 {
     $result = '';
     $validParam = true;
     $defaultParams = array('0' => 'contents', '1' => 'idSlot', '2' => 'slotName', '3' => 'isRepeated', '4' => 'setEventForRedraw');
     if (is_array($slot)) {
         if (array_diff(array_keys($slot), $defaultParams)) {
             $validParam = false;
         } else {
             if ($slot['contents'][0] != null && !$slot['contents'][0] instanceof W3sContent) {
                 throw new RuntimeException('Contents param have to be an instance of W3sContent');
             }
         }
     } else {
         $validParam = false;
     }
     if (!$validParam) {
         throw new RuntimeException(sprintf('DrawSlot requires an array with the following options: %s', array_values($defaultParams)));
     }
     foreach ($slot['contents'] as $content) {
         if ($slot['contents'][0] != null) {
             // Draws the slot with contents
             $curContent = w3sContentManagerFactory::create($content->getContentTypeId(), $content);
             if (isset($slot['setEventForRedraw'])) {
                 $this->interactiveMenuEvents .= sprintf("Event.observe('%s', 'mouseover', InteractiveMenu.show.bind(InteractiveMenu, %s, '%s', %s, %s, '%s', '%s'));\n", 'w3sContentItem_' . $content->getId(), $content->getId(), $content->getContentTypeId(), $content->getSlotId(), $content->getGroupId(), $slot['slotName'], $slot["isRepeated"]);
             } else {
                 $this->interactiveMenuEvents .= sprintf('%s,%s,%s,%s,%s,%s,%s|', 'w3sContentItem_' . $content->getId(), $content->getId(), $content->getContentTypeId(), $content->getSlotId(), $content->getGroupId(), $slot['slotName'], $slot["isRepeated"]);
             }
             $result .= sprintf('<div id="%s">%s</div>', 'w3sContentItem_' . $content->getId(), $curContent->getDisplayContentForEditorMode());
         } else {
             // Draws the slot without contents
             if (isset($slot['setEventForRedraw'])) {
                 $this->interactiveMenuEvents .= sprintf("Event.observe('%s', 'mouseover', InteractiveMenu.hide.bind(InteractiveMenu, 0, '0', %s, 0, '%s', '0'));\n", 'w3sContent_0' . $slot['idSlot'], $slot['idSlot'], $slot['slotName']);
             } else {
                 $this->interactiveMenuEvents .= sprintf('%s,0,0,%s,0,%s,0|', 'w3sContent_0' . $slot['idSlot'], $slot['idSlot'], $slot['slotName']);
             }
             $result .= sprintf('<div style="padding:1px;" id="w3sContent_0' . $slot['idSlot'] . '"><img src="%s" style="border:0px;" onclick="InteractiveMenu.openActionsMenuForAddContent(this)" /></div>', sfConfig::get('app_w3s_web_skin_images_dir') . '/structural/button_add_content.gif');
         }
     }
     return $result;
 }