Advertisement
fabi0

Untitled

May 26th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.12 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  * Description of Category
  11.  *
  12.  * @author fabi0
  13.  */
  14.  
  15. namespace Models;
  16.  
  17. class Category {
  18.  
  19.     private $_query;
  20.  
  21.     public function __construct($databaseInstance) {
  22.         $this->_query = $databaseInstance;
  23.     }
  24.  
  25.     public function getCategoryTree($category_id) {
  26.         $sql = "
  27.            SELECT node.category_id, node.category_name FROM categories AS node, categories AS parent
  28.            WHERE node.category_left BETWEEN parent.category_left AND parent.category_right AND parent.category_id = :id
  29.            ORDER BY node.category_left;";
  30.         $params = array(
  31.             ':id' => array(
  32.                 'param' => $category_id,
  33.                 'type' => 1
  34.             )
  35.         );
  36.         $this->_query->query($sql, $params);
  37.         if ($this->_query->getResult()) {
  38.             return getResult();
  39.         }
  40.         return false;
  41.     }
  42.  
  43.     public function getCategoryDepth($arrange = false, $resultType = 5) {
  44.  
  45.         $result = array();
  46.         $sql = "SELECT category_id,category_parent,category_depth,category_name FROM categories
  47.            ORDER BY categories.category_left;
  48.            SELECT node.category_id, node.category_name, (COUNT(parent.category_name) - 1) AS category_depth
  49.            FROM categories AS node,categories AS parent
  50.            WHERE node.category_left BETWEEN parent.category_left AND parent.category_right
  51.            GROUP BY node.category_id
  52.            ORDER BY node.category_left;";
  53.         $query = $this->_query->query($sql, NULL, $resultType)->getResult();
  54.         if ($arrange) {
  55.             foreach ($query as $value) {
  56.                 $result[$value->category_id]['category_name'] = $value->category_name;
  57.                 $result[$value->category_id]['category_depth'] = $value->category_depth;
  58.                 $result[$value->category_id]['category_parent'] = $value->category_parent;
  59.             }
  60.             return $result;
  61.         }
  62.         return $query;
  63.     }
  64.  
  65.     public function getCategoryWithPosts() {
  66.         $sql = "
  67.            SELECT parent.category_id, COUNT(discussions.discussion_id) as category_posts
  68.            FROM categories AS node ,categories AS parent,discussions
  69.            WHERE node.category_left BETWEEN parent.category_left
  70.            AND parent.category_right AND node.category_id = discussions.discussion_category
  71.            GROUP BY parent.category_id
  72.            ORDER BY node.category_left";
  73.  
  74.         return $this->_query->query($sql)->getResult();
  75.     }
  76.  
  77.     public function getFullCategoryTreeWithPostsCount() {
  78.         $cat1 = $this->getCategoryDepth();
  79.         $cat2 = $this->getCategoryWithPosts();
  80.         $newArray = array();
  81.         foreach ($cat1 as $k => $v) {
  82.  
  83.             $newArray[$v->category_id]['category_name'] = $v->category_name;
  84.             $newArray[$v->category_id]['category_depth'] = $v->category_depth;
  85.         }
  86.  
  87.         foreach ($cat2 as $k => $vv) {
  88.             $newArray[$vv->category_id]['category_posts'] = $vv->category_posts;
  89.         }
  90.  
  91.         foreach ($newArray as $key => $value) {
  92.             if (!isset($newArray[$key]['category_posts'])) {
  93.                 $newArray[$key]['category_posts'] = 0;
  94.             }
  95.         }
  96.         return $newArray;
  97.     }
  98.  
  99.     public function getAllPost() {
  100.         $sql = "SELECT count(discussion_id) as all_post FROM discussions WHERE discussion_name IS NOT NULL";
  101.         return $this->_query->query($sql)->getResult();
  102.     }
  103.  
  104.     public function renameCategory($id, $newName) {
  105.         $sql = "UPDATE categories SET category_name = :name WHERE category_id = :id;";
  106.         $params = array(
  107.             ':name' => array(
  108.                 'param' => $newName,
  109.                 'type' => 2
  110.             ),
  111.             ':id' => array(
  112.                 'param' => $id,
  113.                 'type' => 1
  114.             )
  115.         );
  116.         return $this->_query->query($sql, $params);
  117.     }
  118.  
  119.     public function deleteCategory($category_id) {
  120.         $sql = "
  121.            LOCK TABLE categories WRITE;
  122.            SELECT @myLeft := category_left, @myRight := category_right, @myWidth := category_right - category_left + 1
  123.            FROM categories WHERE category_id = :id;
  124.            DELETE FROM categories WHERE category_left BETWEEN @myLeft AND @myRight;
  125.            UPDATE categories SET category_right = category_right - @myWidth WHERE category_right > @myRight;
  126.            UPDATE categories SET category_left = category_left - @myWidth WHERE category_left > @myRight;
  127.            UNLOCK TABLES;";
  128.         $params = array(
  129.             ':id' => array(
  130.                 'param' => $category_id,
  131.                 'type' => 1
  132.             )
  133.         );
  134.         return $this->_query->_query($sql, $params)->getResult();
  135.     }
  136.  
  137.     public function createCategory($parent_id, $category_name) {
  138.         $sql = "
  139.            LOCK TABLE categories WRITE;
  140.            SELECT @parentDepth :=category_depth FROM categories WHERE category_id = :parent;
  141.            SELECT @myLeft := category_left FROM categories WHERE category_id = :parent;
  142.            UPDATE categories SET category_right = category_right + 2 WHERE category_right > @myLeft;
  143.            UPDATE categories SET category_left = category_left + 2 WHERE category_left > @myLeft;
  144.            INSERT INTO categories(category_name, category_left, category_right, category_parent,category_depth)
  145.            VALUES(:name, @myLeft + 1, @myLeft + 2, :parent,@parentDepth+1);
  146.            UNLOCK TABLES;";
  147.         $params = array(
  148.             ':parent' => array(
  149.                 'param' => $parent_id,
  150.                 'type' => 1
  151.             ),
  152.             ':name' => array(
  153.                 'param' => $category_name,
  154.                 'type' => 2
  155.             )
  156.         );
  157.         return $this->_query->query($sql, $params);
  158.     }
  159.  
  160.     public function createRootCategory($category_name) {
  161.         $sql = "
  162.            LOCK TABLE categories WRITE;
  163.            SELECT @myRight := MAX(category_right) FROM categories;
  164.            INSERT INTO categories(category_name, category_left, category_right) VALUES(:name, IFNULL(@myRight + 1,1),IFNULL(@myRight + 2,2));
  165.            UNLOCK TABLES";
  166.         $params = array(
  167.             ':name' => array(
  168.                 'param' => $category_name,
  169.                 'type' => 2
  170.             )
  171.         );
  172.         return $this->_query->query($sql, $params)->getResult();
  173.     }
  174.  
  175.     public function organizeCategories($data) {
  176.         foreach ($data as $value) {
  177.             $sql = "UPDATE categories SET category_parent = :parent, category_depth = :depth, category_left = :left, category_right = :right "
  178.                     . "WHERE category_id = :itemID;";
  179.             $params = array(
  180.                 ':parent' => array(
  181.                     'param' => (int) $value['parent_id'],
  182.                     'type' => 1
  183.                 ),
  184.                 ':itemID' => array(
  185.                     'param' => (int) $value['item_id'],
  186.                     'type' => 1
  187.                 ),
  188.                 ':depth' => array(
  189.                     'param' => ((int) $value['depth']) - 1,
  190.                     'type' => 1
  191.                 ),
  192.                 ':left' => array(
  193.                     'param' => ((int) $value['left']) - 1,
  194.                     'type' => 1
  195.                 ),
  196.                 ':right' => array(
  197.                     'param' => ((int) $value['right']) - 1,
  198.                     'type' => 1
  199.                 )
  200.             );
  201.             $this->_query->query($sql, $params);
  202.         }
  203.     }
  204.  
  205.     public function buildTree(array $elements, $parentId = 0) {
  206.         $branch = array();
  207.  
  208.         foreach ($elements as $element) {
  209.             if ($element['category_parent'] == $parentId) {
  210.                 $children = $this->buildTree($elements, $element['category_id']);
  211.                 if ($children) {
  212.                     $element['child'] = $children;
  213.                 }
  214.                 $branch[] = $element;
  215.             }
  216.         }
  217.         return $branch;
  218.     }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement