/pimcore/models/Version.php

https://github.com/ngocanh/pimcore · PHP · 450 lines · 210 code · 77 blank · 163 comment · 40 complexity · 51db3ddfde9ea3e61836bdb5b1990577 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.pimcore.org/license
  11. *
  12. * @category Pimcore
  13. * @package Version
  14. * @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
  15. * @license http://www.pimcore.org/license New BSD License
  16. */
  17. class Version extends Pimcore_Model_Abstract {
  18. /**
  19. * @var integer
  20. */
  21. public $id;
  22. /**
  23. * @var integer
  24. */
  25. public $cid;
  26. /**
  27. * @var string
  28. */
  29. public $ctype;
  30. /**
  31. * @var integer
  32. */
  33. public $userId;
  34. /**
  35. * @var User
  36. */
  37. public $user;
  38. /**
  39. * @var string
  40. */
  41. public $note;
  42. /**
  43. * @var integer
  44. */
  45. public $date;
  46. /**
  47. * @var mixed
  48. */
  49. public $data;
  50. /**
  51. * @var bool
  52. */
  53. public $public = false;
  54. /**
  55. * @var boolean
  56. */
  57. public $serialized = false;
  58. /**
  59. * @var bool
  60. */
  61. public static $disabled = false;
  62. /**
  63. * @param integer $id
  64. * @return Version
  65. */
  66. public static function getById($id) {
  67. $version = new self();
  68. $version->getResource()->getById($id);
  69. return $version;
  70. }
  71. /**
  72. * disables the versioning for the current process, this is useful for importers, ...
  73. * There are no new versions created, the read continues to operate normally
  74. *
  75. * @static
  76. * @return void
  77. */
  78. public static function disable () {
  79. self::$disabled = true;
  80. }
  81. /**
  82. * see @ self::disable()
  83. * just enabled the creation of versioning in the current process
  84. *
  85. * @static
  86. * @return void
  87. */
  88. public static function enable () {
  89. self::$disabled = false;
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function save() {
  95. // check if versioning is disabled for this process
  96. if(self::$disabled) {
  97. return;
  98. }
  99. if (!$this->date) {
  100. $this->setDate(time());
  101. }
  102. $data = $this->getData();
  103. // if necessary convert the data to save it to filesystem
  104. if (is_object($data) or is_array($data)) {
  105. $this->setSerialized(true);
  106. $data->_fulldump = true;
  107. $dataString = Pimcore_Tool_Serialize::serialize($this->getData());
  108. unset($this->_fulldump);
  109. } else {
  110. $dataString = $data;
  111. }
  112. $this->id = $this->getResource()->save();
  113. // check if directory exists
  114. $saveDir = dirname($this->getFilePath());
  115. if(!is_dir($saveDir)) {
  116. mkdir($saveDir, 0766, true);
  117. }
  118. // save data to filesystem
  119. if(!is_writable(dirname($this->getFilePath())) || (is_file($this->getFilePath()) && !is_writable($this->getFilePath()))) {
  120. throw new Exception("Cannot save version for element " . $this->getCid() . " with type " . $this->getCtype() . " because the file " . $this->getFilePath() . " is not writeable.");
  121. } else {
  122. file_put_contents($this->getFilePath(),$dataString);
  123. }
  124. $this->cleanHistory();
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function delete() {
  130. if(is_file($this->getFilePath())) {
  131. @unlink($this->getFilePath());
  132. }
  133. $this->getResource()->delete();
  134. }
  135. /**
  136. * Object
  137. *
  138. * @return mixed
  139. */
  140. public function loadData() {
  141. if(!is_file($this->getFilePath()) or !is_readable($this->getFilePath())){
  142. Logger::err("Version: cannot read version data from file system.");
  143. $this->delete();
  144. return;
  145. }
  146. $data = file_get_contents($this->getFilePath());
  147. if ($this->getSerialized()) {
  148. $data = Pimcore_Tool_Serialize::unserialize($data);
  149. }
  150. $data = Element_Service::renewReferences($data);
  151. $this->setData($data);
  152. return $data;
  153. }
  154. /**
  155. * Returns the path on the file system
  156. *
  157. * @return string
  158. */
  159. protected function getFilePath() {
  160. return PIMCORE_VERSION_DIRECTORY . "/" . $this->getCtype() . "/" . $this->getId();
  161. }
  162. /**
  163. * @return void
  164. */
  165. public function cleanHistory() {
  166. if ($this->getCtype() == "document") {
  167. $conf = Pimcore_Config::getSystemConfig()->documents->versions;
  168. }
  169. else if ($this->getCtype() == "asset") {
  170. $conf = Pimcore_Config::getSystemConfig()->assets->versions;
  171. }
  172. else if ($this->getCtype() == "object") {
  173. $conf = Pimcore_Config::getSystemConfig()->objects->versions;
  174. }
  175. else {
  176. return;
  177. }
  178. $days = array();
  179. $steps = array();
  180. if (intval($conf->days) > 0) {
  181. $days = $this->getResource()->getOutdatedVersionsDays($conf->days);
  182. }
  183. else {
  184. $steps = $this->getResource()->getOutdatedVersionsSteps(intval($conf->steps));
  185. }
  186. $versions = array_merge($days, $steps);
  187. foreach ($versions as $id) {
  188. $version = Version::getById($id);
  189. $version->delete();
  190. }
  191. }
  192. /**
  193. * @return integer
  194. */
  195. public function getCid() {
  196. return $this->cid;
  197. }
  198. /**
  199. * @return integer
  200. */
  201. public function getDate() {
  202. return $this->date;
  203. }
  204. /**
  205. * @return integer
  206. */
  207. public function getId() {
  208. return $this->id;
  209. }
  210. /**
  211. * @return string
  212. */
  213. public function getNote() {
  214. return $this->note;
  215. }
  216. /**
  217. * @return integer
  218. */
  219. public function getUserId() {
  220. return $this->userId;
  221. }
  222. /**
  223. * @return void
  224. */
  225. public function setCid($cid) {
  226. $this->cid = (int) $cid;
  227. }
  228. /**
  229. * @param integer $date
  230. * @return void
  231. */
  232. public function setDate($date) {
  233. $this->date = (int) $date;
  234. }
  235. /**
  236. * @param integer $id
  237. * @return void
  238. */
  239. public function setId($id) {
  240. $this->id = (int) $id;
  241. }
  242. /**
  243. * @param string $note
  244. * @return void
  245. */
  246. public function setNote($note) {
  247. $this->note = (string) $note;
  248. }
  249. /**
  250. * @param integer $userId
  251. * @return void
  252. */
  253. public function setUserId($userId) {
  254. if (is_numeric($userId)) {
  255. if ($user = User::getById($userId)) {
  256. $this->userId = $userId;
  257. $this->setUser($user);
  258. }
  259. }
  260. }
  261. /**
  262. * @return mixed
  263. */
  264. public function getData() {
  265. if (!$this->data) {
  266. $this->loadData();
  267. }
  268. return $this->data;
  269. }
  270. /**
  271. * @param mixed $data
  272. * @return void
  273. */
  274. public function setData($data) {
  275. $this->data = $data;
  276. }
  277. /**
  278. * @return boolean
  279. */
  280. public function getSerialized() {
  281. return $this->serialized;
  282. }
  283. /**
  284. * @param boolean $serialized
  285. * @return void
  286. */
  287. public function setSerialized($serialized) {
  288. $this->serialized = (bool) $serialized;
  289. }
  290. /**
  291. * @return string
  292. */
  293. public function getCtype() {
  294. return $this->ctype;
  295. }
  296. /**
  297. * @param string $ctype
  298. * @return void
  299. */
  300. public function setCtype($ctype) {
  301. $this->ctype = (string) $ctype;
  302. }
  303. /**
  304. * @return User
  305. */
  306. public function getUser() {
  307. return $this->user;
  308. }
  309. /**
  310. * @param User $user
  311. * @return void
  312. */
  313. public function setUser($user) {
  314. $this->user = $user;
  315. }
  316. /**
  317. * @return bool
  318. */
  319. public function getPublic() {
  320. return $this->public;
  321. }
  322. /**
  323. * @return bool
  324. */
  325. public function isPublic() {
  326. return $this->public;
  327. }
  328. /**
  329. * @param bool $public
  330. * @return void
  331. */
  332. public function setPublic($public) {
  333. $this->public = (bool) $public;
  334. }
  335. public function maintenanceCleanUp () {
  336. $conf["document"] = Pimcore_Config::getSystemConfig()->documents->versions;
  337. $conf["asset"] = Pimcore_Config::getSystemConfig()->assets->versions;
  338. $conf["object"] = Pimcore_Config::getSystemConfig()->objects->versions;
  339. $types = array();
  340. foreach ($conf as $type => $tConf) {
  341. if (intval($tConf->days) > 0) {
  342. $types[] = array(
  343. "type" => $type,
  344. "days" => intval($tConf->days)
  345. );
  346. }
  347. }
  348. $versions = $this->getResource()->maintenanceGetOutdatedVersions($types);
  349. if(is_array($versions)) {
  350. foreach ($versions as $id) {
  351. $version = Version::getById($id);
  352. if ($version->getCtype() == "document") {
  353. $element = Document::getById($version->getCid());
  354. }
  355. else if ($version->getCtype() == "asset") {
  356. $element = Asset::getById($version->getCid());
  357. }
  358. else if ($version->getCtype() == "object") {
  359. $element = Object_Abstract::getById($version->getCid());
  360. }
  361. if($element instanceof Element_Interface) {
  362. if($element->getModificationDate() > $version->getDate()) {
  363. // delete version if it is outdated
  364. $version->delete();
  365. }
  366. } else {
  367. // delete version if the correspondening element doesn't exist anymore
  368. $version->delete();
  369. }
  370. }
  371. }
  372. }
  373. }