PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/classes/Object/class.Object.php

https://github.com/janislankovskis/OwlSite
PHP | 678 lines | 528 code | 136 blank | 14 comment | 59 complexity | 6610551d860d1dc3ba6cb121d2cc608f MD5 | raw file
  1. <?php
  2. class Object extends O_Model
  3. {
  4. public $tableName = 'objects';
  5. public $sessionName = 'owl_tree';
  6. public $parent, $name, $rewrite, $ordering, $template, $data, $dateSaved, $active, $showonmenu, $ip_addr;
  7. public $alt_title, $alt_description;
  8. public $fullUrl, $opened=false, $children = array();
  9. public $presave = 'calculateData';
  10. public $postsave = 'postSave';
  11. public $fields = array(
  12. 'name' => array(
  13. 'type' => 'text',
  14. 'required' => true,
  15. ),
  16. 'template' => array(
  17. 'type' => 'custom',
  18. 'method' => 'getTemplates',
  19. 'default' => 'page',
  20. ),
  21. 'parent' => array(
  22. 'type' => 'custom',
  23. 'method' => 'loadParentField',
  24. ),
  25. 'ordering' => array(
  26. 'type' => 'custom',
  27. 'method' => 'loadOrdering',
  28. 'alt_lebel' => 'Place after'
  29. ),
  30. 'data' => array(
  31. 'type' => 'custom',
  32. 'method' => 'getTemplateFields',
  33. 'serialized' => true,
  34. ),
  35. 'alt_title' => array(
  36. 'type' => 'text'
  37. ),
  38. 'alt_description' => array(
  39. 'type' => 'textarea'
  40. ),
  41. 'active' => array (
  42. 'type' => 'checkbox',
  43. ),
  44. 'showonmenu' => array (
  45. 'type' => 'checkbox',
  46. ),
  47. 'rewrite' => array(
  48. 'type' => 'rewrite',
  49. 'required' => true,
  50. 'source' => 'name',
  51. ),
  52. 'dateSaved' => array (
  53. 'type' => 'autodatetime',
  54. 'updateonsave' => true,
  55. ),
  56. 'ip_addr' => array(
  57. 'type' => 'autoipaddr',
  58. 'updateonsave' => true,
  59. ),
  60. );
  61. private function returnChildren($id, $tree)
  62. {
  63. $out = array();
  64. foreach($tree as $item)
  65. {
  66. if($item->parent == $id)
  67. {
  68. $item->children = $this->returnChildren($item->id, $tree);
  69. if(sizeof($item->children))
  70. {
  71. $item->hasChildren = true;
  72. }
  73. else
  74. {
  75. $item->hasChildren = false;
  76. }
  77. $out[] = $item;
  78. }
  79. }
  80. return $out;
  81. }
  82. public function getAllDescendants($id = false, $out=array())
  83. {
  84. if(!$id && $id!=0)
  85. {
  86. return false;
  87. }
  88. $list = $this->getChildren($id);
  89. foreach($list as $x)
  90. {
  91. $out[] = $x->id;
  92. $out = array_merge($out, $this->getAllDescendants($x->id, $out));
  93. }
  94. return $out;
  95. }
  96. public function loadTreeChidren($parent = false)
  97. {
  98. if(!$parent && $parent!=0)
  99. {
  100. return false;
  101. }
  102. $params = array('parent' => $parent);
  103. $list = $this->getList($params);
  104. if(!$list)
  105. {
  106. return false;
  107. }
  108. foreach($list as $key=>$item)
  109. {
  110. $list[$key]->children = $item->loadTreeChidren($item->id);
  111. if(sizeof($list[$key]->children))
  112. {
  113. $list[$key]->hasChildren = true;
  114. }
  115. }
  116. if(!isset($_SESSION[$this->sessionName]['opened']))
  117. {
  118. $_SESSION[$this->sessionName]['opened'][] = $parent;
  119. }
  120. else
  121. {
  122. array_push($_SESSION[$this->sessionName]['opened'], $parent);
  123. }
  124. return $list;
  125. }
  126. public function unLoadTreeChidren($parent = false)
  127. {
  128. if(!$parent)
  129. {
  130. return false;
  131. }
  132. if(isset($_SESSION[$this->sessionName]['opened']))
  133. {
  134. foreach($_SESSION[$this->sessionName]['opened'] as $key=>$item)
  135. {
  136. if($item == $parent)
  137. {
  138. unset($_SESSION[$this->sessionName]['opened'][$key]);
  139. }
  140. }
  141. }
  142. }
  143. public function getTree()
  144. {
  145. $ids = array(0); //open root
  146. //load from session opened ids..
  147. if(isset($_SESSION[$this->sessionName]['opened']) && is_array($_SESSION[$this->sessionName]['opened']))
  148. {
  149. $ids = array_merge($ids, $_SESSION[$this->sessionName]['opened']);
  150. }
  151. //little cleanup
  152. $ids = array_unique($ids);
  153. //get all list
  154. $params = array(/* 'parent' => $ids */);
  155. $list = $this->getList($params);
  156. $out = array();
  157. foreach($list as $item)
  158. {
  159. if($item->parent == 0)
  160. {
  161. $item->children = $this->returnChildren($item->id, $list);
  162. if(sizeof($item->children))
  163. {
  164. $item->hasChildren = true;
  165. }
  166. else
  167. {
  168. $item->hasChildren = false;
  169. }
  170. $out[] = $item;
  171. }
  172. }
  173. $out = $this->removeNodes($out, $ids);
  174. return $out;
  175. }
  176. private function removeNodes($tree, $opened)
  177. {
  178. foreach($tree as $key=>$val)
  179. {
  180. if(!in_array($val->parent, $opened))
  181. {
  182. unset($tree[$key]);
  183. }
  184. elseif(sizeof($val->children))
  185. {
  186. $tree[$key]->children = $this->removeNodes($val->children, $opened);
  187. }
  188. }
  189. return $tree;
  190. }
  191. public function getList($params = array(), $getParts=false)
  192. {
  193. $q = array();
  194. $q['select'][] = 'o.*';
  195. $q['from'] = $this->tableName . ' AS o';
  196. if(isset($params['parent']))
  197. {
  198. if(is_array($params['parent']))
  199. {
  200. $q['where'][] = 'o.parent IN (' . implode(',', $params['parent']) . ')';
  201. }
  202. else
  203. {
  204. $q['where'][] = 'o.parent = ' . $params['parent'];
  205. }
  206. }
  207. if(isset($params['id']))
  208. {
  209. $q['where'][] = 'o.id = ' . $params['id'];
  210. }
  211. if(isset($params['active']))
  212. {
  213. $q['where'][] = 'o.active = 1';
  214. }
  215. if(isset($params['showonmenu']))
  216. {
  217. $q['where'][] = 'o.showonmenu = 1';
  218. }
  219. if(isset($params['rewrite']))
  220. {
  221. $q['where'][] = 'o.rewrite = "' . mysql_real_escape_string($params['rewrite']) . '"';
  222. }
  223. if(isset($params['template']))
  224. {
  225. $q['where'][] = 'o.template = "' . mysql_real_escape_string($params['template']) . '"';
  226. }
  227. if(isset($params['limit']))
  228. {
  229. $q['limit'] = $params['limit'];
  230. }
  231. if(isset($params['where']))
  232. {
  233. if(isset($q['where']))
  234. {
  235. $q['where'] = array_merge($q['where'], $params['where']);
  236. }
  237. else
  238. {
  239. $q['where'] = $params['where'];
  240. }
  241. }
  242. $q['order'] = 'o.ordering';
  243. return dbExecute($q, __CLASS__);
  244. }
  245. public function loadOrdering()
  246. {
  247. $parent = $this->parent;
  248. if(!empty($_GET['parent']) && empty($this->id)) // creating new
  249. {
  250. $parent = $_GET['parent'];
  251. }
  252. $list = $this->getChildren($parent);
  253. $select = array();
  254. $select[0] = 'start';
  255. $i = 1;
  256. foreach ($list as $key=>$item)
  257. {
  258. if($this->id != $item->id)
  259. {
  260. $select[$i] = $item->name;
  261. }
  262. $i++;
  263. }
  264. $out = array (
  265. 'type' => 'select',
  266. 'list' => $select,
  267. 'name' => 'ordering',
  268. 'data' => $this->ordering-1,
  269. 'prefer' => 'first',
  270. );
  271. return $out;
  272. }
  273. public function loadParentField()
  274. {
  275. $this->_d_level = 0;
  276. $list = array('0' => '(no parent)');
  277. //$list = $this->loadTreeChidren(0);
  278. foreach( $this->formToArray($this->loadTreeChidren(0)) as $k => $v)
  279. {
  280. $list[$k] = $v;
  281. }
  282. $out = array (
  283. 'type' => 'select',
  284. 'list' => $list,
  285. 'name' => 'parent',
  286. 'data' => $this->parent,
  287. 'addBlank' => true,
  288. );
  289. return $out;
  290. }
  291. private function formToArray($tree, $saved = array())
  292. {
  293. if(!is_array($tree))
  294. {
  295. return $saved;
  296. }
  297. foreach($tree as $item)
  298. {
  299. if($item->id==$this->id)
  300. {
  301. continue;
  302. }
  303. $name = '';
  304. for ($i = 1; $i <= $this->_d_level; $i++)
  305. {
  306. $name .= '→';
  307. }
  308. $name .= ' ' . $item->name;
  309. $saved[$item->id] = $name;
  310. if(sizeof($item->children))
  311. {
  312. $this->_d_level++;
  313. $saved = $this->formToArray($item->children, $saved);
  314. $this->_d_level--;
  315. }
  316. }
  317. return $saved;
  318. }
  319. public function getTemplates()
  320. {
  321. //readdir
  322. $dir = PATH . 'project/templates/';
  323. $fp = opendir($dir);
  324. $templates = array();
  325. while($file = readdir($fp))
  326. {
  327. if(substr($file, -4) == '.xml')
  328. {
  329. $filexml = @simplexml_load_file($dir . $file);
  330. if(is_object($filexml))
  331. {
  332. $name = $this->xml_attribute($filexml, 'name');
  333. $templates[substr($file, 0, -4)] = $name;
  334. }
  335. }
  336. }
  337. asort($templates);
  338. if(sizeof($_POST))
  339. {
  340. $this->template = $_POST['template'];
  341. }
  342. elseif($this->template == '')
  343. {
  344. $this->template = $this->fields['template']['default'];
  345. }
  346. $out = array (
  347. 'type' => 'select',
  348. 'list' => $templates,
  349. 'name' => 'template',
  350. 'data' => $this->template,
  351. 'onchangesubmit' => true,
  352. );
  353. return $out;
  354. }
  355. private function xml_attribute($object, $attribute)
  356. {
  357. if(isset($object[$attribute]))
  358. return (string) $object[$attribute];
  359. }
  360. public function calculateData($data = array())
  361. {
  362. //rewrite //TODO: add ru chars.. and other...
  363. $wrong = array(' ', ' ', ' ', 'ā', 'č', 'ē', 'ģ', 'ī', 'ķ', 'ļ', 'ņ', 'š', 'ū', 'ž');
  364. $right = array('-', '-', '-', 'a', 'c', 'e', 'g', 'i', 'k', 'l', 'n', 's', 'u', 'z');
  365. $data['rewrite'] = mb_convert_case($data['rewrite'], MB_CASE_LOWER, "UTF-8");
  366. $data['rewrite'] = str_replace($wrong, $right, $data['rewrite']);
  367. $allow = '-_-1234567890qwertyuiopasdfghjklzxcvbnm--';
  368. $new = '';
  369. for($i = 0; $i <= strlen($data['rewrite'])-1; $i++)
  370. {
  371. if(strpos($allow, substr($data['rewrite'], $i, 1))!==false)
  372. {
  373. $new .= substr($data['rewrite'], $i, 1);
  374. }
  375. }
  376. $data['rewrite'] = $new;
  377. if(!isset($data['data']))
  378. {
  379. $data['data'] = array();
  380. }
  381. //object custom data
  382. $data['data'] = serialize($data['data']);
  383. //ordering
  384. $data['ordering']++;
  385. //debug($data);
  386. return $data;
  387. }
  388. public function getTemplateFields()
  389. {
  390. $file = PATH . 'project/templates/' . $this->template . '.xml';
  391. $xml = simplexml_load_file($file);
  392. $fields = array();
  393. foreach($xml->field as $x)
  394. {
  395. $field = array(
  396. 'type' => $this->xml_attribute($x, 'type'),
  397. );
  398. $fields[$this->xml_attribute($x, 'name')] = $field;
  399. }
  400. $out = array(
  401. 'type' => 'array',
  402. 'name' => 'data',
  403. 'fields' => $fields,
  404. 'data' => $this->data,
  405. 'repeat' => false,
  406. 'fetchOnlyFirst' => true,
  407. );
  408. return $out;
  409. }
  410. public function fetchData()
  411. {
  412. if(!is_array($this->data))
  413. {
  414. $this->data = @unserialize($this->data);
  415. }
  416. $out = array();
  417. //dealing with 0
  418. foreach($this->data as $k=>$x)
  419. {
  420. $out[$k] = $x[0];
  421. }
  422. return $out;
  423. }
  424. public function getByRewrite($rewrite = false, $parent = 0, $active = true)
  425. {
  426. if(!$rewrite)
  427. {
  428. return false;
  429. }
  430. $q = array();
  431. $q['select'][] = 'o.*';
  432. $q['from'] = $this->tableName . ' AS o';
  433. $q['where'][] = 'o.rewrite = "' . mysql_real_escape_string($rewrite) . '"';
  434. $q['where'][] = 'o.parent = ' . $parent;
  435. if($active)
  436. {
  437. $q['where'][] = 'o.active = 1';
  438. }
  439. $q['limit'] = 1;
  440. $result = dbExecute($q, __CLASS__);
  441. if(!sizeof($result))
  442. {
  443. return false;
  444. }
  445. return $result[0];
  446. }
  447. public function getUrl($force=false)
  448. {
  449. //get parent
  450. if($force)
  451. {
  452. $parts = array();
  453. $parts[] = $this->rewrite;
  454. $parent = $this->parent;
  455. while($parent != 0)
  456. {
  457. $obj = $this->getParent($parent);
  458. if($obj)
  459. {
  460. $parent = $obj->parent;
  461. $parts[] = $obj->rewrite;
  462. }
  463. else
  464. {
  465. $parent = 0;
  466. }
  467. }
  468. $parts = array_reverse($parts);
  469. $url = implode('/', $parts) . '/';
  470. $this->fullUrl = $url;
  471. $q = 'UPDATE ' . $this->tableName . ' SET fullUrl = "' . $url . '" WHERE id = ' . $this->id;
  472. dbExecute($q);
  473. }
  474. return BASE . $this->fullUrl;
  475. }
  476. public function getParent($parent = null)
  477. {
  478. if($parent == null)
  479. {
  480. $parent = $this->parent;
  481. }
  482. $params = array(
  483. 'limit' => 1,
  484. 'id' => $parent,
  485. );
  486. $x = $this->getList($params);
  487. if(!$x)
  488. {
  489. return false;
  490. }
  491. return $x[0];
  492. }
  493. public function postSave()
  494. {
  495. $this->getUrl(true);
  496. $this->updateChildrenUrls($this->id);
  497. $this->updateOrdering($this->id);
  498. }
  499. /*
  500. * updates ordering .. just sets ordering values up from 0
  501. */
  502. public function updateOrdering($id)
  503. {
  504. $obj = new Object($id);
  505. $obj->loadValues();
  506. $list = $this->getChildren($obj->parent);
  507. $next = 1;
  508. foreach($list as $item)
  509. {
  510. $q = 'UPDATE objects SET ordering = ' . $next . ' WHERE id = ' . $item->id;
  511. dbExecute($q);
  512. $next++;
  513. }
  514. }
  515. public function updateChildrenUrls($id)
  516. {
  517. $list = $this->getChildren($id);
  518. foreach($list as $obj)
  519. {
  520. $obj->getUrl(true);
  521. $this->updateChildrenUrls($obj->id);
  522. }
  523. }
  524. public function getChildren($id=null, $params2 = array())
  525. {
  526. if($id == null)
  527. {
  528. $id = $this->id;
  529. }
  530. $params = array('parent' => $id);
  531. $params = array_merge($params, $params2);
  532. return $this->getList($params);
  533. }
  534. public function getVisibleChildren(){
  535. $params = array(
  536. 'parent' => $this->id,
  537. 'active' => true,
  538. );
  539. return $this->getList($params);
  540. }
  541. }
  542. ?>