/pimcore/models/Element/Recyclebin/Item.php

https://github.com/timglabisch/pimcore · PHP · 387 lines · 195 code · 68 blank · 124 comment · 24 complexity · 94fb703acca1c4ec92dcf080d10ebe2f 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 Element
  14. * @copyright Copyright (c) 2009-2013 pimcore GmbH (http://www.pimcore.org)
  15. * @license http://www.pimcore.org/license New BSD License
  16. */
  17. class Element_Recyclebin_Item extends Pimcore_Model_Abstract {
  18. /**
  19. * @var int
  20. */
  21. public $id;
  22. /**
  23. * @var string
  24. */
  25. public $path;
  26. /**
  27. * @var string
  28. */
  29. public $type;
  30. /**
  31. * @var string
  32. */
  33. public $subtype;
  34. /**
  35. * @var int
  36. */
  37. public $amount = 0;
  38. /**
  39. * @var Element_Interface
  40. */
  41. public $element;
  42. /**
  43. * @var int
  44. */
  45. public $date;
  46. /**
  47. * @var string
  48. */
  49. public $deletedby;
  50. /**
  51. * @static
  52. * @param Element_Interface $element
  53. * @param User $user
  54. */
  55. public static function create (Element_Interface $element, User $user) {
  56. $item = new self();
  57. $item->setElement($element);
  58. $item->save($user);
  59. }
  60. /**
  61. * @static
  62. * @param $id
  63. * @return Element_Recyclebin_Item
  64. */
  65. public static function getById ($id) {
  66. $item = new self();
  67. $item->getResource()->getById($id);
  68. return $item;
  69. }
  70. /**
  71. *
  72. */
  73. public function restore () {
  74. $raw = file_get_contents($this->getStoreageFile());
  75. $element = Pimcore_Tool_Serialize::unserialize($raw);
  76. // check for element with the same name
  77. if($element instanceof Document) {
  78. $indentElement = Document::getByPath($element->getFullpath());
  79. if($indentElement) {
  80. $element->setKey($element->getKey()."_restore");
  81. }
  82. }
  83. else if ($element instanceof Asset) {
  84. $indentElement = Asset::getByPath($element->getFullpath());
  85. if($indentElement) {
  86. $element->setFilename($element->getFilename()."_restore");
  87. }
  88. }
  89. else if ($element instanceof Object_Abstract) {
  90. $indentElement = Object_Abstract::getByPath($element->getFullpath());
  91. if($indentElement) {
  92. $element->setKey($element->getKey()."_restore");
  93. }
  94. }
  95. $this->restoreChilds($element);
  96. $this->delete();
  97. }
  98. /**
  99. * @param User $user
  100. * @return void
  101. */
  102. public function save ($user=null) {
  103. if($this->getElement() instanceof Element_Interface) {
  104. $this->setType(Element_Service::getElementType($this->getElement()));
  105. }
  106. $this->setSubtype($this->getElement()->getType());
  107. $this->setPath($this->getElement()->getFullPath());
  108. $this->setDate(time());
  109. $this->loadChilds($this->getElement());
  110. if($user instanceof User){
  111. $this->setDeletedby($user->getName());
  112. }
  113. // serialize data
  114. Element_Service::loadAllFields($this->element);
  115. $this->element->_fulldump = true;
  116. $data = Pimcore_Tool_Serialize::serialize($this->getElement());
  117. $this->getResource()->save();
  118. if(!is_dir(PIMCORE_RECYCLEBIN_DIRECTORY)) {
  119. mkdir(PIMCORE_RECYCLEBIN_DIRECTORY);
  120. }
  121. file_put_contents($this->getStoreageFile(),$data);
  122. $saveBinaryData = function ($element, $rec) {
  123. // assets are kina special because they can contain massive amount of binary data which isn't serialized, we create separate files for them
  124. if($element instanceof Asset) {
  125. if($element->getType() != "folder") {
  126. $handle = fopen($this->getStorageFileBinary($element), "w+");
  127. $src = $element->getStream();
  128. stream_copy_to_stream($src, $handle);
  129. fclose($handle);
  130. }
  131. $children = $element->getChilds();
  132. foreach ($children as $child) {
  133. $rec($child, $rec);
  134. }
  135. }
  136. };
  137. $saveBinaryData($this->getElement(), $saveBinaryData);
  138. chmod($this->getStoreageFile(), 0766);
  139. }
  140. /**
  141. *
  142. */
  143. public function delete () {
  144. unlink($this->getStoreageFile());
  145. // remove binary files
  146. $files = glob(PIMCORE_RECYCLEBIN_DIRECTORY . "/" . $this->getId() . "_*");
  147. if(is_array($files)) {
  148. foreach ($files as $file) {
  149. unlink($file);
  150. }
  151. }
  152. $this->getResource()->delete();
  153. }
  154. /**
  155. * @param Element_Interface $element
  156. */
  157. public function loadChilds (Element_Interface $element) {
  158. $this->amount++;
  159. Element_Service::loadAllFields($element);
  160. // for all
  161. $element->getProperties();
  162. if(method_exists($element,"getScheduledTasks")) {
  163. $element->getScheduledTasks();
  164. }
  165. $element->_fulldump = true;
  166. if(method_exists($element,"getChilds")) {
  167. if($element instanceof Object_Abstract) {
  168. // because we also want variants
  169. $childs = $element->getChilds(array(Object_Abstract::OBJECT_TYPE_FOLDER, Object_Abstract::OBJECT_TYPE_VARIANT, Object_Abstract::OBJECT_TYPE_OBJECT));
  170. } else {
  171. $childs = $element->getChilds();
  172. }
  173. foreach ($childs as $child) {
  174. $this->loadChilds($child);
  175. }
  176. }
  177. }
  178. /**
  179. * @param Element_Interface $element
  180. */
  181. public function restoreChilds (Element_Interface $element) {
  182. $restoreBinaryData = function ($element, $rec) {
  183. // assets are kina special because they can contain massive amount of binary data which isn't serialized, we create separate files for them
  184. if($element instanceof Asset) {
  185. $binFile = $this->getStorageFileBinary($element);
  186. if(file_exists($binFile)) {
  187. $binaryHandle = fopen($binFile, "r+");
  188. $element->setStream($binaryHandle);
  189. }
  190. }
  191. };
  192. $restoreBinaryData($element, $restoreBinaryData);
  193. $element->save();
  194. if(method_exists($element,"getChilds")) {
  195. if($element instanceof Object_Abstract) {
  196. // don't use the getter because this will return an empty array (variants are excluded by default)
  197. $childs = $element->o_childs;
  198. } else {
  199. $childs = $element->getChilds();
  200. }
  201. foreach ($childs as $child) {
  202. $this->restoreChilds($child);
  203. }
  204. }
  205. }
  206. /**
  207. * @return string
  208. */
  209. public function getStoreageFile () {
  210. return PIMCORE_RECYCLEBIN_DIRECTORY . "/" . $this->getId() . ".psf";
  211. }
  212. public function getStorageFileBinary($element) {
  213. return PIMCORE_RECYCLEBIN_DIRECTORY . "/" . $this->getId() . "_" . Element_Service::getElementType($element) . "-" . $element->getId() . ".bin";
  214. }
  215. /**
  216. * @return int
  217. */
  218. public function getId() {
  219. return $this->id;
  220. }
  221. /**
  222. * @param int $id
  223. */
  224. public function setId ($id) {
  225. $this->id = (int) $id;
  226. return $this;
  227. }
  228. /**
  229. * @return string
  230. */
  231. public function getPath () {
  232. return $this->path;
  233. }
  234. /**
  235. * @param string $path
  236. */
  237. public function setPath ($path) {
  238. $this->path = $path;
  239. return $this;
  240. }
  241. /**
  242. * @return string
  243. */
  244. public function getType () {
  245. return $this->type;
  246. }
  247. /**
  248. * @param $type
  249. */
  250. public function setType ($type) {
  251. $this->type = $type;
  252. return $this;
  253. }
  254. /**
  255. * @return string
  256. */
  257. public function getSubtype () {
  258. return $this->subtype;
  259. }
  260. /**
  261. * @param $subtype
  262. */
  263. public function setSubtype ($subtype) {
  264. $this->subtype = $subtype;
  265. return $this;
  266. }
  267. /**
  268. * @return int
  269. */
  270. public function getAmount () {
  271. return $this->amount;
  272. }
  273. /**
  274. * @param $amount
  275. */
  276. public function setAmount ($amount) {
  277. $this->amount = (int) $amount;
  278. return $this;
  279. }
  280. /**
  281. * @return int
  282. */
  283. public function getDate () {
  284. return $this->date;
  285. }
  286. /**
  287. * @param $date
  288. */
  289. public function setDate ($date) {
  290. $this->date = (int) $date;
  291. return $this;
  292. }
  293. /**
  294. * @return Element_Interface
  295. */
  296. public function getElement () {
  297. return $this->element;
  298. }
  299. /**
  300. * @param $element
  301. */
  302. public function setElement ($element) {
  303. $this->element = $element;
  304. return $this;
  305. }
  306. /**
  307. * @param $username
  308. */
  309. public function setDeletedby($username){
  310. $this->deletedby = $username;
  311. return $this;
  312. }
  313. /**
  314. * @return string
  315. */
  316. public function getDeletedby(){
  317. return $this->deletedby;
  318. }
  319. }