/** * Synchronizes an object with the database * @param $object object to be synchronized. It should already be persistent (had its id assigned) */ function update($object) { // Set updated and updatedBy $isoDateFormat = new IsoDateFormat(); $session = new Session(); $date = Date::getTodayDate(); $user = $session->getSessionUser(); $object->setUpdated($isoDateFormat->toDatetimeString($date)); $object->setUpdatedBy($user->getId()); $persistence = $this->newPersistenceObject(); $persistence->setProperty("ID", $object->getId()); $persistence->setProperty("classID", $object->getClassID()); $persistence->setProperty("created", $object->getCreated()); $persistence->setProperty("createdBy", $object->getCreatedBy()); $persistence->setProperty("endPublishing", $object->getEndPublishing()); $persistence->setProperty("fullTextIndex", $object->getFullTextIndex()); $persistence->setProperty("hits", $object->getHits()); $persistence->setProperty("isPublished", $object->getIsPublished()); $persistence->setProperty("startPublishing", $object->getStartPublishing()); $persistence->setProperty("updated", $object->getUpdated()); $persistence->setProperty("updatedBy", $object->getUpdatedBy()); $persistence->update(); }
/** * Collects the Object sent by HTTP. This method also fills the controller messages with the appropiate errors. * @param $object Object - object to update, if not supplied a new one is created * @return Object - Object collected, it there was problems collecting, it will return null */ function collectObject($object = null) { // Signal -> if true, there was an error, so do not save $save = true; // Get languages $languageMapper = new LanguageMapper(); $languageArray = $languageMapper->getAll(); // Collect data from attributes for each language $frontAttributeArray = array(); $objectAttributeArray = array(); foreach ($languageArray as $language) { $attributeArray = $this->class->getAttributesForLanguage($language); foreach ($attributeArray as $attribute) { // Get the ObjectAttribute if it was already saved (it could be new...) if (is_null($object) == false) { $objectAttribute = $object->getAttributeForLanguage($attribute->getId(), $language->getId()); if ($objectAttribute != null) { $attribute = $objectAttribute; } } // Create his GUI representation $frontAttribute = FrontAttributeFactory::newInstance($language, $attribute); // Collect it $frontAttribute->collectWidget(); if ($frontAttribute->isValid() == false) { $this->frontAttributeNotValidMessage($frontAttribute); $save = false; } array_push($frontAttributeArray, $frontAttribute); array_push($objectAttributeArray, $frontAttribute->toObjectAttribute()); } } // Collect object relations $classRelationArray = $this->class->getClassRelations(); $objectRelationArray = array(); foreach ($classRelationArray as $classRelation) { /* @var $classRelation ClassRelation */ $name = "classRelation" . $classRelation->getId() . "Select"; $valueArray = $_POST[$name]; if (is_null($valueArray) == false && $valueArray != "") { foreach ($valueArray as $value) { $objectRelation = new ObjectRelation(); $objectRelation->setChildId($value); $objectRelation->setPosition($classRelation->getPosition()); array_push($objectRelationArray, $objectRelation); } } else { if ($classRelation->getIsRequired()) { // It was required, notify the user $this->dataNotValidMessage($classRelation->getTitle()); $save = false; } } } // Collect publishing info // Get date format $dateFormat = DateFormatFactory::getDateFormat(); $publishCheckbox = $_REQUEST["publishCheckbox"]; $publish = 0; if ($publishCheckbox == "-1") { $publish = true; } // Only get publishing dates if the user issued publish in true $publishFromDateString = null; $publishToDateString = null; if ($publish == true) { // Get ISO dateFormat for the database $isoDateFormat = new IsoDateFormat(); // Get publishFrom/to checking if they are available first $publishFrom = $_REQUEST["publishFromText"]; if ($publishFrom != null && $publishFrom != "") { $publishFromDate = $dateFormat->parseDate($publishFrom); if ($publishFromDate == null) { $this->dataNotValidMessage($this->text["from"]); $save = false; } else { $publishFromDateString = $isoDateFormat->toDatetimeString($publishFromDate); } } $publishTo = $_REQUEST["publishToText"]; if ($publishTo != null && $publishTo != "") { $publishToDate = $dateFormat->parseDate($publishTo); if ($publishToDate == null) { $this->dataNotValidMessage($this->text["to"]); $save = false; } else { $publishToDateString = $isoDateFormat->toDatetimeString($publishToDate); } } } $hits = $_REQUEST["hitsText"]; // Get tree references $folders = $_REQUEST["folders"]; $objectFolderArray = array(); $folderMapper = new FolderMapper(); if ($folders != null && $folders != "") { $folderIdArray = split(",", $folders); foreach ($folderIdArray as $folderId) { $folder = $folderMapper->get($folderId); /* @var $folder Folder */ $objectFolderPosition = $folder->getNextObjectFolderPosition(); $objectFolder = new ObjectFolder(null, $folderId); $objectFolder->setPosition($objectFolderPosition); array_push($objectFolderArray, $objectFolder); } } if ($save == false) { return null; } // My new object if ($object == null) { $object = new Object(); } $object->setId($this->objectId); $object->setClassID($this->classId); $object->setIsPublished($publish); $object->setStartPublishing($publishFromDateString); $object->setEndPublishing($publishToDateString); $object->setHits($hits); // Attributes $object->setAttributes($objectAttributeArray); // Folders $object->setObjectFolders($objectFolderArray); // Relationships $object->setObjectRelations($objectRelationArray); // Save attached data, if any /* @var $frontAttribute FrontAttribute */ foreach ($frontAttributeArray as $frontAttribute) { $frontAttribute->saveAttachedData(); } // Return the object return $object; }