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

/trunk/manage/controllers/content/contentelement.bak.php

https://bitbucket.org/pooshonk/esw
PHP | 575 lines | 466 code | 68 blank | 41 comment | 100 complexity | 86d3233f9e951b60d8cc261a1d58ac8b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class Contentelementcontroller{
  3. private $content = array();
  4. private $type = 'content';
  5. private $contentSQL;
  6. public function __construct(PeacockCarterFrameworkRegistry $registry, $directCall)
  7. {
  8. $this->registry = $registry;
  9. }
  10. public function getContentSQL()
  11. {
  12. return $this->contentSQL;
  13. }
  14. public function toggleActive( $feather )
  15. {
  16. $sql = "UPDATE content SET active=1-active WHERE ID={$feather}";
  17. $this->registry->getObject('db')->executeQuery( $sql );
  18. }
  19. public function toggleSecurity( $feather )
  20. {
  21. $sql = "UPDATE content SET secure=1-secure WHERE ID={$feather}";
  22. $this->registry->getObject('db')->executeQuery( $sql );
  23. }
  24. public function toggleMenu( $feather )
  25. {
  26. $sql = "UPDATE content SET menu=1-menu WHERE ID={$feather}";
  27. $this->registry->getObject('db')->executeQuery( $sql );
  28. }
  29. public function getRevisions( $feather )
  30. {
  31. // get current revision
  32. $sql = "SELECT r.name, DATE_FORMAT(r.created, '%D %b %Y') as created, u.username as author, r.ID as revision_id FROM content_versions r, users u, content f WHERE u.ID=r.author AND r.ID=f.current_revision AND f.id={$feather} AND f.deleted=0";
  33. $cacheCurrent = $this->registry->getObject('db')->cacheQuery( $sql );
  34. // get revision history
  35. $sql = "SELECT p.content_id as feather_id, r.ID as revision_id, r.name, DATE_FORMAT(r.created, '%D %b %Y') as created, DATE_FORMAT(p.date_changed, '%D %b %Y') as dsaa, u.username as author FROM content_versions r, users u, content_versions_history p WHERE u.ID=r.author AND r.ID=p.version_id AND p.content_id={$feather} ORDER BY p.ID";
  36. $cachePrevious = $this->registry->getObject('db')->cacheQuery( $sql );
  37. return array( 'current' => $cacheCurrent, 'previous' => $cachePrevious);
  38. }
  39. public function setContent( $feathers )
  40. {
  41. $this->content = $feathers;
  42. }
  43. public function setType( $type )
  44. {
  45. $this->type = $type;
  46. }
  47. public function getContent()
  48. {
  49. return $this->content;
  50. }
  51. private function findLowestOrder( $feathers )
  52. {
  53. $orders = array();
  54. foreach( $feathers as $feather )
  55. {
  56. $orders[] = $this->content[ $this->type . '-' . $feather]['order'];
  57. }
  58. return min($orders);
  59. }
  60. private function findHighestOrder( $feathers )
  61. {
  62. $orders = array();
  63. foreach( $feathers as $feather )
  64. {
  65. $orders[] = $this->content[$this->type . '-' . feather]['order'];
  66. }
  67. return max($orders);
  68. }
  69. private function getParents( $feather )
  70. {
  71. $parentKey = $this->content[$this->type . '-' . $feather]['parent'];
  72. if( $parentKey == 0 )
  73. {
  74. return array();
  75. }
  76. else
  77. {
  78. if( $this->content[$this->type . '-' .$parentKey]['parent'] == 0 )
  79. {
  80. return array($parentKey);
  81. }
  82. else
  83. {
  84. $pkarray = array($parentKey);
  85. return array_merge( $pkarray, $this->getParents($parentKey) );
  86. }
  87. }
  88. }
  89. /**
  90. * Get IDs for children
  91. * @param $feather int the feather to get children of
  92. * @return array of IDs of children
  93. */
  94. private function getChildren( $feather )
  95. {
  96. $list = array();
  97. foreach( $this->content as $key => $data )
  98. {
  99. if( $data['parent'] == $feather )
  100. {
  101. $list[] = $data['ID'];
  102. $childCatsa = $this->getChildren( $data['ID'] );
  103. $list = array_merge( $list, $childCatsa );
  104. }
  105. }
  106. return $list;
  107. }
  108. public function buildStructure( $extrafields = array(), $extratables = array(), $extraconditions = array(), $extrafirstorder = array(), $extraendorder = array() )
  109. {
  110. $start = microtime();
  111. $extrafields = ( count($extrafields) > 0 ) ? ", " . implode( ", ", $extrafields ) : "";
  112. $extratables = ( count($extratables) > 0 ) ? ", " . implode( ", ", $extratables ) : "";
  113. $extraconditions = ( count($extraconditions) > 0 ) ? " AND " . implode( " AND ", $extraconditions ) : "";
  114. $extrafirstorder = ( count($extrafirstorder) > 0 ) ? " " . implode( ", ", $extrafirstorder ) : "";
  115. $extraendorder = ( count($extraendorder) > 0 ) ? ", " . implode( ", ", $extraendorder ) : "";
  116. $sql = "SELECT r.*, f.ID as fid, f.menu as menu, 1-f.menu as newmenu, f.path as path, f.placeholder, f.active, 1-f.active as newactive, f.parent, f.secure,
  117. 1-f.secure as newsecure, f.order,
  118. (SELECT COUNT(*)+1
  119. FROM content_versions_history
  120. WHERE content_id=f.ID ) AS revisions, u.username as content_author, u.ID as user_id{$extrafields}
  121. FROM content_versions r, content f, content_types t, users u {$extratables}
  122. WHERE u.ID=f.author
  123. AND r.ID=f.current_revision
  124. AND f.type=t.ID
  125. AND t.reference='{$this->type}'
  126. AND f.deleted=0 {$extraconditions}
  127. ORDER BY {$extrafirstorder}f.order, f.ID{$extraendorder} ";
  128. $this->contentSQL = $sql;
  129. $this->registry->getObject('db')->executeQuery( $sql );
  130. $pages = array();
  131. $parentsForFirstChildCheck = array();
  132. while( $page = $this->registry->getObject('db')->getRows() )
  133. {
  134. //echo $page['ID'];
  135. $r = $page['ID'];
  136. $page['ID'] = $page['fid'];
  137. $page['revision_id'] = $r;
  138. $page['levelsdeep'] = 0;
  139. $pages[$this->type . '-' . $page['fid']] = $page;
  140. }
  141. foreach( $pages as $key => $data )
  142. {
  143. if( $data['parent'] == 0 )
  144. {
  145. $pages[ $key ]['levelsdeep'] = 0;
  146. }
  147. elseif( ! isset( $pages[ $this->type. '-'. $data['parent'] ] ) )
  148. {
  149. $pages[ $key ]['levelsdeep'] = 0;
  150. }
  151. else
  152. {
  153. $pages[ $key ]['levelsdeep'] = $pages[ $this->type. '-'. $data['parent'] ]['levelsdeep'] + 1;
  154. }
  155. if( ! in_array( $data['parent'], $parentsForFirstChildCheck ) )
  156. {
  157. $parentsForFirstChildCheck[] = $data['parent'];
  158. $pages[ $key ]['position'] = 'first-child';
  159. }
  160. }
  161. $this->content = $pages;
  162. $newpages = array();
  163. $k = 0;
  164. foreach( $pages as $key => $page )
  165. {
  166. $indents = '';
  167. for( $i = 0; $i < $page['levelsdeep']; $i++ )
  168. {
  169. $indents .= '&nbsp;&nbsp;&nbsp;&nbsp;';
  170. }
  171. $page['indents'] = $indents;
  172. //echo $page['name'];
  173. $page['style'] = ( $k % 2) ? 'alt' : 'norm';
  174. $page['W'] = ( $k % 2) ? '' : 'W';
  175. $page['upstyle'] = ( $k == 0 ) ? 'off' : '';
  176. $page['upcomleft'] = ( $k == 0 ) ? '<!-- ' : '';
  177. $page['upcomright'] = ( $k == 0 ) ? ' -->' : '';
  178. $page['downstyle'] = '';
  179. $page['downcomleft'] = '';
  180. $page['downcomright'] = '';
  181. // actionstr types
  182. if( $this->type == 'page' )
  183. {
  184. $page['actionstr'] = 'page';
  185. }
  186. else if( $this->type == 'blog' )
  187. {
  188. $page['actionstr'] = 'blog';
  189. }
  190. else if( $this->type == 'directory' )
  191. {
  192. $page['actionstr'] = 'directory';
  193. }
  194. else if ( $this->type == 'image')
  195. {
  196. $page['actionstr'] = 'image';
  197. }
  198. else if ( $this->type == 'stadium')
  199. {
  200. $page['actionstr'] = 'stadium';
  201. }
  202. else if ( $this->type == 'league')
  203. {
  204. $page['actionstr'] = 'league';
  205. }
  206. else if ( $this->type == 'club')
  207. {
  208. $page['actionstr'] = 'club';
  209. }
  210. $page['usercomleft'] = '';
  211. $page['usercomright'] = '';
  212. $page['contentlinkstart'] = '';
  213. $page['contentlinkend'] = '';
  214. if( $page['placeholder'] == 1 )
  215. {
  216. $page['username'] = 'system';
  217. $page['actionstr'] = 'placeholder';
  218. $page['contentlinkstart'] = '<!-- ';
  219. $page['contentlinkend'] = '--> ';
  220. $page['usercomleft'] = '<!-- ';
  221. $page['usercomright'] = ' -->';
  222. }
  223. $newpages[] = $page;
  224. $k++;
  225. }
  226. if( count( $newpages ) > 0 )
  227. {
  228. $newpages[$k-1]['downstyle'] = 'off';
  229. $newpages[$k-1]['downcomleft'] = '<!-- ';
  230. $newpages[$k-1]['downcomright'] = ' -->';
  231. }
  232. $pages_cache =$this->registry->getObject('db')->cacheData( $newpages );
  233. $this->cacheID = $pages_cache;
  234. if( $this->type == 'page' )
  235. {
  236. $this->registry->getObject('template')->getPage()->addTag('pages', array( 'DATA', $pages_cache ) );
  237. }
  238. else if( $this->type == 'blog' )
  239. {
  240. $this->registry->getObject('template')->getPage()->addTag('blogs', array( 'DATA', $pages_cache ) );
  241. }
  242. elseif( $this->type == 'directory' )
  243. {
  244. $this->registry->getObject('template')->getPage()->addTag('directory', array( 'DATA', $pages_cache ) );
  245. }
  246. elseif( $this->type == 'album' )
  247. {
  248. $this->registry->getObject('template')->getPage()->addTag('albums', array( 'DATA', $pages_cache ) );
  249. }
  250. elseif( $this->type == 'image' )
  251. {
  252. $this->registry->getObject('template')->getPage()->addTag('images', array( 'DATA', $pages_cache ) );
  253. }
  254. elseif( $this->type == 'product-cat' )
  255. {
  256. $this->registry->getObject('template')->getPage()->addTag('productcats', array( 'DATA', $pages_cache ) );
  257. }
  258. elseif( $this->type == 'product' )
  259. {
  260. $this->registry->getObject('template')->getPage()->addTag('products', array( 'DATA', $pages_cache ) );
  261. }
  262. elseif( $this->type == 'stadium' )
  263. {
  264. $this->registry->getObject('template')->getPage()->addTag('stadiums', array( 'DATA', $pages_cache ) );
  265. }
  266. elseif( $this->type == 'league' )
  267. {
  268. $this->registry->getObject('template')->getPage()->addTag('leagues', array( 'DATA', $pages_cache ) );
  269. }
  270. elseif( $this->type == 'club' )
  271. {
  272. $this->registry->getObject('template')->getPage()->addTag('clubs', array( 'DATA', $pages_cache ) );
  273. }
  274. }
  275. public function getCacheID()
  276. {
  277. return $this->cacheID;
  278. }
  279. public function moveLeft( $content )
  280. {
  281. $this->buildStructure();
  282. /**
  283. Move Left:
  284. if parent=0
  285. can't move left
  286. else
  287. Find page where order < current page order AND parent in Parent Pages(excl. current parent) get highest order
  288. */
  289. // ADDED: moving left needs to check if the element above has children, if so, it needs to change the order to be less than the lowest child of that item
  290. if( $this->content[$this->type . '-'. $content ]['parent'] != 0 )
  291. {
  292. $parents = $this->getParents( $content );
  293. $parentkeys = array();
  294. foreach( $parents as $parent )
  295. {
  296. $parentkeys[] = $this->type . '-' . $parent;
  297. }
  298. $highestorder = 0;
  299. $newparent = 0;
  300. $abovepage = 0;
  301. foreach( $this->content as $key => $data )
  302. {
  303. if( ( in_array( $key, $parentkeys ) ) && ( $data['order'] < $this->content[$this->type. '-' . $content]['order'] ) && ( $this->content[$key]['parent'] != $this->content[$this->type . '-' . $content]['parent'] ) && ( $key != $this->type . '-'.$this->content[$this->type . '-' .$content]['parent'] ) )
  304. {
  305. if( $data['order'] > $highestorder )
  306. {
  307. $highestorder = $data['order'];
  308. $newparent = $data['ID'];
  309. }
  310. }
  311. if( $data['order'] == ($this->content[$this->type .'-' . $content]['order'] -1) )
  312. {
  313. $abovepage = $data['ID'];
  314. }
  315. }
  316. $changes = array();
  317. $changes['parent'] = $newparent;
  318. $this->registry->getObject('db')->updateRecords('content', $changes,"ID=" . $content );
  319. if( $abovepage != 0 )
  320. {
  321. $this->buildStructure();
  322. $children = $this->getChildren( $abovepage );
  323. if( !empty( $children ) )
  324. {
  325. //echo 'move up';
  326. $this->buildStructure();
  327. $this->movePageUp( $content );
  328. }
  329. }
  330. return true;
  331. }
  332. else
  333. {
  334. return false;
  335. }
  336. }
  337. public function moveRight( $content )
  338. {
  339. $this->buildStructure();
  340. /*
  341. Move Right:
  342. Find page where order < current page order AND parent ! in Parent Pages get highest order
  343. IF none:
  344. can't move right
  345. Else
  346. update parent to parent of found page
  347. */
  348. //if( $this->pages['page-'. $page ]['parent'] != 0 )
  349. //{
  350. $parents = $this->getParents( $content );
  351. $parentkeys = array();
  352. foreach( $parents as $parent )
  353. {
  354. $parentkeys[] = $this->type . '-' . $parent;
  355. }
  356. $highestorder = 0;
  357. $newparent = $this->content[$this->type . '-' . $content]['parent'];
  358. foreach( $this->content as $key => $data )
  359. {
  360. if( ( $data['parent'] == $this->content[$this->type . '-'.$content]['parent'] ) && ( $data['order'] < $this->content[$this->type . '-' . $content]['order'] ) )
  361. {
  362. if( $data['order'] > $highestorder || $highestorder == 0 )
  363. {
  364. $highestorder = $data['order'];
  365. $newparent = $data['ID'];
  366. //echo $data['ID'];
  367. }
  368. }
  369. }
  370. $this->registry->getObject('db')->updateRecords('content', array('parent'=>$newparent),"ID=" . $content );
  371. }
  372. public function moveUp( $content, $constantParent = false )
  373. {
  374. $this->buildStructure();
  375. if( $this->content[$this->type. '-' . $content ]['order'] != 1 )
  376. {
  377. $children = $this->getChildren( $content );
  378. //echo '<pre>' . print_r( $children, true ) . '</pre>';
  379. $pagesatsamelevelorder = 0;
  380. $abovepageatsamelevel = 0;
  381. foreach( $this->content as $key => $pagedata )
  382. {
  383. if( $pagedata['parent'] == $this->content[$this->type . '-' . $content]['parent'] && $pagedata['order'] > $pagesatsamelevelorder && $pagedata['order'] < $this->content[$this->type . '-' . $content]['order'] )
  384. {
  385. $abovepageatsamelevel = $pagedata['ID'];
  386. //echo 'above page ' . $abovepageatsamelevel . '<br />';
  387. $pagesatsamelevelorder = $pagedata['order'];
  388. }
  389. }
  390. if( $abovepageatsamelevel == 0 && $this->content[$this->type . '-' . $content]['parent'] != 0 && $constantParent == false )
  391. {
  392. $p = $this->content[$this->type . '-' . $content]['parent'];
  393. $pagesatsamelevelorder = $this->content[$this->type . '-'. $p]['order'];
  394. $parent = $this->content[$this->type . '-'. $p]['parent'];
  395. $sql = "UPDATE content SET `parent`={$parent} WHERE ID=" . $content;
  396. //echo $sql. '<br />';
  397. $this->registry->getObject('db')->executeQuery( $sql );
  398. }
  399. $amount = count( $children ) + 1;
  400. $page_type_sql = "SELECT ID FROM content_types WHERE reference='{$this->type}'";
  401. $this->registry->getObject('db')->executeQuery( $page_type_sql );
  402. $ptdata = $this->registry->getObject('db')->getRows();
  403. $type = $ptdata['ID'];
  404. $const = ( $constantParent == true ) ? " parent=".$this->content[$this->type . '-' . $content]['parent']." AND " : " ";
  405. $sql = "UPDATE content SET `order`=(`order`+{$amount}) WHERE {$const} `type`={$type} AND `deleted`=0 AND `order`>={$pagesatsamelevelorder} AND `order` < " . ($this->content[$this->type . '-' . $content ]['order'] );
  406. $this->registry->getObject('db')->executeQuery( $sql );
  407. $sql = "UPDATE content SET `order`={$pagesatsamelevelorder} WHERE ID=" . $content;
  408. //echo $sql. '<br />';
  409. $this->registry->getObject('db')->executeQuery( $sql );
  410. if( !empty( $children) )
  411. {
  412. $new = $this->content[$this->type . '-' . $content]['order'] - $pagesatsamelevelorder;
  413. $children = implode( ',', $children );
  414. $sql = "UPDATE content SET `order`=`order`-{$new} WHERE {$const} `type`={$type} AND ID IN ({$children})";
  415. //echo 'move children up ' . $sql . '<br />';
  416. $this->registry->getObject('db')->executeQuery( $sql );
  417. }
  418. //$this->buildSiteStructure();
  419. }
  420. }
  421. public function moveDown( $content, $constParent = false )
  422. {
  423. $this->buildStructure();
  424. $page_type_sql = "SELECT ID FROM content_types WHERE reference='{$this->type}'";
  425. $this->registry->getObject('db')->executeQuery( $page_type_sql );
  426. $ptdata = $this->registry->getObject('db')->getRows();
  427. $type = $ptdata['ID'];
  428. $children = $this->getChildren( $content );
  429. // if we are the lowest child in a list, and we are not in a situation where parents are constant, we don't move we simply move left.
  430. if( $this->content[$this->type . '-' . $content]['parent'] != 0 && $constParent == false )
  431. {
  432. //$cop = $this->getChildren( $this->content[$this->type . '-' . $content]['parent'] );
  433. $parent = $this->content[$this->type . '-' . $content]['parent'];
  434. $sql = "SELECT f.id as cid FROM content f, content d WHERE f.type='{$type}' AND f.order > d.order AND f.deleted=0 AND d.ID={$content} AND f.parent={$parent} ORDER BY f.order LIMIT 1";
  435. $this->registry->getObject('db')->executeQuery( $sql );
  436. if( $this->registry->getObject('db')->numRows() == 0 )
  437. {
  438. $this->moveLeft( $content );
  439. }
  440. else
  441. {
  442. if( ! empty( $children ) )
  443. {
  444. $children = implode( ',', $children );
  445. $sql = "SELECT f.id as cid FROM content f, content d WHERE f.ID NOT IN ({$children}) AND f.type='{$type}' AND f.order > d.order AND f.deleted=0 AND d.ID={$content} ORDER BY f.order LIMIT 1";
  446. $this->registry->getObject('db')->executeQuery( $sql );
  447. if( $this->registry->getObject('db')->numRows() == 1 )
  448. {
  449. $data = $this->registry->getObject('db')->getRows();
  450. //echo 'has children' . $data['cid'] . '<br />';
  451. $this->moveUp( $data['cid'], $constParent );
  452. }
  453. }
  454. else
  455. {
  456. $sql = "SELECT f.id as cid FROM content f, content d WHERE f.type='{$type}' AND f.order > d.order AND f.deleted=0 AND d.ID={$content } ORDER BY f.order LIMIT 1";
  457. $this->registry->getObject('db')->executeQuery( $sql );
  458. if( $this->registry->getObject('db')->numRows() == 1 )
  459. {
  460. $data = $this->registry->getObject('db')->getRows();
  461. $this->moveUp( $data['cid'], $constParent );
  462. }
  463. }
  464. }
  465. }
  466. else
  467. {
  468. if( ! empty( $children ) )
  469. {
  470. $children = implode( ',', $children );
  471. $sql = "SELECT f.id as cid FROM content f, content d WHERE f.ID NOT IN ({$children}) AND f.type='{$type}' AND f.order > d.order AND f.deleted=0 AND d.ID={$content} ORDER BY f.order LIMIT 1";
  472. $this->registry->getObject('db')->executeQuery( $sql );
  473. if( $this->registry->getObject('db')->numRows() == 1 )
  474. {
  475. $data = $this->registry->getObject('db')->getRows();
  476. //echo 'has children' . $data['cid'] . '<br />';
  477. $this->moveUp( $data['cid'], $constParent );
  478. }
  479. }
  480. else
  481. {
  482. $const = ( $constParent == true ) ? " f.parent=".$this->content[$this->type . '-' . $content]['parent']." AND " : " ";
  483. $sql = "SELECT f.id as cid FROM content f, content d WHERE {$const} f.type='{$type}' AND f.order > d.order AND f.deleted=0 AND d.ID={$content } ORDER BY f.order LIMIT 1";
  484. $this->registry->getObject('db')->executeQuery( $sql );
  485. if( $this->registry->getObject('db')->numRows() == 1 )
  486. {
  487. $data = $this->registry->getObject('db')->getRows();
  488. $this->moveUp( $data['cid'], $constParent );
  489. }
  490. }
  491. }
  492. }
  493. public function delete( $content )
  494. {
  495. $content = intval( $content );
  496. $sql = "SELECT `order` FROM content WHERE ID=" . $content;
  497. $this->registry->getObject('db')->executeQuery( $sql );
  498. $data = $this->registry->getObject('db')->getRows();
  499. $changes = array( 'deleted' => 1, 'order' => -100 );
  500. $this->registry->getObject('db')->updateRecords( 'content', $changes, 'ID=' . $content);
  501. $order = $data['order'];
  502. $sql = "UPDATE content c, content_types t SET c.`order`=c.`order`-1 WHERE c.type=t.ID and t.reference='{$this->type}' AND `order` > {$order} ";
  503. $this->registry->getObject('db')->executeQuery( $sql );
  504. }
  505. }
  506. ?>