/** * Inserts the menu links related to the inserted content, * into the w3sMenuElements table * * @return bool * */ protected function setDefaultRelatedElements() { $bRollBack = false; $con = Propel::getConnection(); $con = w3sPropelWorkaround::beginTransaction($con); for ($i = 1; $i < 4; $i++) { $newMenu = new W3sMenuElement(); $contentValues = array("ContentId" => $this->content->getId(), "PageId" => 0, "Link" => w3sCommonFunctions::toI18n('This is a link'), "ExternalLink" => '', "Image" => '', "RolloverImage" => '', "Position" => $i); $newMenu->fromArray($contentValues); $result = $newMenu->save(); if ($newMenu->isModified() && $result == 0) { $bRollBack = true; break; } } if (!$bRollBack) { // Everything was fine so W3StudioCMS commits to database $con->commit(); $result = true; } else { // Something was wrong so W3StudioCMS aborts the operation and restores to previous status w3sPropelWorkaround::rollBack($con); $result = false; } }
/** * Checks if requested page if free. If everything is fine sets it as active * * @param int The id of the user * @param str The operation requested * @param str The previous operation * @return bool 0 - An errour occoured during saving process * 1 - The page has been setted as active page * 2 - The page is in use by another user */ public static function setRequestedOperation($idUser, $operation, $prevOperation = '', $maxInactiveTime = 300) { if ($idUser == null) { throw new InvalidArgumentException('idUser cannot be null'); } // Checks the status of used pages self::checkInactiveOperations($maxInactiveTime); $con = Propel::getConnection(); $bCommit = true; $con = w3sPropelWorkaround::beginTransaction($con); // Verifies if the requested operation can be made if (self::isRequestedFree($idUser, $operation)) { // Removes the previous operation if exists if ($prevOperation != '') { $bCommit = self::deleteOperation($idUser, $prevOperation) != 0 ? true : false; } // Sets the requested page as active page and returns the operation result if ($bCommit) { $bCommit = self::saveRequestedOperation($idUser, $operation); } } else { $bCommit = false; } if ($bCommit) { $con->commit(); $result = true; } else { w3sPropelWorkaround::rollBack($con); $result = false; } return $result; }
/** * Copies the menu links for the sourceContent to the targetContent * * @parameter int The id of the source content * @parameter int The id of the target content * * @return bool false - The save operation has failed * true - The save operation has correctly done */ public static function copyRelatedElements($sourceContent, $targetContent) { $bRollBack = false; $con = Propel::getConnection(); $con = w3sPropelWorkaround::beginTransaction($con); // Deletes all the target menus $targetMenus = W3sMenuElementPeer::getContentMenu($targetContent); foreach ($targetMenus as $targetMenu) { $targetMenu->delete(); } // Retrieves the menu rows related to source content $sourceMenus = DbFinder::from('W3sMenuElement')->where('contentId', $sourceContent)->orderBy('position')->find(); foreach ($sourceMenus as $sourceMenu) { $oTargetMenu = new W3sMenuElement(); $contentValues = array("ContentId" => $targetContent, "PageId" => $sourceMenu->getPageId(), "Link" => $sourceMenu->getLink(), "ExternalLink" => $sourceMenu->getExternalLink(), "Image" => $sourceMenu->getImage(), "RolloverImage" => $sourceMenu->getRolloverImage(), "Position" => $sourceMenu->getPosition()); $oTargetMenu->fromArray($contentValues); // Saves $result = $oTargetMenu->save(); if ($oTargetMenu->isModified() && $result == 0) { $bRollBack = true; break; } } if (!$bRollBack) { // Everything was fine so W3StudioCMS commits to database $con->commit(); $result = true; } else { // Something was wrong so W3StudioCMS aborts the operation and restores to previous status w3sPropelWorkaround::rollBack($con); $result = false; } return $result; }
/** * Moves the content. * * @param array An array with the id of contents that belongs to the slot * */ public function moveContents($newSlotContents) { $con = Propel::getConnection(); $bRollBack = false; $con = w3sPropelWorkaround::beginTransaction($con); // Builds an array with the contents' id of the current slot before saving // the moving operation $oldSlotContents = array(); foreach ($this->slotContents as $content) { $oldSlotContents[] = $content->getId(); } // Compares the both arrays to find the content to move. There are // three possible cases: // $res1 = 0 and $res2 = 1 - The slot is receiving the content // $res1 = 1 and $res2 = 0 - The slot is losing the content // $res1 = 0 and $res2 = 0 - The slot is the same $res1 = array_diff($oldSlotContents, $newSlotContents); $res2 = array_diff($newSlotContents, $oldSlotContents); if (count($res1) == 0 && count($res2) == 1) { // Here the slot receives the content. This means that the content changes // its slotId. Here is managed this change $content = DbFinder::from('W3sContent')->findPK($this->getArrayValueFromKey($res2)); $contentManager = w3sContentManagerFactory::create($content->getContentTypeId(), $content); $params = array("SlotId" => $this->currentSlotId); $result = $contentManager->edit($params); if ($contentManager->getContent()->isModified() && $result == 0) { $bRollBack = true; } } // Here the slot loses the content. In this case when the new position will be changed // the content that moves will not be involved in the change position, so have to be skipped $currentContentId = count($res1) == 1 && count($res2) == 0 ? $this->getArrayValueFromKey($res1) : null; if (!$bRollBack && !$this->changeContentsPosition($newSlotContents, $currentContentId)) { $bRollBack = true; } if (!$bRollBack) { $con->commit(); $result = 1; } else { w3sPropelWorkaround::rollBack($con); $result = 0; } return $result; }
/** * Tries to make an automation match of the templates slots using their names * * @return bool false - An error occoured * true - Savving success * */ protected function matchSlots() { $con = Propel::getConnection(); $bRollBack = false; $con = w3sPropelWorkaround::beginTransaction($con); // Search the slots of source template $slots = DbFinder::from('W3sSlot')->where('TemplateId', $this->sourceTemplate)->find(); foreach ($slots as $slot) { // Search the same slots on destination template by name $destSlot = DbFinder::from('W3sSlot')->where('TemplateId', $this->destTemplate)->where('SlotName', $slot->getSlotName())->findOne(); if ($destSlot != null) { // Saves the mapping when a match has been found if (!$this->saveMap($slot->getId(), $destSlot->getId())) { $bRollBack = true; break; } } } if (!$bRollBack) { $con->commit(); $result = true; } else { w3sPropelWorkaround::rollBack($con); $result = false; } return $result; }
/** * Deletes the current page 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 */ public function delete($op = 1) { $result = false; if ($this->group != null) { // We assure that all the operations W3StudioCMS makes will be successfully done $con = Propel::getConnection(); $rollBack = false; $con = w3sPropelWorkaround::beginTransaction($con); $this->group->setToDelete($op); $result = $this->group->save(); if ($this->group->isModified() && $result == 0) { $rollBack = true; } else { $rollBack = $this->deleteRelatedPages($op) ? false : true; } if (!$rollBack) { // Everything was fine so W3StudioCMS commits to database $con->commit(); $result = true; } else { // Something was wrong so W3StudioCMS aborts the operation and restores to previous status w3sPropelWorkaround::rollBack($con); $result = false; } } return $result; }
public function executeChangeRepeatedContents($request) { if ($request->hasParameter('page') && $request->hasParameter('lang') && $request->hasParameter('slotName') && $request->hasParameter('newRepeatedValue')) { $bRollBack = false; $con = Propel::getConnection(); $con = w3sPropelWorkaround::beginTransaction($con); $slots = DbFinder::from('W3sSlot')->where('SlotName', $this->getRequestParameter('slotName'))->find(); foreach ($slots as $slot) { $slot->setRepeatedContents($this->getRequestParameter('newRepeatedValue')); $result = $slot->save(); if ($slot->isModified() && $result == 0) { $bRollBack = true; break; } } if (!$bRollBack) { // Everything was fine so W3StudioCMS commits to database $con->commit(); $this->idLanguage = $this->getRequestParameter('lang'); $this->idPage = $this->getRequestParameter('page'); $this->forward('controlPanel', 'drawSlots'); } else { // Something was wrong so W3StudioCMS aborts the operation and restores to previous status w3sPropelWorkaround::rollBack($con); $this->result = 0; $this->getResponse()->setStatusCode(404); return sfView::ERROR; } } else { $this->result = 2; $this->getResponse()->setStatusCode(404); return sfView::ERROR; } }
/** * Makes a space between contents, moving their positions of one unit * up or down, according to the op param * * @param array An array with the following keys: * array("PageId" => value, * "SlotId" => value, * "LanguageId" => value, * "ContentPosition" => value) * @param str The operation to do. Permitted values are "add - del" * * @return bool * */ protected function makeSpace($params, $op) { // Checks the $params parameter. If doesn't match, throwns and exception $required = array("PageId", "SlotId", "LanguageId", "ContentPosition"); if ($diff = array_diff($required, array_keys($params))) { throw new RuntimeException(sprintf('The variable $params requires the following options: \'%s\'.', implode('\', \'', $required))); } // Checks the $op parameter. If doesn't match, throwns and exception $required = array("add", "del"); if (!in_array($op, $required)) { throw new RuntimeException(sprintf('The variable $op requires the following options: \'%s\'.', implode('\', \'', $required))); } $bRollBack = false; $con = Propel::getConnection(); $con = w3sPropelWorkaround::beginTransaction($con); // Retrieves all the contents placed below the current content $contents = DbFinder::from('W3sContent')->where('LanguageId', $params["LanguageId"])->where('PageId', $params["PageId"])->where('SlotId', $params["SlotId"])->where('ContentPosition', '>=', $params["ContentPosition"])->where('ToDelete', 0)->orderBy('ContentPosition')->find(); // If there are no contents, the user is modifing the last content if (count($contents)) { foreach ($contents as $content) { /* When user is adding a content W3StudioCMS needs to create a space between the position * of the over content and the position of the bottom content, to give the new content * that place. This will be made increasing by 1 the positions for all contents placed * below the content to add. * * If user is deleting the content, this creates an empty space between the contents' * positions. W3StudioCMS will decrement by 1 the positions of the contents below the * one the user have deleted */ $newPosition = $op == 'add' ? $content->getContentPosition() + 1 : $content->getContentPosition() - 1; $content->setContentPosition($newPosition); $result = $content->save(); if ($content->isModified() && $result == 0) { $bRollBack = true; break; } } } if (!$bRollBack) { $con->commit(); $result = true; } else { w3sPropelWorkaround::rollBack($con); $result = false; } return $result; }
public function saveLinks($params) { $con = Propel::getConnection(); $bRollBack = false; $con = w3sPropelWorkaround::beginTransaction($con); $position = 1; foreach ($params as $key => $menuValues) { if (is_array($menuValues)) { $oMenu = DbFinder::from('W3sMenuElement')->findPK($key); if ($oMenu != null) { foreach ($menuValues as $menuValue) { $values = explode("=", $menuValue); switch ($values[0]) { case 'w3s_ppt_link': $oMenu->setLink($values[1]); break; case 'w3s_ppt_image': $oMenu->setImage($values[1]); break; case 'w3s_ppt_rollover_image': $oMenu->setRolloverImage($values[1]); break; case 'w3s_ppt_int_link': $oMenu->setPageId($values[1]); break; case 'w3s_ppt_ext_link': $oMenu->setExternalLink($values[1]); break; } } $oMenu->setPosition($position); $result = $oMenu->save(); if ($oMenu->isModified() && $result == 0) { $bRollBack = true; break; } $position++; } } } if (!$bRollBack) { // 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; } }
/** * Adds the theme to database after it has been loaded to make it available to be used * * @author Giansimon Diblas <*****@*****.**> * * @return Bool - True: success - False: failure * */ public function add($themeName) { $bRollBack = false; $con = Propel::getConnection(); $con = w3sPropelWorkaround::beginTransaction($con); $theme = new W3sProject(); $theme->setProjectName($themeName); $result = $theme->save(); if ($theme->isModified() && $result == 0) { $bRollBack = true; } if (!$bRollBack) { $idTheme = $theme->getId(); $templates = $this->scanTheme($themeName); foreach ($templates as $templateName) { $template = new W3sTemplate(); $template->setProjectId($idTheme); $template->setTemplateName($templateName); $result = $template->save(); if ($template->isModified() && $result == 0) { $bRollBack = true; break; } if (!$bRollBack) { $idTemplate = $template->getId(); $slots = $this->getSlotsFromTemplate($themeName, $templateName); foreach ($slots as $slotName) { $slot = new W3sSlot(); $slot->setTemplateId($idTemplate); $slot->setSlotName($slotName); $result = $slot->save(); if ($slot->isModified() && $result == 0) { $bRollBack = true; break; } } } } } if (!$bRollBack) { // Everything was fine so W3StudioCMS commits to database $con->commit(); $result = true; } else { // Something was wrong so W3StudioCMS aborts the operation and restores to previous status w3sPropelWorkaround::rollBack($con); $result = false; } return $result; }
/** * Deletes from the database all the deleted languages, pages and contents * during the edit phase * * @return bool The operation result */ private function updateDb() { try { $con = Propel::getConnection(); $con = w3sPropelWorkaround::beginTransaction($con); // Deletes the languages $c = new Criteria(); $c->add(W3sLanguagePeer::TO_DELETE, 1); W3sLanguagePeer::doDelete($c); // Deletes the groups $c = new Criteria(); $c->add(W3sGroupPeer::TO_DELETE, 1); W3sGroupPeer::doDelete($c); // Deletes the pages $c = new Criteria(); $c->add(W3sPagePeer::TO_DELETE, 1); W3sPagePeer::doDelete($c); // Deletes the contents $c = new Criteria(); $c->add(W3sContentPeer::TO_DELETE, 1); W3sContentPeer::doDelete($c); $con->commit(); return true; } catch (Exception $e) { w3sPropelWorkaround::rollBack($con); sfContext::getInstance()->getLogger()->info('W3StudioCMS - ' . $this->setExceptionError($e->getMessage())); return false; } }