PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/tree/Tree.php

https://gitlab.com/staging06/myproject
PHP | 491 lines | 373 code | 80 blank | 38 comment | 49 complexity | dca14594d1b8a80db2227b44a684f7a7 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class TreeCore
  27. {
  28. const DEFAULT_TEMPLATE_DIRECTORY = 'helpers/tree';
  29. const DEFAULT_TEMPLATE = 'tree.tpl';
  30. const DEFAULT_HEADER_TEMPLATE = 'tree_header.tpl';
  31. const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder.tpl';
  32. const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item.tpl';
  33. protected $_attributes;
  34. private $_context;
  35. protected $_data;
  36. protected $_data_search;
  37. protected $_headerTemplate;
  38. protected $_id_tree;
  39. private $_id;
  40. protected $_node_folder_template;
  41. protected $_node_item_template;
  42. protected $_template;
  43. /** @var string */
  44. private $_template_directory;
  45. private $_title;
  46. private $_no_js;
  47. /** @var TreeToolbar|ITreeToolbar */
  48. private $_toolbar;
  49. public function __construct($id, $data = null)
  50. {
  51. $this->setId($id);
  52. if (isset($data)) {
  53. $this->setData($data);
  54. }
  55. }
  56. public function __toString()
  57. {
  58. return $this->render();
  59. }
  60. public function setActions($value)
  61. {
  62. if (!isset($this->_toolbar)) {
  63. $this->setToolbar(new TreeToolbarCore());
  64. }
  65. $this->getToolbar()->setTemplateDirectory($this->getTemplateDirectory())->setActions($value);
  66. return $this;
  67. }
  68. public function getActions()
  69. {
  70. if (!isset($this->_toolbar)) {
  71. $this->setToolbar(new TreeToolbarCore());
  72. }
  73. return $this->getToolbar()->setTemplateDirectory($this->getTemplateDirectory())->getActions();
  74. }
  75. public function setAttribute($name, $value)
  76. {
  77. if (!isset($this->_attributes)) {
  78. $this->_attributes = array();
  79. }
  80. $this->_attributes[$name] = $value;
  81. return $this;
  82. }
  83. public function getAttribute($name)
  84. {
  85. return $this->hasAttribute($name) ? $this->_attributes[$name] : null;
  86. }
  87. public function setAttributes($value)
  88. {
  89. if (!is_array($value) && !$value instanceof Traversable) {
  90. throw new PrestaShopException('Data value must be an traversable array');
  91. }
  92. $this->_attributes = $value;
  93. return $this;
  94. }
  95. public function setIdTree($id_tree)
  96. {
  97. $this->_id_tree = $id_tree;
  98. return $this;
  99. }
  100. public function getIdTree()
  101. {
  102. return $this->_id_tree;
  103. }
  104. public function getAttributes()
  105. {
  106. if (!isset($this->_attributes)) {
  107. $this->_attributes = array();
  108. }
  109. return $this->_attributes;
  110. }
  111. public function setContext($value)
  112. {
  113. $this->_context = $value;
  114. return $this;
  115. }
  116. public function getContext()
  117. {
  118. if (!isset($this->_context)) {
  119. $this->_context = Context::getContext();
  120. }
  121. return $this->_context;
  122. }
  123. public function setDataSearch($value)
  124. {
  125. if (!is_array($value) && !$value instanceof Traversable) {
  126. throw new PrestaShopException('Data value must be an traversable array');
  127. }
  128. $this->_data_search = $value;
  129. return $this;
  130. }
  131. public function getDataSearch()
  132. {
  133. if (!isset($this->_data_search)) {
  134. $this->_data_search = array();
  135. }
  136. return $this->_data_search;
  137. }
  138. public function setData($value)
  139. {
  140. if (!is_array($value) && !$value instanceof Traversable) {
  141. throw new PrestaShopException('Data value must be an traversable array');
  142. }
  143. $this->_data = $value;
  144. return $this;
  145. }
  146. public function getData()
  147. {
  148. if (!isset($this->_data)) {
  149. $this->_data = array();
  150. }
  151. return $this->_data;
  152. }
  153. public function setHeaderTemplate($value)
  154. {
  155. $this->_headerTemplate = $value;
  156. return $this;
  157. }
  158. public function getHeaderTemplate()
  159. {
  160. if (!isset($this->_headerTemplate)) {
  161. $this->setHeaderTemplate(self::DEFAULT_HEADER_TEMPLATE);
  162. }
  163. return $this->_headerTemplate;
  164. }
  165. public function setId($value)
  166. {
  167. $this->_id = $value;
  168. return $this;
  169. }
  170. public function getId()
  171. {
  172. return $this->_id;
  173. }
  174. public function setNodeFolderTemplate($value)
  175. {
  176. $this->_node_folder_template = $value;
  177. return $this;
  178. }
  179. public function getNodeFolderTemplate()
  180. {
  181. if (!isset($this->_node_folder_template)) {
  182. $this->setNodeFolderTemplate(self::DEFAULT_NODE_FOLDER_TEMPLATE);
  183. }
  184. return $this->_node_folder_template;
  185. }
  186. public function setNodeItemTemplate($value)
  187. {
  188. $this->_node_item_template = $value;
  189. return $this;
  190. }
  191. public function getNodeItemTemplate()
  192. {
  193. if (!isset($this->_node_item_template)) {
  194. $this->setNodeItemTemplate(self::DEFAULT_NODE_ITEM_TEMPLATE);
  195. }
  196. return $this->_node_item_template;
  197. }
  198. public function setTemplate($value)
  199. {
  200. $this->_template = $value;
  201. return $this;
  202. }
  203. public function getTemplate()
  204. {
  205. if (!isset($this->_template)) {
  206. $this->setTemplate(self::DEFAULT_TEMPLATE);
  207. }
  208. return $this->_template;
  209. }
  210. /**
  211. * @param $value
  212. *
  213. * @return Tree
  214. */
  215. public function setTemplateDirectory($value)
  216. {
  217. $this->_template_directory = $this->_normalizeDirectory($value);
  218. return $this;
  219. }
  220. /**
  221. * @return string
  222. */
  223. public function getTemplateDirectory()
  224. {
  225. if (!isset($this->_template_directory)) {
  226. $this->_template_directory = $this->_normalizeDirectory(
  227. self::DEFAULT_TEMPLATE_DIRECTORY);
  228. }
  229. return $this->_template_directory;
  230. }
  231. public function getTemplateFile($template)
  232. {
  233. if (preg_match_all('/((?:^|[A-Z])[a-z]+)/', get_class($this->getContext()->controller), $matches) !== false) {
  234. $controller_name = strtolower($matches[0][1]);
  235. }
  236. if ($this->getContext()->controller instanceof ModuleAdminController && isset($controller_name) && file_exists($this->_normalizeDirectory(
  237. $this->getContext()->controller->getTemplatePath()).$controller_name.DIRECTORY_SEPARATOR.$this->getTemplateDirectory().$template)) {
  238. return $this->_normalizeDirectory($this->getContext()->controller->getTemplatePath()).
  239. $controller_name.DIRECTORY_SEPARATOR.$this->getTemplateDirectory().$template;
  240. } elseif ($this->getContext()->controller instanceof ModuleAdminController && file_exists($this->_normalizeDirectory(
  241. $this->getContext()->controller->getTemplatePath()).$this->getTemplateDirectory().$template)) {
  242. return $this->_normalizeDirectory($this->getContext()->controller->getTemplatePath())
  243. .$this->getTemplateDirectory().$template;
  244. } elseif ($this->getContext()->controller instanceof AdminController && isset($controller_name)
  245. && file_exists($this->_normalizeDirectory($this->getContext()->smarty->getTemplateDir(0)).'controllers'
  246. .DIRECTORY_SEPARATOR.$controller_name.DIRECTORY_SEPARATOR.$this->getTemplateDirectory().$template)) {
  247. return $this->_normalizeDirectory($this->getContext()->smarty->getTemplateDir(0)).'controllers'
  248. .DIRECTORY_SEPARATOR.$controller_name.DIRECTORY_SEPARATOR.$this->getTemplateDirectory().$template;
  249. } elseif (file_exists($this->_normalizeDirectory($this->getContext()->smarty->getTemplateDir(1))
  250. .$this->getTemplateDirectory().$template)) {
  251. return $this->_normalizeDirectory($this->getContext()->smarty->getTemplateDir(1))
  252. .$this->getTemplateDirectory().$template;
  253. } elseif (file_exists($this->_normalizeDirectory($this->getContext()->smarty->getTemplateDir(0))
  254. .$this->getTemplateDirectory().$template)) {
  255. return $this->_normalizeDirectory($this->getContext()->smarty->getTemplateDir(0))
  256. .$this->getTemplateDirectory().$template;
  257. } else {
  258. return $this->getTemplateDirectory().$template;
  259. }
  260. }
  261. public function setNoJS($value)
  262. {
  263. $this->_no_js = $value;
  264. return $this;
  265. }
  266. public function setTitle($value)
  267. {
  268. $this->_title = $value;
  269. return $this;
  270. }
  271. public function getTitle()
  272. {
  273. return $this->_title;
  274. }
  275. public function setToolbar($value)
  276. {
  277. if (!is_object($value)) {
  278. throw new PrestaShopException('Toolbar must be a class object');
  279. }
  280. $reflection = new ReflectionClass($value);
  281. if (!$reflection->implementsInterface('ITreeToolbarCore')) {
  282. throw new PrestaShopException('Toolbar class must implements ITreeToolbarCore interface');
  283. }
  284. $this->_toolbar = $value;
  285. return $this;
  286. }
  287. public function getToolbar()
  288. {
  289. if (isset($this->_toolbar)) {
  290. if ($this->getDataSearch()) {
  291. $this->_toolbar->setData($this->getDataSearch());
  292. } else {
  293. $this->_toolbar->setData($this->getData());
  294. }
  295. }
  296. return $this->_toolbar;
  297. }
  298. public function addAction($action)
  299. {
  300. if (!isset($this->_toolbar)) {
  301. $this->setToolbar(new TreeToolbarCore());
  302. }
  303. $this->getToolbar()->setTemplateDirectory($this->getTemplateDirectory())->addAction($action);
  304. return $this;
  305. }
  306. public function removeActions()
  307. {
  308. if (!isset($this->_toolbar)) {
  309. $this->setToolbar(new TreeToolbarCore());
  310. }
  311. $this->getToolbar()->setTemplateDirectory($this->getTemplateDirectory())->removeActions();
  312. return $this;
  313. }
  314. public function render($data = null)
  315. {
  316. //Adding tree.js
  317. $admin_webpath = str_ireplace(_PS_CORE_DIR_, '', _PS_ADMIN_DIR_);
  318. $admin_webpath = preg_replace('/^'.preg_quote(DIRECTORY_SEPARATOR, '/').'/', '', $admin_webpath);
  319. $bo_theme = ((Validate::isLoadedObject($this->getContext()->employee)
  320. && $this->getContext()->employee->bo_theme) ? $this->getContext()->employee->bo_theme : 'default');
  321. if (!file_exists(_PS_BO_ALL_THEMES_DIR_.$bo_theme.DIRECTORY_SEPARATOR.'template')) {
  322. $bo_theme = 'default';
  323. }
  324. $js_path = __PS_BASE_URI__.$admin_webpath.'/themes/'.$bo_theme.'/js/tree.js';
  325. if ($this->getContext()->controller->ajax) {
  326. if (!$this->_no_js) {
  327. $html = '<script type="text/javascript" src="'.$js_path.'"></script>';
  328. }
  329. } else {
  330. $this->getContext()->controller->addJs($js_path);
  331. }
  332. //Create Tree Template
  333. $template = $this->getContext()->smarty->createTemplate(
  334. $this->getTemplateFile($this->getTemplate()),
  335. $this->getContext()->smarty
  336. );
  337. if (trim($this->getTitle()) != '' || $this->useToolbar()) {
  338. //Create Tree Header Template
  339. $headerTemplate = $this->getContext()->smarty->createTemplate(
  340. $this->getTemplateFile($this->getHeaderTemplate()),
  341. $this->getContext()->smarty
  342. );
  343. $headerTemplate->assign($this->getAttributes())
  344. ->assign(array(
  345. 'title' => $this->getTitle(),
  346. 'toolbar' => $this->useToolbar() ? $this->renderToolbar() : null
  347. ));
  348. $template->assign('header', $headerTemplate->fetch());
  349. }
  350. //Assign Tree nodes
  351. $template->assign($this->getAttributes())->assign(array(
  352. 'id' => $this->getId(),
  353. 'nodes' => $this->renderNodes($data),
  354. 'id_tree' => $this->getIdTree()
  355. ));
  356. return (isset($html) ? $html : '').$template->fetch();
  357. }
  358. public function renderNodes($data = null)
  359. {
  360. if (!isset($data)) {
  361. $data = $this->getData();
  362. }
  363. if (!is_array($data) && !$data instanceof Traversable) {
  364. throw new PrestaShopException('Data value must be an traversable array');
  365. }
  366. $html = '';
  367. foreach ($data as $item) {
  368. if (array_key_exists('children', $item)
  369. && !empty($item['children'])) {
  370. $html .= $this->getContext()->smarty->createTemplate(
  371. $this->getTemplateFile($this->getNodeFolderTemplate()),
  372. $this->getContext()->smarty
  373. )->assign(array(
  374. 'children' => $this->renderNodes($item['children']),
  375. 'node' => $item
  376. ))->fetch();
  377. } else {
  378. $html .= $this->getContext()->smarty->createTemplate(
  379. $this->getTemplateFile($this->getNodeItemTemplate()),
  380. $this->getContext()->smarty
  381. )->assign(array(
  382. 'node' => $item
  383. ))->fetch();
  384. }
  385. }
  386. return $html;
  387. }
  388. public function renderToolbar()
  389. {
  390. return $this->getToolbar()->render();
  391. }
  392. public function useInput()
  393. {
  394. return isset($this->_input_type);
  395. }
  396. public function useToolbar()
  397. {
  398. return isset($this->_toolbar);
  399. }
  400. private function _normalizeDirectory($directory)
  401. {
  402. $last = $directory[strlen($directory) - 1];
  403. if (in_array($last, array('/', '\\'))) {
  404. $directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
  405. return $directory;
  406. }
  407. $directory .= DIRECTORY_SEPARATOR;
  408. return $directory;
  409. }
  410. }