/pimcore/models/Document/Tag/Renderlet.php

https://github.com/ngocanh/pimcore · PHP · 341 lines · 183 code · 57 blank · 101 comment · 39 complexity · a2758b4058dd62e24aae4f808beac7e4 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 Document
  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 Document_Tag_Renderlet extends Document_Tag {
  18. /**
  19. * Contains the ID of the linked object
  20. *
  21. * @var integer
  22. */
  23. public $id;
  24. /**
  25. * Contains the object
  26. *
  27. * @var Document | Asset | Object_Abstract
  28. */
  29. public $o;
  30. /**
  31. * Contains the type
  32. *
  33. * @var string
  34. */
  35. public $type;
  36. /**
  37. * Contains the subtype
  38. *
  39. * @var string
  40. */
  41. public $subtype;
  42. /**
  43. * @see Document_Tag_Interface::getType
  44. * @return string
  45. */
  46. public function getType() {
  47. return "renderlet";
  48. }
  49. /**
  50. * @see Document_Tag_Interface::getData
  51. * @return mixed
  52. */
  53. public function getData() {
  54. return array(
  55. "id" => $this->id,
  56. "type" => $this->getObjectType(),
  57. "subtype" => $this->subtype
  58. );
  59. }
  60. /**
  61. * Converts the data so it's suitable for the editmode
  62. *
  63. * @return mixed
  64. */
  65. public function getDataEditmode() {
  66. if ($this->o instanceof Element_Interface) {
  67. return array(
  68. "id" => $this->id,
  69. "type" => $this->getObjectType(),
  70. "subtype" => $this->subtype
  71. );
  72. }
  73. return null;
  74. }
  75. /**
  76. * @see Document_Tag_Interface::frontend
  77. * @return string
  78. */
  79. public function frontend() {
  80. if (!$this->options["controller"] && !$this->options["action"]) {
  81. $this->options["controller"] = Pimcore_Config::getSystemConfig()->documents->default_controller;
  82. $this->options["action"] = Pimcore_Config::getSystemConfig()->documents->default_action;
  83. }
  84. $document = null;
  85. if ($this->o instanceof Document) {
  86. $document = $this->o;
  87. }
  88. if ($this->o instanceof Element_Interface) {
  89. $blockparams = array("action", "controller", "module", "template");
  90. $params = array(
  91. "template" => $this->options["template"],
  92. "object" => $this->o,
  93. "element" => $this->o,
  94. "document" => $document,
  95. "id" => $this->id,
  96. "type" => $this->type,
  97. "subtype" => $this->subtype,
  98. "disableBlockClearing" => true
  99. );
  100. foreach ($this->options as $key => $value) {
  101. if (!array_key_exists($key, $params) && !in_array($key, $blockparams)) {
  102. $params[$key] = $value;
  103. }
  104. }
  105. if ($this->getView() != null) {
  106. return $this->getView()->action($this->options["action"], $this->options["controller"], $this->options["module"], $params);
  107. }
  108. }
  109. }
  110. /**
  111. * @see Document_Tag_Interface::setDataFromResource
  112. * @param mixed $data
  113. * @return void
  114. */
  115. public function setDataFromResource($data) {
  116. $data = unserialize($data);
  117. $this->id = $data["id"];
  118. $this->type = $data["type"];
  119. $this->subtype = $data["subtype"];
  120. $this->setElement();
  121. }
  122. /**
  123. * @see Document_Tag_Interface::setDataFromEditmode
  124. * @param mixed $data
  125. * @return void
  126. */
  127. public function setDataFromEditmode($data) {
  128. $this->id = $data["id"];
  129. $this->type = $data["type"];
  130. $this->subtype = $data["subtype"];
  131. $this->setElement();
  132. }
  133. /**
  134. * Sets the element by the data stored for the object
  135. *
  136. * @return void
  137. */
  138. public function setElement() {
  139. $this->o = Element_Service::getElementById($this->type, $this->id);
  140. }
  141. /**
  142. * @return array
  143. */
  144. public function resolveDependencies() {
  145. $dependencies = array();
  146. if ($this->o instanceof Document) {
  147. $key = "document_" . $this->o->getId();
  148. $dependencies[$key] = array(
  149. "id" => $this->o->getId(),
  150. "type" => "document"
  151. );
  152. }
  153. else if ($this->o instanceof Asset) {
  154. $key = "asset_" . $this->o->getId();
  155. $dependencies[$key] = array(
  156. "id" => $this->o->getId(),
  157. "type" => "asset"
  158. );
  159. }
  160. else if ($this->o instanceof Object_Abstract) {
  161. $key = "object_" . $this->o->getO_Id();
  162. $dependencies[$key] = array(
  163. "id" => $this->o->getO_Id(),
  164. "type" => "object"
  165. );
  166. }
  167. return $dependencies;
  168. }
  169. /**
  170. * get correct type of object as string
  171. * @param mixed $data
  172. * @return void
  173. */
  174. public function getObjectType($object = null) {
  175. if (!$object) {
  176. $object = $this->o;
  177. }
  178. if($object instanceof Element_Interface){
  179. return Element_Service::getType($object);
  180. } else {
  181. return false;
  182. }
  183. }
  184. /**
  185. * @return boolean
  186. */
  187. public function isEmpty () {
  188. $this->load();
  189. if($this->o instanceof Element_Interface) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * Receives a Webservice_Data_Document_Element from webservice import and fill the current tag's data
  196. *
  197. * @abstract
  198. * @param Webservice_Data_Document_Element $data
  199. * @return void
  200. */
  201. public function getFromWebserviceImport($wsElement) {
  202. $data = $wsElement->value;
  203. if ($data->id !==null) {
  204. $this->type = $data->type;
  205. $this->subtype = $data->subtype;
  206. $this->id = $data->id;
  207. if (is_numeric($this->id)) {
  208. if ($this->type == "asset") {
  209. $this->o = Asset::getById($this->id);
  210. if(!$this->o instanceof Asset){
  211. throw new Exception("cannot get values from web service import - referenced asset with id [ ".$this->id." ] is unknown");
  212. }
  213. } else if ($this->type == "document") {
  214. $this->o = Document::getById($this->id);
  215. if(!$this->o instanceof Document){
  216. throw new Exception("cannot get values from web service import - referenced document with id [ ".$this->id." ] is unknown");
  217. }
  218. } else if ($this->type == "object") {
  219. $this->o = Object_Abstract::getById($this->id);
  220. if(!$this->o instanceof Object_Abstract){
  221. throw new Exception("cannot get values from web service import - referenced object with id [ ".$this->id." ] is unknown");
  222. }
  223. } else {
  224. p_r($this);
  225. throw new Exception("cannot get values from web service import - type is not valid");
  226. }
  227. } else {
  228. throw new Exception("cannot get values from web service import - id is not valid");
  229. }
  230. }
  231. }
  232. /**
  233. * @return bool
  234. */
  235. public function sanityCheck() {
  236. $sane = true;
  237. if($this->id){
  238. $el = Element_Service::getElementById($this->type, $this->id);
  239. if(!$el instanceof Element_Interface){
  240. $sane = false;
  241. Logger::notice("Detected insane relation, removing reference to non existent ".$this->type." with id [".$this->id."]");
  242. $this->id = null;
  243. $this->type = null;
  244. $this->o=null;
  245. $this->subtype=null;
  246. }
  247. }
  248. return $sane;
  249. }
  250. /**
  251. * @return array
  252. */
  253. public function __sleep() {
  254. $finalVars = array();
  255. $parentVars = parent::__sleep();
  256. $blockedVars = array("o");
  257. foreach ($parentVars as $key) {
  258. if (!in_array($key, $blockedVars)) {
  259. $finalVars[] = $key;
  260. }
  261. }
  262. return $finalVars;
  263. }
  264. /**
  265. * this method is called by Document_Service::loadAllDocumentFields() to load all lazy loading fields
  266. *
  267. * @return void
  268. */
  269. public function load () {
  270. if(!$this->o) {
  271. $this->setElement();
  272. }
  273. }
  274. /**
  275. * Rewrites id from source to target, $idMapping contains sourceId => targetId mapping
  276. * @param array $idMapping
  277. * @return void
  278. */
  279. public function rewriteIds($idMapping) {
  280. if($this->type == "document" and array_key_exists((int) $this->id, $idMapping)) {
  281. $this->id = $idMapping[(int) $this->id];
  282. }
  283. }
  284. }