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

/modules/admin/classes/model/adminpages.php

https://bitbucket.org/seyar/kinda.local
PHP | 762 lines | 627 code | 96 blank | 39 comment | 47 complexity | 43043904976c18db6726f17bd9c5a65d MD5 | raw file
  1. <?php
  2. defined('SYSPATH') OR die('No direct access allowed.');
  3. class Model_AdminPages extends Model
  4. {
  5. static private $_childpages = array();
  6. public function page_types()
  7. {
  8. $query = DB::select()
  9. ->from('pagetypes')
  10. ->order_by('id')
  11. ->cached(60);
  12. $result = $query->execute(); //->cached(10)
  13. if ($result->count() == 0)
  14. return array();
  15. else
  16. {
  17. $return = array();
  18. foreach( $result->as_array() as $item )
  19. {
  20. $item['name'] = i18n::get($item['name']);
  21. $return[] = $item;
  22. }
  23. }
  24. return $return;
  25. }
  26. function find_childrens($param_id, &$param_rows, $depth=0)
  27. {
  28. $p_return = array();
  29. foreach ($param_rows as $p_k => $p_v)
  30. {
  31. if ($p_v['parent_id'] == $param_id)
  32. {
  33. $p_v['depth'] = $depth; // save object depth
  34. $p_v['page_title'] = str_repeat('-', $depth) . $p_v['page_title'];
  35. $p_return[] = $p_v;
  36. $p_current_key = count($p_return) - 1;
  37. foreach ($this->find_childrens($p_v['id'], $param_rows, $depth + 1) AS $value)
  38. {
  39. $p_return[] = $value;
  40. }
  41. }
  42. }
  43. return $p_return;
  44. }
  45. // function
  46. public function page_folders($lang_id = 1)
  47. {
  48. $return = array('0' => array('name' => I18n::get('Site Root'), 'url' => '/'));
  49. $query = DB::select('page_url', 'id', 'page_title', 'parent_url', 'parent_id')
  50. ->from('content')
  51. ->where('pagetypes_id', '=', '2') // Folder
  52. ->where('languages_id', '=', $lang_id)
  53. ->order_by('parent_id')
  54. ->cached(10);
  55. $result = $query->execute(); //->cached(10)
  56. if ($result->count() == 0)
  57. return $return;
  58. else
  59. {
  60. $rows = $result->as_array();
  61. $rows = $this->find_childrens(0, $rows);
  62. foreach ($rows as $key => $value)
  63. {
  64. $value['parent_url'] = $value['parent_url'] == '/' ? '' : $value['parent_url'];
  65. $return[$value['id']] = array('name' => $value['page_title']
  66. , 'url' => $value['parent_url'] . '/' . $value['page_url']);
  67. }
  68. return $return;
  69. }
  70. }
  71. public function get_page_templates()
  72. {
  73. $template_list = array();
  74. $template_info = Model_AdminTemplates::get_info(
  75. Model_AdminDomains::domain_template($_SERVER['HTTP_HOST'])
  76. );
  77. foreach ($template_info->templates->filename as $key => $item)
  78. {
  79. $item = $item . '';
  80. if ($template_info->default != $item)
  81. $template_list[$item] = $item;
  82. }
  83. /*
  84. $tdir = TEMPLATEPATH . 'consol';
  85. if (!file_exists($tdir) or !is_dir($tdir) or is_link($tdir)) return $template_list;
  86. foreach (scandir($tdir) as $item)
  87. {
  88. if(!is_dir("$tdir/$item")) $template_list[] = $item;
  89. }
  90. echo Kohana::debug( (string)$template_info->default);
  91. echo Kohana::debug($template_list);
  92. die();
  93. */
  94. $template_list = Arr::unshift($template_list, (string) $template_info->default, (string) $template_info->default);
  95. return $template_list;
  96. }
  97. public function page_visible_to()
  98. {
  99. $return = $enum_type = array('all' => I18n::get('All'));
  100. $query = DB::query(Database::SELECT, "SHOW COLUMNS FROM content LIKE 'visible_to' ;")
  101. ->cached(60);
  102. $result = $query->execute(); //->cached(10)
  103. if ($result->count() == 0)
  104. return $enum_type;
  105. else
  106. {
  107. $columns = $result->current();
  108. preg_match_all("|'([^']+)'|i", $columns['Type'], $enum_type);
  109. foreach ($enum_type[1] as $value)
  110. {
  111. $return[$value] = I18n::get(ucfirst($value));
  112. }
  113. return $return;
  114. }
  115. }
  116. public function page_show($languages_id = NULL, $parent_id = 0)
  117. {
  118. $return = array();
  119. $query = DB::select('content.*', array('pagetypes.name', 'page_type_name'))
  120. ->from('content')
  121. ->join('pagetypes')->on('pagetypes.id', '=', 'content.pagetypes_id')
  122. ->order_by('page_order');
  123. if (is_numeric($languages_id))
  124. $query->where('content.languages_id', '=', $languages_id);
  125. if ($parent_id)
  126. $query->and_where('content.parent_id', '!=', '0');
  127. else
  128. $query->and_where('content.parent_id', '=', '0');
  129. $result = $query->execute();
  130. if ($result->count() == 0)
  131. return array();
  132. else
  133. {
  134. if (!$parent_id)
  135. return $result->as_array();
  136. else
  137. {
  138. // Recurse function
  139. function find_childrens($param_id, &$param_rows, $depth=1)
  140. {
  141. $p_return = array();
  142. foreach ($param_rows as $p_k => $p_v)
  143. {
  144. if ($p_v['parent_id'] == $param_id)
  145. {
  146. $p_v['depth'] = $depth; // save object depth
  147. $p_return[] = $p_v;
  148. if ($p_v['pagetypes_id'] == 2) // Folder
  149. {
  150. $p_current_key = count($p_return) - 1;
  151. $p_return[$p_current_key]['childs'] = find_childrens($p_v['id'], $param_rows, $depth + 1);
  152. }
  153. }
  154. }
  155. return $p_return;
  156. }
  157. // function
  158. $rows = $result->as_array();
  159. $return = find_childrens($parent_id, $rows);
  160. return $return;
  161. }
  162. }
  163. }
  164. public function page_load($id = NULL, $where = NULL)
  165. {
  166. $query = DB::select()->from('content')->limit(1);
  167. if ((int) $id || $where)
  168. {
  169. if ((int) $id)
  170. $query->where('id', '=', $id);
  171. if ($where)
  172. {
  173. foreach ($where as $key => $item)
  174. {
  175. $query->and_where($key, '=', $item);
  176. }
  177. }
  178. $result = $query->execute();
  179. if ($result->count() == 0)
  180. return array();
  181. else
  182. {
  183. $return = $result->current();
  184. $return['page_blocks'] = self::getPageBlocks(NULL, $id);
  185. return $return;
  186. }
  187. }
  188. else
  189. {
  190. return array();
  191. }
  192. }
  193. public function page_save($lang, $id = NULL)
  194. {
  195. if ($post = Model_AdminPages::page_validate())
  196. {
  197. $post['parent_url'] .= '/';
  198. $parent_page = Model_AdminPages::page_load($post['parent_id']);
  199. if (!$parent_page)
  200. $parent_page = array('parent_url'=>'', 'page_url'=>'');
  201. $post['parent_url'] = str_replace('//', '/', $parent_page['parent_url'].'/'.$parent_page['page_url']);
  202. if ((int) $id)
  203. { // update
  204. $id = (int) $id;
  205. // Load current page info
  206. $result = DB::select()->from('content')
  207. ->limit(1)
  208. ->where('id', '=', $id)
  209. ->execute()
  210. ->current();
  211. DB::update('content')
  212. ->set(
  213. array(
  214. 'parent_id' => $post['parent_id'],
  215. 'parent_url' => $post['parent_url'],
  216. 'languages_id' => $lang,
  217. 'pagetypes_id' => $post['pagetypes_id'],
  218. 'page_url' => Controller_Admin::to_url(str_replace('//', '/', $post['page_url'])),
  219. 'page_title' => $post['page_title'],
  220. 'page_name' => $post['page_name'],
  221. 'page_content' => $post['page_content'],
  222. 'page_keywords' => $post['page_keywords'],
  223. 'page_description' => $post['page_description'],
  224. 'is_default' => $post['is_default'],
  225. 'is_cached' => $post['is_cached'],
  226. 'hide_from_map' => $post['is_on_map'],
  227. 'page_associated_words' => $post['page_associated_words'],
  228. 'page_disabled_words' => $post['page_disabled_words'],
  229. 'visible_to' => $post['visible_to'] ? $post['visible_to'] : 'all',
  230. 'page_template' => $post['page_template'],
  231. 'status' => $post['status'],
  232. )
  233. )
  234. ->where('id', '=', $id)
  235. ->execute()
  236. ;
  237. if ($post['pagetypes_id'] == 2) // Folder
  238. Model_AdminPages::updateFolderChilds($id, $result, $post);
  239. }
  240. else
  241. { // create
  242. $id = $total_rows = 0;
  243. list($id, $total_rows) = DB::insert('content',
  244. array(
  245. 'parent_id', 'parent_url', 'languages_id',
  246. 'pagetypes_id', 'page_url', 'page_title',
  247. 'page_name', 'page_content', 'page_keywords',
  248. 'page_description', 'is_default', 'is_cached',
  249. 'hide_from_map', 'page_associated_words', 'page_disabled_words',
  250. 'visible_to', 'page_template', 'status'
  251. )
  252. )
  253. ->values(
  254. array(
  255. $post['parent_id'], ($post['parent_url'] ? $post['parent_url'] : '/'), $lang,
  256. $post['pagetypes_id'], Controller_Admin::to_url(str_replace('//', '/', $post['page_url']))
  257. , $post['page_title'],
  258. $post['page_name'], $post['page_content'], $post['page_keywords'],
  259. $post['page_description'], $post['is_default'], $post['is_cached'],
  260. $post['is_on_map'], $post['page_associated_words'], $post['page_disabled_words'],
  261. 'all', $post['page_template'], $post['status'],
  262. )
  263. )
  264. ->execute();
  265. $result = DB::query(Database::SELECT,
  266. "SELECT COUNT(*) AS cnt FROM content WHERE parent_id={$post['parent_id']}")->execute();
  267. $pages_count = $result->current();
  268. $page_order = $pages_count['cnt'];
  269. DB::query(Database::UPDATE,
  270. " UPDATE content SET date_create = NOW(), page_order = $page_order"
  271. . " WHERE id = $id")->execute();
  272. }
  273. // Set IS_DEFAULT status
  274. if ($post['is_default'])
  275. {
  276. DB::query(Database::UPDATE, " UPDATE content SET is_default = 0 "
  277. . " WHERE languages_id = $lang and is_default = 1 and id <> $id")->execute();
  278. }
  279. Model_AdminPages::deletePageBlocks($id);
  280. if ($post['page_block_name'])
  281. Model_AdminPages::savePageBlocks($id, $post, $lang);
  282. return $id;
  283. }
  284. else
  285. {
  286. return false;
  287. }
  288. }
  289. public function page_move_new_diz($data = NULL)
  290. {
  291. parse_str( substr($data,0,-1), $data );
  292. foreach($data['pages'] as $key => $item)
  293. {
  294. DB::update('content')->set(array('page_order'=>($key+1)))->where('id','=',$item)->execute();
  295. }
  296. }
  297. public function page_move($id = NULL, $new_parent_id = 0, $new_order = 0)
  298. {
  299. // echo (Kohana::debug($id, $new_parent_id , $new_order));
  300. try
  301. {
  302. $newpage = DB::select()
  303. ->from('content')
  304. ->where('parent_id', '=', $new_parent_id)
  305. ->and_where_open()
  306. ->and_where('page_order', '>=', $new_order)
  307. ->and_where('page_order', '<', '65535')
  308. ->and_where_close()
  309. ->execute()
  310. ;
  311. $newpage = $newpage->current();
  312. // готовим место для cтраницы которую мы меняем
  313. DB::query(Database::UPDATE,
  314. " UPDATE content SET page_order = page_order+1 "
  315. . " WHERE parent_id=$new_parent_id AND ( page_order >= $new_order AND page_order < 65535 )")->execute();
  316. // cтраница которую мы меняем
  317. DB::update('content')
  318. ->set(array(
  319. 'parent_id' => $new_parent_id,
  320. 'parent_url' => $newpage['parent_url'],
  321. 'page_order' => $new_order
  322. ))
  323. ->where('id','=', $id)
  324. ->execute()
  325. ;
  326. // меняем номера пейдж ордер всей старой ветки.
  327. $table_hash = 't'.uniqid();
  328. DB::query(Database::DELETE, "DROP TABLE IF EXISTS $table_hash")->execute();
  329. $sql = "CREATE TEMPORARY TABLE $table_hash (i INT AUTO_INCREMENT, id INT UNSIGNED DEFAULT 0,
  330. PRIMARY KEY (`i`)
  331. )
  332. SELECT id
  333. FROM content
  334. WHERE parent_id = ".$newpage['parent_id']."
  335. ORDER BY page_order
  336. ";
  337. DB::query(Database::INSERT, $sql)->execute();
  338. DB::query(Database::UPDATE, "UPDATE content,$table_hash SET content.page_order = ($table_hash.i-1) WHERE $table_hash.id = content.id")->execute();
  339. // DB::query(Database::UPDATE,
  340. // " UPDATE content SET parent_id=$new_parent_id, page_order = $new_order "
  341. // . " WHERE id = $id")->execute();
  342. return 1;
  343. }
  344. catch (Database_Exception $e)
  345. {
  346. echo $e->getMessage();
  347. }
  348. }
  349. public function page_delete($id = NULL)
  350. {
  351. $query = DB::select()->from('content');
  352. if (is_numeric($id))
  353. {
  354. $query->where('id', '=', $id); // ->limit(1)
  355. $row = $query->execute()->current();
  356. // $query = DB::delete('content');
  357. // $query->where('id', '=', $id); // ->limit(1)
  358. // $total_rows = $query->execute();
  359. // if( $row['pagetypes_id'] == 2 )
  360. // {
  361. self::getChildrenPagesRecurse($id);
  362. $query = DB::delete('content');
  363. $query->where('id', 'IN', DB::expr( '('.implode(',', self::$_childpages).')' ) );
  364. $total_rows = $query->execute();
  365. // }
  366. }
  367. return true;
  368. }
  369. public function page_delete_list($array)
  370. {
  371. $query = DB::delete('content');
  372. if( count($array) )
  373. {
  374. foreach( $array as $item )
  375. {
  376. self::getChildrenPagesRecurse($item);
  377. }
  378. $query->where('id', 'IN', DB::expr( '('.implode(',', self::$_childpages).')' ) );
  379. $total_rows = $query->execute();
  380. }
  381. return true;
  382. }
  383. /**
  384. * Function return chilren of cur page
  385. * put data in self::$_childpages
  386. *
  387. * @param int $id
  388. * @return array
  389. */
  390. static function getChildrenPagesRecurse( $id )
  391. {
  392. $query = DB::select()
  393. ->from('content')
  394. ;
  395. array_push(self::$_childpages, (int)$id);
  396. if( is_numeric($id) )
  397. {
  398. $query->where('parent_id','=', $id);
  399. $query = $query->execute();
  400. if( $query->count() > 0 )
  401. {
  402. foreach( $query->as_array('id', 'parent_id') as $key => $item )
  403. {
  404. self::getChildrenPagesRecurse($key);
  405. }
  406. }
  407. }
  408. return;
  409. }
  410. public function page_active($id = NULL)
  411. {
  412. if (is_numeric($id))
  413. {
  414. DB::query(Database::UPDATE, "UPDATE content SET status = !status WHERE id =$id LIMIT 1")
  415. ->execute();
  416. }
  417. return true;
  418. }
  419. public function page_copy_new_diz($id = NULL)
  420. {
  421. }
  422. public function page_copy($id = NULL)
  423. {
  424. if (is_numeric($id))
  425. {
  426. $resut = DB::select('page_url')->from('content')->limit(1)->where('id', '=', $id)->execute()->as_array();
  427. $query = DB::query(Database::INSERT,
  428. ' INSERT INTO content ( page_url, parent_id, parent_url, languages_id,
  429. pagetypes_id, page_name, page_title,
  430. page_content, page_keywords,
  431. page_description, is_default, is_cached,
  432. hide_from_map, page_associated_words, page_disabled_words,
  433. date_create, visible_to, page_template )' .
  434. ' SELECT CONCAT_WS("_", t2.page_url, t3.s_sum ) AS page_url, t2.parent_id, t2.parent_url, t2.languages_id,
  435. t2.pagetypes_id, page_name, CONCAT(t2.page_title," ' . (I18n::get('CopyAdd')) . '") As page_title,
  436. t2.page_content, t2.page_keywords,
  437. t2.page_description, t2.is_default, t2.is_cached,
  438. t2.hide_from_map, t2.page_associated_words, t2.page_disabled_words,
  439. NOW(), t2.visible_to, t2.page_template ' .
  440. ' FROM content AS t2 ' .
  441. ' LEFT JOIN ( ' .
  442. ' SELECT COUNT(*)+1 AS s_sum, :id AS id FROM content AS tc WHERE page_url LIKE "' . $resut[0]['page_url'] . '%" ' .
  443. ' ) AS t3 ON (t3.id = t2.id) ' .
  444. ' WHERE t2.id=:id')
  445. ->bind(':id', $id);
  446. $result = $query->execute();
  447. }
  448. return true;
  449. }
  450. public function page_validate()
  451. {
  452. $keys = array(
  453. 'parent_id', 'parent_url',
  454. 'pagetypes_id', 'page_url', 'page_title',
  455. 'page_name', 'page_content', 'page_keywords',
  456. 'page_description', 'is_default', 'is_cached',
  457. 'is_on_map', 'page_associated_words', 'page_disabled_words',
  458. 'visible_to', 'page_template', 'status', 'page_block_name', 'page_block_text', 'page_block_id'
  459. );
  460. $params = Arr::extract($_POST, $keys, 0);
  461. $post = Validate::factory($params)
  462. ->rule('pagetypes_id', 'not_empty')
  463. ->rule('pagetypes_id', 'digit')
  464. ->rule('parent_id', 'not_empty')
  465. ->rule('parent_id', 'digit')
  466. ->rule('page_url', 'not_empty')
  467. ->rule('page_url', 'max_length', array(255))
  468. ->rule('page_keywords', 'max_length', array(255))
  469. ->rule('page_description', 'max_length', array(255))
  470. ->rule('page_template', 'not_empty')
  471. ;
  472. if ($post->check())
  473. {
  474. return $params;
  475. }
  476. else
  477. {
  478. Messages::add($post->errors('validate'));
  479. }
  480. }
  481. public function updateFolderChilds($id = NULL, $prev_info = array(), $new_info = array())
  482. {
  483. // If Folder URL or Folder Parent was changed then fix childrens params
  484. if ($prev_info['page_url'] != $new_info['page_url'] or $prev_info['parent_url'] != $new_info['parent_url'])
  485. {
  486. $old_childs_url = ($prev_info['parent_url'] != '/' ? $prev_info['parent_url'] : '') . '/' . $prev_info['page_url'];
  487. $new_childs_url = ($new_info['parent_url'] != '/' ? $new_info['parent_url'] : '') . '/' . $new_info['page_url'];
  488. DB::query(Database::UPDATE,
  489. " UPDATE content SET parent_url = CONCAT( '$new_childs_url', SUBSTRING(parent_url, LENGTH('$old_childs_url')+1) ) "
  490. . " WHERE parent_url LIKE '$old_childs_url%'")->execute();
  491. }
  492. }
  493. function getSearch($queryOrig)
  494. {
  495. $queryOrig = mb_strtolower($queryOrig, 'UTF-8');
  496. $fields = array('page_url', 'page_title', 'page_name', 'page_associated_words', 'page_content');
  497. $query = explode(' ', $queryOrig);
  498. $found = array();
  499. $str = "SELECT * FROM content WHERE ";
  500. $like = '';
  501. foreach ($fields as $item)
  502. {
  503. $like .= '(';
  504. foreach ($query as $word)
  505. {
  506. if( mb_strlen( $word, 'UTF-8' ) > Kohana::config('search.min_count_for_search') )
  507. $like .= " LCASE(content.$item) LIKE '%".mysql_escape_string($word)."%' AND ";
  508. }
  509. if( trim($like) != '(' )
  510. $like = rtrim($like,'AND ').') OR ';
  511. }
  512. $select = $str . '('.rtrim($like,' OR').') AND content.status = 1';
  513. $queryDB = DB::query(Database::SELECT, $select.' ORDER BY date_update DESC');
  514. $result = $queryDB->execute();
  515. if ($result->count() == 0)
  516. return array();
  517. $found = $result->as_array();
  518. $return = array();
  519. foreach ($found as $field)
  520. {
  521. $text = '';
  522. foreach ($fields as $item)
  523. {
  524. foreach($query as $word)
  525. if( strstr(mb_strtolower(strip_tags($field[$item]), 'UTF-8'), $word) ) $text .= $field[$item].' ';
  526. }
  527. $return[] = array(
  528. 'text' => $text,
  529. 'url' => str_replace('//', '/', $field['parent_url'] .'/'. $field['page_url']),
  530. 'date' => $field['date_update']
  531. );
  532. }
  533. return $return;
  534. }
  535. function savePageBlocks($id, $post, $lang)
  536. {
  537. $page_blocks = self::getPageBlocks(NULL, $id);
  538. foreach ($post['page_block_name'] as $key => $item)
  539. {
  540. if ($post['page_block_text'][$key])
  541. {
  542. if ($page_blocks[$item])
  543. {
  544. DB::update('page_blocks')
  545. ->set(array(
  546. 'page_block_name' => $item,
  547. 'page_block_text' => $post['page_block_text'][$key],
  548. 'date' => DB::expr('NOW()'),
  549. 'language_id' => $lang
  550. ))
  551. ->where('id', '=', $post['page_block_id'][$key])
  552. ->execute()
  553. ;
  554. }
  555. else
  556. {
  557. DB::insert('page_blocks', array(
  558. 'page_block_name',
  559. 'page_block_text',
  560. 'page_id',
  561. 'date',
  562. 'language_id',
  563. ))
  564. ->values(array(
  565. $item,
  566. $post['page_block_text'][$key],
  567. $id,
  568. DB::expr('NOW()'),
  569. $lang
  570. ))
  571. ->execute()
  572. ;
  573. }
  574. }
  575. }
  576. }
  577. static public function getPageBlocks($id = NULL, $page_id = NULL)
  578. {
  579. $query = DB::select()
  580. ->from('page_blocks')
  581. ;
  582. if ($id)
  583. $query->and_where('id', '=', $id);
  584. if ($page_id)
  585. $query->and_where('page_id', '=', $page_id);
  586. $result = $query->execute();
  587. if ($result->count() == 0)
  588. return array();
  589. else
  590. {
  591. if ($id)
  592. return $result->current();
  593. else
  594. return $result->as_array('page_block_name');
  595. }
  596. }
  597. function deletePageBlocks($page_id)
  598. {
  599. DB::delete('page_blocks')->where('page_id', '=', $page_id)->execute();
  600. return 1;
  601. }
  602. function siteFoldersOtherLang($lang_id = 1)
  603. {
  604. $langs = Model_Content::get_languages_array();
  605. $return = array();
  606. foreach ($langs as $item)
  607. {
  608. if ($item['id'] != $lang_id)
  609. $return[$item['id']] = $this->page_folders($item['id']);
  610. }
  611. return $return;
  612. }
  613. function copy2otherlang($forLang = NULL, $id, $lang_id = 1)
  614. {
  615. $page = $this->page_load($id);
  616. unset($page['id']);
  617. unset($page['page_blocks']);
  618. // "copy_for_lang"
  619. // "toFolderLang" => string(1) "/"
  620. if ($forLang)
  621. {
  622. foreach ($forLang as $key => $item)
  623. {
  624. $page['languages_id'] = $key;
  625. $temp_page = $this->page_load(NULL, array('parent_url' => $_POST['toFolderLang'], 'page_url' => $page['page_url'], 'languages_id' => $key));
  626. if (count($temp_page) === 0 && $item == 1)
  627. {
  628. $page['parent_url'] = $_POST['toFolderLang'];
  629. $page['parent_id'] = $_POST['toParent_id'][$key];
  630. DB::insert('content',
  631. array_keys($page)
  632. )
  633. ->values(
  634. $page
  635. )
  636. ->execute()
  637. ;
  638. }
  639. }
  640. }
  641. return 1;
  642. }
  643. }