PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_zoo/elements/element/element.php

https://github.com/foxbei/joomla15
PHP | 713 lines | 267 code | 97 blank | 349 comment | 27 complexity | f48cbb08a9d00ad82b99d8216e9916e0 MD5 | raw file
  1. <?php
  2. /**
  3. * @package ZOO Component
  4. * @file element.php
  5. * @version 2.3.7 March 2011
  6. * @author YOOtheme http://www.yootheme.com
  7. * @copyright Copyright (C) 2007 - 2011 YOOtheme GmbH
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. */
  10. /*
  11. Class: Element
  12. The Element abstract class
  13. */
  14. abstract class Element {
  15. /*
  16. Variable: $identifier
  17. Element identifier.
  18. */
  19. public $identifier;
  20. /*
  21. Variable: $ordering
  22. Element ordering.
  23. */
  24. public $ordering = 0;
  25. /*
  26. Variable: $_type
  27. Elements related type object.
  28. */
  29. protected $_type;
  30. /*
  31. Variable: $_item
  32. Elements related item object.
  33. */
  34. protected $_item;
  35. /*
  36. Variable: $_callbacks
  37. Element callbacks.
  38. */
  39. protected $_callbacks = array();
  40. /*
  41. Variable: $_config
  42. Config parameter object.
  43. */
  44. protected $_config;
  45. /*
  46. Variable: $_metaxml
  47. Element meta xml.
  48. */
  49. protected $_metaxml;
  50. /*
  51. Variable: $_data
  52. Element data.
  53. */
  54. protected $_data;
  55. /*
  56. Variable: $_path
  57. Element file path.
  58. */
  59. protected $_path;
  60. /*
  61. Function: Constructor
  62. */
  63. public function __construct() {
  64. $this->_config = new YParameter();
  65. // initialize data
  66. $this->_data = ElementData::newInstance($this);
  67. }
  68. /*
  69. Function: getElementType
  70. Gets the elements type.
  71. Returns:
  72. string - the elements type
  73. */
  74. public function getElementType() {
  75. return strtolower(str_replace('Element', '', get_class($this)));
  76. }
  77. /*
  78. Function: getElementData
  79. Gets the elements data.
  80. Returns:
  81. ElementData - the elements data
  82. */
  83. public function getElementData() {
  84. return $this->_data;
  85. }
  86. /*
  87. Function: setData
  88. Set data through xml string.
  89. Parameters:
  90. $xml - string
  91. Returns:
  92. Void
  93. */
  94. public function setData($xml) {
  95. $this->_data = ElementData::newInstance($this);
  96. if (!empty($xml) && ($xml = YXML::loadString($xml))) {
  97. foreach ($xml->children() as $xml_element) {
  98. if ((string) $xml_element->attributes()->identifier == $this->identifier) {
  99. $this->_data->decodeXML($xml_element);
  100. break;
  101. }
  102. }
  103. }
  104. return $this;
  105. }
  106. /*
  107. Function: unsetData
  108. Unsets element data
  109. Returns:
  110. Void
  111. */
  112. public function unsetData() {
  113. if (isset($this->_data)) {
  114. $this->_data->unsetData();
  115. }
  116. return $this;
  117. }
  118. /*
  119. Function: bindData
  120. Set data through data array.
  121. Parameters:
  122. $data - array
  123. Returns:
  124. Void
  125. */
  126. public function bindData($data = array()) {
  127. $this->_data = ElementData::newInstance($this);
  128. foreach ($data as $key => $value) {
  129. $this->_data->set($key, $value);
  130. }
  131. }
  132. /*
  133. Function: toXML
  134. Get elements XML representation.
  135. Returns:
  136. string - XML representation
  137. */
  138. public function toXML() {
  139. return $this->_data->encodeData()->asXML(true);
  140. }
  141. /*
  142. Function: getLayout
  143. Get element layout path and use override if exists.
  144. Returns:
  145. String - Layout path
  146. */
  147. public function getLayout($layout = null) {
  148. // init vars
  149. $type = $this->getElementType();
  150. $path = ZOO_ADMIN_PATH."/elements/{$type}/tmpl";
  151. // set default
  152. if ($layout == null) {
  153. $layout = "{$type}.php";
  154. }
  155. // find layout
  156. if (JPath::find($path, $layout)) {
  157. return $path."/".$layout;
  158. }
  159. return null;
  160. }
  161. /*
  162. Function: getSearchData
  163. Get elements search data.
  164. Returns:
  165. String - Search data
  166. */
  167. public function getSearchData() {
  168. return null;
  169. }
  170. /*
  171. Function: getOrdering
  172. Get order number.
  173. Returns:
  174. Int - order number
  175. */
  176. public function getOrdering() {
  177. return $this->ordering;
  178. }
  179. /*
  180. Function: getItem
  181. Get related item object.
  182. Returns:
  183. Item - item object
  184. */
  185. public function getItem() {
  186. return $this->_item;
  187. }
  188. /*
  189. Function: getType
  190. Get related type object.
  191. Returns:
  192. Type - type object
  193. */
  194. public function getType() {
  195. return $this->_type;
  196. }
  197. /*
  198. Function: getGroup
  199. Get element group.
  200. Returns:
  201. string - group
  202. */
  203. public function getGroup() {
  204. $metadata = $this->getMetadata();
  205. return $metadata['group'];
  206. }
  207. /*
  208. Function: setOrdering
  209. Set order number.
  210. Parameters:
  211. $ordering - order number
  212. Returns:
  213. Void
  214. */
  215. public function setOrdering($ordering) {
  216. $this->ordering = $ordering;
  217. }
  218. /*
  219. Function: setItem
  220. Set related item object.
  221. Parameters:
  222. $item - item object
  223. Returns:
  224. Void
  225. */
  226. public function setItem($item) {
  227. $this->_item = $item;
  228. }
  229. /*
  230. Function: setType
  231. Set related type object.
  232. Parameters:
  233. $type - type object
  234. Returns:
  235. Void
  236. */
  237. public function setType($type) {
  238. $this->_type = $type;
  239. }
  240. /*
  241. Function: hasValue
  242. Checks if the element's value is set.
  243. Parameters:
  244. $params - render parameter
  245. Returns:
  246. Boolean - true, on success
  247. */
  248. public function hasValue($params = array()) {
  249. $value = $this->_data->get('value');
  250. return !empty($value);
  251. }
  252. /*
  253. Function: render
  254. Renders the element.
  255. Parameters:
  256. $params - render parameter
  257. Returns:
  258. String - html
  259. */
  260. public function render($params = array()) {
  261. // render layout
  262. if ($layout = $this->getLayout()) {
  263. return self::renderLayout($layout, array('value' => $this->_data->get('value')));
  264. }
  265. return $this->_data->get('value');
  266. }
  267. /*
  268. Function: renderLayout
  269. Renders the element using template layout file.
  270. Parameters:
  271. $__layout - layouts template file
  272. $__args - layouts template file args
  273. Returns:
  274. String - html
  275. */
  276. protected function renderLayout($__layout, $__args = array()) {
  277. // init vars
  278. if (is_array($__args)) {
  279. foreach ($__args as $__var => $__value) {
  280. $$__var = $__value;
  281. }
  282. }
  283. // render layout
  284. $__html = '';
  285. ob_start();
  286. include($__layout);
  287. $__html = ob_get_contents();
  288. ob_end_clean();
  289. return $__html;
  290. }
  291. /*
  292. Function: edit
  293. Renders the edit form field.
  294. Must be overloaded by the child class.
  295. Returns:
  296. String - html
  297. */
  298. abstract public function edit();
  299. /*
  300. Function: loadAssets
  301. Load elements css/js assets.
  302. Returns:
  303. Void
  304. */
  305. public function loadAssets() {}
  306. /*
  307. Function: registerCallback
  308. Register a callback function.
  309. Returns:
  310. Void
  311. */
  312. public function registerCallback($method) {
  313. if (!in_array(strtolower($method), $this->_callbacks)) {
  314. $this->_callbacks[] = strtolower($method);
  315. }
  316. }
  317. /*
  318. Function: callback
  319. Execute elements callback function.
  320. Returns:
  321. Mixed
  322. */
  323. public function callback($method, $args = array()) {
  324. // try to call a elements class method
  325. if (in_array(strtolower($method), $this->_callbacks) && method_exists($this, $method)) {
  326. // call method
  327. $res = call_user_func_array(array($this, $method), $args);
  328. // output if method returns a string
  329. if (is_string($res)) {
  330. echo $res;
  331. }
  332. }
  333. }
  334. /*
  335. Function: getConfig
  336. Retrieve element configuration.
  337. Returns:
  338. YParameter
  339. */
  340. public function getConfig() {
  341. return $this->_config;
  342. }
  343. /*
  344. Function: bindConfig
  345. Binds the data array.
  346. Parameters:
  347. $data - data array
  348. $ignore - An array or space separated list of fields not to bind
  349. Returns:
  350. Element
  351. */
  352. public function bindConfig($data, $ignore = array()) {
  353. $this->_config = new YParameter();
  354. $this->_config->set('identifier', $this->identifier);
  355. // convert space separated list
  356. if (!is_array($ignore)) {
  357. $ignore = explode(' ', $ignore);
  358. }
  359. // bind data array
  360. if (is_array($data)) {
  361. foreach ($data as $name => $value) {
  362. if (!in_array($name, $ignore)) {
  363. $this->_config->set($name, $value);
  364. }
  365. }
  366. }
  367. return $this;
  368. }
  369. /*
  370. Function: loadConfig
  371. Load xml element configuration.
  372. Parameters:
  373. $xml - XML for this element
  374. Returns:
  375. Element
  376. */
  377. public function loadConfig(YXMLElement $xml) {
  378. // bind xml data
  379. foreach ($xml->attributes() as $name => $value) {
  380. $this->_config->set($name, (string) $value);
  381. }
  382. // set identifier
  383. $this->identifier = $this->_config->get('identifier');
  384. return $this;
  385. }
  386. /*
  387. Function: getConfigForm
  388. Get parameter form object to render input form.
  389. Returns:
  390. Parameter Object
  391. */
  392. public function getConfigForm() {
  393. $xml = $this->getPath().'/'.$this->getElementType().'.xml';
  394. // get parameter xml file
  395. if (JFile::exists($xml)) {
  396. // get form
  397. $form = new YParameterFormDefault($xml);
  398. $form->addElementPath(ZOO_ADMIN_PATH.'/joomla/elements');
  399. $form->setValues($this->_config);
  400. $form->element = $this; // add reference to element
  401. return $form;
  402. }
  403. return null;
  404. }
  405. /*
  406. Function: getConfigXML
  407. Get element configuration as xml formatted string.
  408. Returns:
  409. String
  410. */
  411. public function getConfigXML($ignore = array()) {
  412. $xml = YXMLElement::create('param')
  413. ->addAttribute('type', $this->getElementType())
  414. ->addAttribute('identifier', $this->_config->get('identifier'))
  415. ->addAttribute('name', $this->_config->get('name'));
  416. if ($xmlfile = $this->getMetaXML()) {
  417. $params = $xmlfile->xpath('params/param');
  418. if ($params) {
  419. foreach ($params as $param) {
  420. $name = (string) $param->attributes()->name;
  421. $value = $this->_config->get($name);
  422. if (isset($value) && !in_array($name, $ignore)) {
  423. $xml->addAttribute($name, $value);
  424. }
  425. }
  426. }
  427. }
  428. return $xml;
  429. }
  430. /*
  431. Function: loadConfigAssets
  432. Load elements css/js config assets.
  433. Returns:
  434. Element
  435. */
  436. public function loadConfigAssets() {
  437. return $this;
  438. }
  439. /*
  440. Function: getMetaData
  441. Get elements xml meta data.
  442. Returns:
  443. Array - Meta information
  444. */
  445. public function getMetaData() {
  446. $data = array();
  447. $xml = $this->getMetaXML();
  448. if (!$xml) {
  449. return false;
  450. }
  451. $data['type'] = $xml->attributes()->type ? (string) $xml->attributes()->type : 'Unknown';
  452. $data['group'] = $xml->attributes()->group ? (string) $xml->attributes()->group : 'Unknown';
  453. $data['hidden'] = $xml->attributes()->hidden ? (string) $xml->attributes()->hidden : 'false';
  454. $data['trusted'] = $xml->attributes()->trusted ? (string) $xml->attributes()->trusted : 'false';
  455. $data['name'] = (string) $xml->name;
  456. $data['creationdate'] = $xml->creationDate ? (string) $xml->creationDate : 'Unknown';
  457. $data['author'] = $xml->author ? (string) $xml->author : 'Unknown';
  458. $data['copyright'] = (string) $xml->copyright;
  459. $data['authorEmail'] = (string) $xml->authorEmail;
  460. $data['authorUrl'] = (string) $xml->authorUrl;
  461. $data['version'] = (string) $xml->version;
  462. $data['description'] = (string) $xml->description;
  463. return $data;
  464. }
  465. /*
  466. Function: getMetaXML
  467. Get elements xml meta file.
  468. Returns:
  469. Object - YXMLElement
  470. */
  471. public function getMetaXML() {
  472. if (empty($this->_metaxml)) {
  473. $this->_metaxml = YXML::loadFile($this->getPath().'/'.$this->getElementType().'.xml');
  474. }
  475. return $this->_metaxml;
  476. }
  477. /*
  478. Function: getPath
  479. Get path to element's base directory.
  480. Returns:
  481. String - Path
  482. */
  483. public function getPath() {
  484. if (empty($this->_path)) {
  485. $rc = new ReflectionClass(get_class($this));
  486. $this->_path = dirname($rc->getFileName());
  487. }
  488. return $this->_path;
  489. }
  490. }
  491. class ElementData {
  492. protected $_params;
  493. protected $_element;
  494. public function __construct($element) {
  495. $this->_element = $element;
  496. $this->_params = new YParameter();
  497. }
  498. public static function newInstance($element) {
  499. $current_class = get_class($element);
  500. do {
  501. $class_name = $current_class.'Data';
  502. if (class_exists($class_name)) {
  503. return new $class_name($element);
  504. }
  505. } while ($current_class = get_parent_class($current_class));
  506. }
  507. public function getParams() {
  508. return $this->_params;
  509. }
  510. public function set($name, $value) {
  511. $this->_params->set($name, $value);
  512. }
  513. public function get($name, $default = null) {
  514. return $this->_params->get($name, $default);
  515. }
  516. public function encodeData() {
  517. $xml = YXMLElement::create($this->_element->getElementType())->addAttribute('identifier', $this->_element->identifier);
  518. foreach ($this->_params->toArray() as $key => $value) {
  519. $xml->addChild($key, $value, null, true);
  520. }
  521. return $xml;
  522. }
  523. public function decodeXML(YXMLElement $element_xml) {
  524. foreach ($element_xml->children() as $child) {
  525. $this->set($child->getName(), (string) $child);
  526. }
  527. return $this;
  528. }
  529. public function unsetData() {
  530. foreach($this->_params as $key => $data) {
  531. unset($this->_params[$key]);
  532. }
  533. $this->_element = null;
  534. }
  535. }
  536. // Declare the interface 'iSubmittable'
  537. interface iSubmittable {
  538. /*
  539. Function: renderSubmission
  540. Renders the element in submission.
  541. Parameters:
  542. $params - submission parameters
  543. Returns:
  544. String - html
  545. */
  546. public function renderSubmission($params = array());
  547. /*
  548. Function: validateSubmission
  549. Validates the submitted element
  550. Parameters:
  551. $value - YArray value
  552. $params - YArray submission parameters
  553. Returns:
  554. Array - cleaned value
  555. */
  556. public function validateSubmission($value, $params);
  557. }
  558. interface iSubmissionUpload {
  559. /*
  560. Function: doUpload
  561. Does the actual upload during submission
  562. Returns:
  563. void
  564. */
  565. public function doUpload();
  566. }
  567. /*
  568. Class: ElementException
  569. */
  570. class ElementException extends YException {}