Esempio n. 1
0
 /**
  * Revert selected Version as last Version.
  *
  * @param int $id restored Version row ID
  * @param string $path restored Version Path
  * @return JObject
  *     revertVersion   reverted Version Number
  *     oldLastVersion  archived last Version Number
  *     newLastVersion  new last Version from reverted Version number
  */
 function revert($id, $path)
 {
     $safePath = $this->_db->quote($path);
     $this->_db->setQuery('SELECT MAX(`id`) FROM `#__joomdoc_file` WHERE `path` = ' . $safePath . ' GROUP BY `path`');
     $cid = $this->_db->loadColumn();
     // the latest file version list
     // get Row with given ID and given Path which isn't last Version - candidate to revert as last Version
     $query = 'SELECT `id`, `version` FROM `#__joomdoc_file` WHERE `id` = ' . (int) $id . ' AND `path` = ' . $safePath;
     if ($cid) {
         $query .= ' AND `id` NOT IN (' . implode(', ', $cid) . ')';
     }
     $this->_db->setQuery($query);
     $candidate = $this->_db->loadObject();
     if (is_null($candidate)) {
         return false;
     }
     // get last Version number
     $query = 'SELECT MAX(`version`) FROM `#__joomdoc_file` WHERE `path` = ' . $safePath;
     $this->_db->setQuery($query);
     $lastVersion = $this->_db->loadResult();
     if (is_null($lastVersion)) {
         return false;
     }
     // full Path of last Version
     $lastVersionPath = JoomDOCFileSystem::getFullPath($path);
     // full Path of revert Version
     $revertVersionPath = JoomDOCFileSystem::getVersionPath($lastVersionPath, $candidate->version, true);
     // full Path to archive last Version
     $archiveVersionPath = JoomDOCFileSystem::getVersionPath($lastVersionPath, $lastVersion, true);
     // move last Version as archive Version
     if (!JFile::move($lastVersionPath, $archiveVersionPath)) {
         return false;
     }
     // copy revert Version as last Version
     if (!JFile::copy($revertVersionPath, $lastVersionPath)) {
         return false;
     }
     // copy revert Version DB Row
     $newLastVersionID = JoomDOCModelList::copyRow('#__joomdoc_file', 'id', $candidate->id);
     // increment new last Version number
     $newLastVersion = $lastVersion + 1;
     $query = 'UPDATE `#__joomdoc_file` SET `version` = ' . $newLastVersion . ', `state` = ' . JOOMDOC_STATE_PUBLISHED . ' WHERE `id` = ' . $newLastVersionID;
     $this->_db->setQuery($query);
     $this->_db->query();
     $output = new JObject();
     $output->revertVersion = $candidate->version;
     $output->oldLastVersion = $lastVersion;
     $output->newLastVersion = $newLastVersion;
     JModelLegacy::getInstance(JOOMDOC_DOCUMENTS, JOOMDOC_MODEL_PREFIX)->flat($path);
     return $output;
 }