PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/th/application/models/cms_item_list.php

https://gitlab.com/anurat/earththailand
PHP | 516 lines | 267 code | 54 blank | 195 comment | 22 complexity | d0529ec7fa802679e6dc7957574967d1 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CMS item list class
  4. *
  5. * a class for managing item list in Thaiecoalert project
  6. *
  7. * @author Anurat Chapanond
  8. *
  9. * @copyright Connectiv Co., Ltd.
  10. *
  11. * @version 1.0
  12. *
  13. * @since 11/02/14
  14. *
  15. */
  16. class Cms_item_list {
  17. /**
  18. * Constructor
  19. *
  20. */
  21. function __construct() {
  22. }
  23. /**
  24. * Get a list of item lists
  25. *
  26. * @return ItemList object array
  27. */
  28. public function get_item_list_list() {
  29. return ItemListQuery::create()
  30. ->orderBy_order()
  31. ->find();
  32. }
  33. /**
  34. * Get the number of item lists
  35. *
  36. * @return int
  37. */
  38. public function get_item_list_no() {
  39. return ItemListQuery::create()->count();
  40. }
  41. /**
  42. * Get a item_list
  43. *
  44. * @param int $item_list_id
  45. *
  46. * @return ItemList object
  47. */
  48. public function get_item_list( $item_list_id ) {
  49. return ItemListQuery::create()->findPk( $item_list_id );
  50. }
  51. /**
  52. * Get item_lists in each table
  53. *
  54. * @param string $table
  55. * @param string $type
  56. *
  57. * @return ItemList object array
  58. */
  59. public function get_item_lists( $table, $type="enabled" ) {
  60. $item_lists = ItemListQuery::create()
  61. ->filterBy_table( $table )
  62. ->_if( $type == "enabled" )
  63. ->filterBy_enable( 1 )
  64. ->_endif()
  65. ->orderBy_order()
  66. ->find();
  67. return $item_lists;
  68. }
  69. /**
  70. * Get the number of item_lists in each table
  71. *
  72. * @param string $table
  73. * @param string $type
  74. *
  75. * @return ItemList object array
  76. */
  77. public function get_item_lists_no( $table, $type="enabled" ) {
  78. return ItemListQuery::create()
  79. ->filterBy_table( $table )
  80. ->_if( $type == "enabled" )
  81. ->filterBy_enable( 1 )
  82. ->_endif()
  83. ->orderBy_order()
  84. ->count();
  85. }
  86. /**
  87. * Get a item_list by table ID
  88. *
  89. * @param string $table
  90. * @param int $table_id
  91. *
  92. * @return ItemList object
  93. */
  94. public function get_item_list_by_table( $table, $table_id ) {
  95. return ItemListQuery::create()
  96. ->filterBy_table( $table )
  97. ->filterBy_table_id( $table_id )
  98. ->findOne();
  99. }
  100. /**
  101. * Check whether item table already exists
  102. *
  103. * @param string $table
  104. * @param int $table_id
  105. *
  106. * @return boolean
  107. */
  108. public function has_item_list( $table, $table_id ) {
  109. return ( ItemListQuery::create()
  110. ->filterBy_table( $table )
  111. ->filterBy_table_id( $table_id )
  112. ->count() > 0 );
  113. }
  114. /**
  115. * Get maximum number of item_list order from
  116. *
  117. * @param int $gallery_id
  118. *
  119. * @return int
  120. */
  121. public function get_max_order( $table ) {
  122. $con = Propel::getConnection();
  123. $sql = "SELECT MAX( it.order )
  124. FROM item_list it
  125. WHERE it.table = '{$table}'";
  126. $stmt = $con->prepare( $sql );
  127. $stmt->execute();
  128. $result = $stmt->fetchAll();
  129. return $result[0][0];
  130. }
  131. /**
  132. * increment all table order by one
  133. *
  134. * @param string $table
  135. *
  136. */
  137. private function increment_order( $table ) {
  138. $item_lists = $this->get_item_lists( $table );
  139. foreach( $item_lists as $item_list ) {
  140. $item_list->set_order( $item_list->get_order() +1 );
  141. $item_list->save();
  142. }
  143. }
  144. /**
  145. * Add new item_list
  146. *
  147. * @param string $table
  148. * @param int $table_id
  149. *
  150. * @return ItemList object
  151. */
  152. public function add_item_list( $table, $table_id ) {
  153. $item_list = new ItemList();
  154. $item_list->set_table( $table );
  155. $item_list->set_table_id( $table_id );
  156. $item_list->set_order( $this->get_max_order( $table ) +1 );
  157. $item_list->set_lock( 0 );
  158. $item_list->set_enable( 1 );
  159. $item_list->save();
  160. return $item_list;
  161. }
  162. /**
  163. * Update item_list
  164. *
  165. * @param int $item_list_id
  166. * @param string $table
  167. * @param int $table_id
  168. * @param int $order
  169. * @param int $lock
  170. * @param int $enable
  171. *
  172. * @return ItemList object
  173. */
  174. public function update_item_list( $item_list_id, $table, $table_id, $order, $lock, $enable ) {
  175. $item_list = ItemListQuery::create()->findPk( $item_list_id );
  176. $item_list->set_table( $table );
  177. $item_list->set_table_id( $table_id );
  178. $item_list->set_order( $order );
  179. $item_list->set_lock( $lock );
  180. $item_list->set_enable( $enable );
  181. $item_list->save();
  182. return $item_list;
  183. }
  184. /**
  185. * Lock an item list
  186. *
  187. * @param string $table
  188. * @param int $table_id
  189. *
  190. */
  191. public function lock( $table, $table_id ) {
  192. $item_list = $this->get_item_list_by_table( $table, $table_id );
  193. $item_list->set_lock( 1 );
  194. $item_list->save();
  195. }
  196. /**
  197. * Unlock an item list
  198. *
  199. * @param string $table
  200. * @param int $table_id
  201. *
  202. */
  203. public function unlock( $table, $table_id ) {
  204. $item_list = $this->get_item_list_by_table( $table, $table_id );
  205. $item_list->set_lock( 0 );
  206. $item_list->save();
  207. }
  208. /**
  209. * Enable an item list
  210. *
  211. * @param string $table
  212. * @param int $table_id
  213. *
  214. */
  215. public function enable( $table, $table_id ) {
  216. $item_list = $this->get_item_list_by_table( $table, $table_id );
  217. $item_list->set_enable( 1 );
  218. $item_list->save();
  219. }
  220. /**
  221. * Disable an item list
  222. *
  223. * @param string $table
  224. * @param int $table_id
  225. *
  226. */
  227. public function disable( $table, $table_id ) {
  228. $item_list = $this->get_item_list_by_table( $table, $table_id );
  229. $item_list->set_enable( 0 );
  230. $item_list->save();
  231. }
  232. /**
  233. * Get the current position of an item list
  234. * order descendingly
  235. *
  236. * @param ItemList object $item_list
  237. *
  238. * @return int
  239. */
  240. protected function get_current_position( $item_list ) {
  241. $table = $item_list->get_table();
  242. $order = $item_list->get_order();
  243. return ItemListQuery::create()
  244. ->filterBy_table( $table )
  245. ->filterBy_order( array( 'min' => $order ) )
  246. ->count();
  247. }
  248. /**
  249. * Reorder items by new order
  250. *
  251. * @param string $table
  252. * @param int $table_id
  253. * @param int $new_pos
  254. *
  255. */
  256. public function reorder( $table, $table_id, $new_pos ) {
  257. $item_list = $this->get_item_list_by_table( $table, $table_id );
  258. $cur_pos = $this->get_current_position( $item_list );
  259. $reverse = ( $cur_pos > $new_pos );
  260. $window = abs( $cur_pos -$new_pos ) +1;
  261. $ils = ItemListQuery::create()
  262. ->filterBy_table( $table )
  263. ->orderBy_order( 'DESC' )
  264. ->offset( ( $reverse )? $new_pos -1: $cur_pos -1 )
  265. ->limit( $window )
  266. ->find();
  267. $window = min( $window, sizeof( $ils ) );
  268. $il1 = $ils[0];
  269. $il2 = $ils[$window -1];
  270. $min_order = min( $il1->get_order(), $il2->get_order() );
  271. $max_order = max( $il1->get_order(), $il2->get_order() );
  272. $i = 0;
  273. foreach( $ils as $il ) {
  274. if( $reverse ) {
  275. if( $il == $il2 ) {
  276. $il->set_order( $max_order );
  277. $il->save();
  278. }
  279. else if( $il == $il1 ) {
  280. $il->set_order( $max_order -1 );
  281. $il->save();
  282. }
  283. else {
  284. $il->set_order( $max_order -$i -1 );
  285. $il->save();
  286. }
  287. }
  288. else {
  289. if( $il == $il1 ) {
  290. $il->set_order( $min_order );
  291. $il->save();
  292. }
  293. else {
  294. $il->set_order( $max_order -$i +1 );
  295. $il->save();
  296. }
  297. }
  298. $i++;
  299. }
  300. }
  301. /**
  302. * Get the highest position or moved items
  303. *
  304. * @param string $table
  305. * @param int array $table_ids
  306. *
  307. * @return int
  308. */
  309. private function _get_reorder_highest_pos( $table, $table_ids ) {
  310. // get the highest position from table_ids
  311. $hitem = ItemListQuery::create()
  312. ->filterBy_table( $table )
  313. ->filterBy_table_id( $table_ids )
  314. ->orderBy_order( 'DESC' )
  315. ->findOne();
  316. $highest_order = $hitem->get_order();
  317. // get the number of items with higher order
  318. $no = ItemListQuery::create()
  319. ->filterBy_table( $table )
  320. ->filterBy_order( array( 'min' => $highest_order ) )
  321. ->count();
  322. return $no;
  323. }
  324. /**
  325. * Reorder items by new order
  326. *
  327. * @param string $table
  328. * @param string $mm_table_id
  329. * @param int $new_pos
  330. *
  331. * @return int
  332. */
  333. public function multi_reorder( $table, $mm_table_id, $new_pos ) {
  334. $total_no = $this->get_item_lists_no( $table, "all" );
  335. // invalid new position
  336. if( $new_pos < 1 || $new_pos > $total_no ) {
  337. return 1;
  338. }
  339. $table_ids = explode( ',', $mm_table_id );
  340. $reorder_no = sizeof( $table_ids );
  341. $reorder_highest_pos = $this->_get_reorder_highest_pos( $table, $table_ids );
  342. echo "\nreorder highest pos =" .$reorder_highest_pos;
  343. // populate table_ids
  344. $reorders = ItemListQuery::create()
  345. ->filterBy_table( $table )
  346. ->filterBy_table_id( $table_ids )
  347. ->orderBy_order( 'DESC' )
  348. ->find();
  349. // populate the rest
  350. $con = Propel::getConnection();
  351. $sql = "SELECT *
  352. FROM item_list il
  353. WHERE il.table = '{$table}'
  354. AND il.table_id NOT IN ( {$mm_table_id} )
  355. ORDER BY il.order DESC";
  356. $stmt = $con->prepare( $sql );
  357. $stmt->execute();
  358. $rests = ItemListPeer::populateObjects( $stmt );
  359. // in case new position is too low
  360. // lower than the number of moved items then adjust new position
  361. // so that moved items will be at the bottom of the list
  362. $new_pos = ( $new_pos > $total_no -$reorder_no )? $total_no -$reorder_no +1: $new_pos;
  363. echo "\nnew_pos=" .( $new_pos );
  364. if( $new_pos < $reorder_highest_pos ) {
  365. // move items up the list
  366. echo 'move up';
  367. $i = 0;
  368. foreach( $reorders as $reorder ) {
  369. $reorder->set_order( $total_no - $new_pos - $i + 1 );
  370. $reorder->save();
  371. echo ( $total_no - $new_pos - $i + 1 ) ."\n";
  372. $i++;
  373. }
  374. $i = 0;
  375. $j = 0;
  376. echo "rests\n";
  377. foreach( $rests as $rest ) {
  378. if( $i +1 < $new_pos ) {
  379. // reorder the items that are higher than moved items
  380. $rest->set_order( $total_no - $i );
  381. $rest->save();
  382. echo ( $total_no - $i ) ."\n";
  383. $i++;
  384. } else {
  385. // reorder the items that are lower than moved items
  386. $rest->set_order( $total_no -$new_pos +1 -$reorder_no -$j );
  387. $rest->save();
  388. echo ( $total_no -$new_pos +1 -$reorder_no -$j ) ."\n";
  389. $j++;
  390. }
  391. }
  392. }
  393. else {
  394. // move items down the list
  395. echo 'move down';
  396. $i = 0;
  397. foreach( $reorders as $reorder ) {
  398. $reorder->set_order( $total_no - $new_pos - $i + 1 +$reorder_no );
  399. $reorder->save();
  400. echo ( $total_no - $new_pos - $i + 1 +$reorder_no ) ."\n";
  401. $i++;
  402. }
  403. $i = 0;
  404. $j = 0;
  405. echo "rests\n";
  406. foreach( $rests as $rest ) {
  407. if( $i < $new_pos -$reorder_no ) {
  408. // reorder the items that are higher than moved items
  409. $rest->set_order( $total_no - $i );
  410. $rest->save();
  411. echo ( $total_no - $i ) ."\n";
  412. $i++;
  413. } else {
  414. // reorder the items that are lower than moved items
  415. $rest->set_order( $total_no -$new_pos +1 -$j );
  416. $rest->save();
  417. echo ( $total_no -$new_pos +1 -$j ) ."\n";
  418. $j++;
  419. }
  420. }
  421. }
  422. return 0;
  423. }
  424. /**
  425. * Update item_list orders
  426. *
  427. * @param array $id_list
  428. * @param array $order
  429. *
  430. */
  431. public function update_orders( $table, $id_list, $order ) {
  432. $max_order = max( $order );
  433. foreach( $id_list as $i => $id ) {
  434. $item_list = $this->get_item_list_by_table( $table, $id );
  435. $item_list->set_order( $max_order -$i );
  436. $item_list->save();
  437. }
  438. }
  439. /**
  440. * Delete item_list
  441. *
  442. * @param int $item_list_id
  443. *
  444. */
  445. public function delete_item_list( $item_list_id ) {
  446. ItemListQuery::create()->findPk( $item_list_id )->delete();
  447. }
  448. /**
  449. * Delete item_list by table
  450. *
  451. * @param string $table
  452. * @param int $table_id
  453. *
  454. */
  455. public function delete_item_list_by_table( $table, $table_id ) {
  456. ItemListQuery::create()
  457. ->filterBy_table( $table )
  458. ->filterBy_table_id( $table_id )
  459. ->delete();
  460. }
  461. }
  462. ?>