Advertisement
fabi0

Untitled

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