/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
- <?php
-
- class Contentelementcontroller{
-
- private $content = array();
- private $type = 'content';
- private $contentSQL;
-
- public function __construct(PeacockCarterFrameworkRegistry $registry, $directCall)
- {
- $this->registry = $registry;
- }
-
- public function getContentSQL()
- {
- return $this->contentSQL;
- }
-
- public function toggleActive( $feather )
- {
- $sql = "UPDATE content SET active=1-active WHERE ID={$feather}";
- $this->registry->getObject('db')->executeQuery( $sql );
- }
-
- public function toggleSecurity( $feather )
- {
- $sql = "UPDATE content SET secure=1-secure WHERE ID={$feather}";
- $this->registry->getObject('db')->executeQuery( $sql );
- }
-
- public function toggleMenu( $feather )
- {
- $sql = "UPDATE content SET menu=1-menu WHERE ID={$feather}";
- $this->registry->getObject('db')->executeQuery( $sql );
- }
-
- public function getRevisions( $feather )
- {
- // get current revision
- $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";
- $cacheCurrent = $this->registry->getObject('db')->cacheQuery( $sql );
- // get revision history
- $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";
- $cachePrevious = $this->registry->getObject('db')->cacheQuery( $sql );
- return array( 'current' => $cacheCurrent, 'previous' => $cachePrevious);
-
- }
-
- public function setContent( $feathers )
- {
- $this->content = $feathers;
- }
-
- public function setType( $type )
- {
- $this->type = $type;
- }
-
- public function getContent()
- {
- return $this->content;
- }
-
-
- private function findLowestOrder( $feathers )
- {
- $orders = array();
- foreach( $feathers as $feather )
- {
- $orders[] = $this->content[ $this->type . '-' . $feather]['order'];
-
- }
- return min($orders);
- }
-
- private function findHighestOrder( $feathers )
- {
- $orders = array();
- foreach( $feathers as $feather )
- {
- $orders[] = $this->content[$this->type . '-' . feather]['order'];
-
- }
- return max($orders);
- }
-
- private function getParents( $feather )
- {
- $parentKey = $this->content[$this->type . '-' . $feather]['parent'];
- if( $parentKey == 0 )
- {
- return array();
- }
- else
- {
- if( $this->content[$this->type . '-' .$parentKey]['parent'] == 0 )
- {
- return array($parentKey);
- }
- else
- {
- $pkarray = array($parentKey);
- return array_merge( $pkarray, $this->getParents($parentKey) );
- }
- }
-
- }
-
- /**
- * Get IDs for children
- * @param $feather int the feather to get children of
- * @return array of IDs of children
- */
- private function getChildren( $feather )
- {
- $list = array();
- foreach( $this->content as $key => $data )
- {
- if( $data['parent'] == $feather )
- {
- $list[] = $data['ID'];
- $childCatsa = $this->getChildren( $data['ID'] );
- $list = array_merge( $list, $childCatsa );
- }
- }
- return $list;
- }
-
-
-
- public function buildStructure( $extrafields = array(), $extratables = array(), $extraconditions = array(), $extrafirstorder = array(), $extraendorder = array() )
- {
- $start = microtime();
- $extrafields = ( count($extrafields) > 0 ) ? ", " . implode( ", ", $extrafields ) : "";
- $extratables = ( count($extratables) > 0 ) ? ", " . implode( ", ", $extratables ) : "";
- $extraconditions = ( count($extraconditions) > 0 ) ? " AND " . implode( " AND ", $extraconditions ) : "";
- $extrafirstorder = ( count($extrafirstorder) > 0 ) ? " " . implode( ", ", $extrafirstorder ) : "";
- $extraendorder = ( count($extraendorder) > 0 ) ? ", " . implode( ", ", $extraendorder ) : "";
-
-
- $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,
- 1-f.secure as newsecure, f.order,
- (SELECT COUNT(*)+1
- FROM content_versions_history
- WHERE content_id=f.ID ) AS revisions, u.username as content_author, u.ID as user_id{$extrafields}
- FROM content_versions r, content f, content_types t, users u {$extratables}
- WHERE u.ID=f.author
- AND r.ID=f.current_revision
- AND f.type=t.ID
- AND t.reference='{$this->type}'
- AND f.deleted=0 {$extraconditions}
- ORDER BY {$extrafirstorder}f.order, f.ID{$extraendorder} ";
- $this->contentSQL = $sql;
- $this->registry->getObject('db')->executeQuery( $sql );
- $pages = array();
- $parentsForFirstChildCheck = array();
- while( $page = $this->registry->getObject('db')->getRows() )
- {
- //echo $page['ID'];
- $r = $page['ID'];
- $page['ID'] = $page['fid'];
- $page['revision_id'] = $r;
- $page['levelsdeep'] = 0;
- $pages[$this->type . '-' . $page['fid']] = $page;
- }
- foreach( $pages as $key => $data )
- {
- if( $data['parent'] == 0 )
- {
- $pages[ $key ]['levelsdeep'] = 0;
- }
- elseif( ! isset( $pages[ $this->type. '-'. $data['parent'] ] ) )
- {
- $pages[ $key ]['levelsdeep'] = 0;
- }
- else
- {
- $pages[ $key ]['levelsdeep'] = $pages[ $this->type. '-'. $data['parent'] ]['levelsdeep'] + 1;
- }
- if( ! in_array( $data['parent'], $parentsForFirstChildCheck ) )
- {
- $parentsForFirstChildCheck[] = $data['parent'];
- $pages[ $key ]['position'] = 'first-child';
- }
- }
- $this->content = $pages;
-
- $newpages = array();
- $k = 0;
- foreach( $pages as $key => $page )
- {
- $indents = '';
- for( $i = 0; $i < $page['levelsdeep']; $i++ )
- {
- $indents .= ' ';
- }
- $page['indents'] = $indents;
- //echo $page['name'];
- $page['style'] = ( $k % 2) ? 'alt' : 'norm';
- $page['W'] = ( $k % 2) ? '' : 'W';
- $page['upstyle'] = ( $k == 0 ) ? 'off' : '';
- $page['upcomleft'] = ( $k == 0 ) ? '<!-- ' : '';
- $page['upcomright'] = ( $k == 0 ) ? ' -->' : '';
- $page['downstyle'] = '';
- $page['downcomleft'] = '';
- $page['downcomright'] = '';
-
- // actionstr types
- if( $this->type == 'page' )
- {
- $page['actionstr'] = 'page';
- }
- else if( $this->type == 'blog' )
- {
- $page['actionstr'] = 'blog';
- }
- else if( $this->type == 'directory' )
- {
- $page['actionstr'] = 'directory';
- }
- else if ( $this->type == 'image')
- {
- $page['actionstr'] = 'image';
- }
- else if ( $this->type == 'stadium')
- {
- $page['actionstr'] = 'stadium';
- }
- else if ( $this->type == 'league')
- {
- $page['actionstr'] = 'league';
- }
- else if ( $this->type == 'club')
- {
- $page['actionstr'] = 'club';
- }
-
-
- $page['usercomleft'] = '';
- $page['usercomright'] = '';
- $page['contentlinkstart'] = '';
- $page['contentlinkend'] = '';
-
- if( $page['placeholder'] == 1 )
- {
- $page['username'] = 'system';
- $page['actionstr'] = 'placeholder';
- $page['contentlinkstart'] = '<!-- ';
- $page['contentlinkend'] = '--> ';
- $page['usercomleft'] = '<!-- ';
- $page['usercomright'] = ' -->';
-
-
- }
-
-
- $newpages[] = $page;
-
- $k++;
- }
-
- if( count( $newpages ) > 0 )
- {
- $newpages[$k-1]['downstyle'] = 'off';
- $newpages[$k-1]['downcomleft'] = '<!-- ';
- $newpages[$k-1]['downcomright'] = ' -->';
- }
-
- $pages_cache =$this->registry->getObject('db')->cacheData( $newpages );
- $this->cacheID = $pages_cache;
-
- if( $this->type == 'page' )
- {
- $this->registry->getObject('template')->getPage()->addTag('pages', array( 'DATA', $pages_cache ) );
- }
- else if( $this->type == 'blog' )
- {
- $this->registry->getObject('template')->getPage()->addTag('blogs', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'directory' )
- {
- $this->registry->getObject('template')->getPage()->addTag('directory', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'album' )
- {
- $this->registry->getObject('template')->getPage()->addTag('albums', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'image' )
- {
- $this->registry->getObject('template')->getPage()->addTag('images', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'product-cat' )
- {
- $this->registry->getObject('template')->getPage()->addTag('productcats', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'product' )
- {
- $this->registry->getObject('template')->getPage()->addTag('products', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'stadium' )
- {
- $this->registry->getObject('template')->getPage()->addTag('stadiums', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'league' )
- {
- $this->registry->getObject('template')->getPage()->addTag('leagues', array( 'DATA', $pages_cache ) );
- }
- elseif( $this->type == 'club' )
- {
- $this->registry->getObject('template')->getPage()->addTag('clubs', array( 'DATA', $pages_cache ) );
- }
-
-
- }
-
- public function getCacheID()
- {
- return $this->cacheID;
- }
-
- public function moveLeft( $content )
- {
- $this->buildStructure();
- /**
- Move Left:
- if parent=0
- can't move left
- else
- Find page where order < current page order AND parent in Parent Pages(excl. current parent) get highest order
-
- */
-
- // 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
-
- if( $this->content[$this->type . '-'. $content ]['parent'] != 0 )
- {
- $parents = $this->getParents( $content );
- $parentkeys = array();
- foreach( $parents as $parent )
- {
- $parentkeys[] = $this->type . '-' . $parent;
- }
- $highestorder = 0;
- $newparent = 0;
- $abovepage = 0;
- foreach( $this->content as $key => $data )
- {
- 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'] ) )
- {
-
- if( $data['order'] > $highestorder )
- {
- $highestorder = $data['order'];
- $newparent = $data['ID'];
- }
- }
- if( $data['order'] == ($this->content[$this->type .'-' . $content]['order'] -1) )
- {
- $abovepage = $data['ID'];
- }
- }
- $changes = array();
- $changes['parent'] = $newparent;
- $this->registry->getObject('db')->updateRecords('content', $changes,"ID=" . $content );
- if( $abovepage != 0 )
- {
- $this->buildStructure();
- $children = $this->getChildren( $abovepage );
- if( !empty( $children ) )
- {
- //echo 'move up';
- $this->buildStructure();
- $this->movePageUp( $content );
- }
- }
-
-
-
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public function moveRight( $content )
- {
- $this->buildStructure();
- /*
- Move Right:
- Find page where order < current page order AND parent ! in Parent Pages get highest order
- IF none:
- can't move right
- Else
- update parent to parent of found page
- */
- //if( $this->pages['page-'. $page ]['parent'] != 0 )
- //{
- $parents = $this->getParents( $content );
- $parentkeys = array();
- foreach( $parents as $parent )
- {
- $parentkeys[] = $this->type . '-' . $parent;
- }
-
- $highestorder = 0;
- $newparent = $this->content[$this->type . '-' . $content]['parent'];
- foreach( $this->content as $key => $data )
- {
-
- if( ( $data['parent'] == $this->content[$this->type . '-'.$content]['parent'] ) && ( $data['order'] < $this->content[$this->type . '-' . $content]['order'] ) )
- {
- if( $data['order'] > $highestorder || $highestorder == 0 )
- {
- $highestorder = $data['order'];
- $newparent = $data['ID'];
- //echo $data['ID'];
- }
- }
- }
- $this->registry->getObject('db')->updateRecords('content', array('parent'=>$newparent),"ID=" . $content );
- }
-
- public function moveUp( $content, $constantParent = false )
- {
- $this->buildStructure();
- if( $this->content[$this->type. '-' . $content ]['order'] != 1 )
- {
- $children = $this->getChildren( $content );
- //echo '<pre>' . print_r( $children, true ) . '</pre>';
- $pagesatsamelevelorder = 0;
- $abovepageatsamelevel = 0;
- foreach( $this->content as $key => $pagedata )
- {
- if( $pagedata['parent'] == $this->content[$this->type . '-' . $content]['parent'] && $pagedata['order'] > $pagesatsamelevelorder && $pagedata['order'] < $this->content[$this->type . '-' . $content]['order'] )
- {
- $abovepageatsamelevel = $pagedata['ID'];
- //echo 'above page ' . $abovepageatsamelevel . '<br />';
- $pagesatsamelevelorder = $pagedata['order'];
- }
- }
- if( $abovepageatsamelevel == 0 && $this->content[$this->type . '-' . $content]['parent'] != 0 && $constantParent == false )
- {
- $p = $this->content[$this->type . '-' . $content]['parent'];
- $pagesatsamelevelorder = $this->content[$this->type . '-'. $p]['order'];
- $parent = $this->content[$this->type . '-'. $p]['parent'];
- $sql = "UPDATE content SET `parent`={$parent} WHERE ID=" . $content;
- //echo $sql. '<br />';
- $this->registry->getObject('db')->executeQuery( $sql );
-
- }
- $amount = count( $children ) + 1;
- $page_type_sql = "SELECT ID FROM content_types WHERE reference='{$this->type}'";
- $this->registry->getObject('db')->executeQuery( $page_type_sql );
- $ptdata = $this->registry->getObject('db')->getRows();
- $type = $ptdata['ID'];
- $const = ( $constantParent == true ) ? " parent=".$this->content[$this->type . '-' . $content]['parent']." AND " : " ";
- $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'] );
- $this->registry->getObject('db')->executeQuery( $sql );
- $sql = "UPDATE content SET `order`={$pagesatsamelevelorder} WHERE ID=" . $content;
- //echo $sql. '<br />';
- $this->registry->getObject('db')->executeQuery( $sql );
- if( !empty( $children) )
- {
- $new = $this->content[$this->type . '-' . $content]['order'] - $pagesatsamelevelorder;
- $children = implode( ',', $children );
- $sql = "UPDATE content SET `order`=`order`-{$new} WHERE {$const} `type`={$type} AND ID IN ({$children})";
- //echo 'move children up ' . $sql . '<br />';
- $this->registry->getObject('db')->executeQuery( $sql );
- }
- //$this->buildSiteStructure();
-
- }
- }
-
- public function moveDown( $content, $constParent = false )
- {
- $this->buildStructure();
- $page_type_sql = "SELECT ID FROM content_types WHERE reference='{$this->type}'";
- $this->registry->getObject('db')->executeQuery( $page_type_sql );
- $ptdata = $this->registry->getObject('db')->getRows();
- $type = $ptdata['ID'];
- $children = $this->getChildren( $content );
-
- // 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.
- if( $this->content[$this->type . '-' . $content]['parent'] != 0 && $constParent == false )
- {
- //$cop = $this->getChildren( $this->content[$this->type . '-' . $content]['parent'] );
- $parent = $this->content[$this->type . '-' . $content]['parent'];
- $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";
- $this->registry->getObject('db')->executeQuery( $sql );
-
- if( $this->registry->getObject('db')->numRows() == 0 )
- {
- $this->moveLeft( $content );
- }
- else
- {
- if( ! empty( $children ) )
- {
- $children = implode( ',', $children );
- $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";
- $this->registry->getObject('db')->executeQuery( $sql );
- if( $this->registry->getObject('db')->numRows() == 1 )
- {
- $data = $this->registry->getObject('db')->getRows();
- //echo 'has children' . $data['cid'] . '<br />';
- $this->moveUp( $data['cid'], $constParent );
- }
- }
- else
- {
- $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";
- $this->registry->getObject('db')->executeQuery( $sql );
- if( $this->registry->getObject('db')->numRows() == 1 )
- {
- $data = $this->registry->getObject('db')->getRows();
- $this->moveUp( $data['cid'], $constParent );
- }
- }
- }
- }
- else
- {
-
- if( ! empty( $children ) )
- {
- $children = implode( ',', $children );
- $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";
- $this->registry->getObject('db')->executeQuery( $sql );
- if( $this->registry->getObject('db')->numRows() == 1 )
- {
- $data = $this->registry->getObject('db')->getRows();
- //echo 'has children' . $data['cid'] . '<br />';
- $this->moveUp( $data['cid'], $constParent );
- }
- }
- else
- {
- $const = ( $constParent == true ) ? " f.parent=".$this->content[$this->type . '-' . $content]['parent']." AND " : " ";
-
- $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";
-
- $this->registry->getObject('db')->executeQuery( $sql );
- if( $this->registry->getObject('db')->numRows() == 1 )
- {
- $data = $this->registry->getObject('db')->getRows();
- $this->moveUp( $data['cid'], $constParent );
- }
- }
-
-
- }
-
- }
-
- public function delete( $content )
- {
- $content = intval( $content );
- $sql = "SELECT `order` FROM content WHERE ID=" . $content;
- $this->registry->getObject('db')->executeQuery( $sql );
- $data = $this->registry->getObject('db')->getRows();
- $changes = array( 'deleted' => 1, 'order' => -100 );
- $this->registry->getObject('db')->updateRecords( 'content', $changes, 'ID=' . $content);
- $order = $data['order'];
- $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} ";
- $this->registry->getObject('db')->executeQuery( $sql );
- }
-
- }
-
-
-
- ?>