Advertisement
fabi0

Untitled

May 20th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.58 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) {
  26.         return $this->_query->query("
  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 = '$category'
  29.            ORDER BY node.category_left;")->getResult();
  30.     }
  31.  
  32.     public function getCategoryDepth() {
  33.  
  34.         return $this->_query->query("
  35.            SELECT node.category_id, node.category_name, (COUNT(parent.category_name) - 1) AS category_depth
  36.            FROM categories AS node,categories AS parent
  37.            WHERE node.category_left BETWEEN parent.category_left AND parent.category_right
  38.            GROUP BY node.category_id
  39.            ORDER BY node.category_left;")->getResult();
  40.     }
  41.  
  42.     public function getCategoryWithPosts() {
  43.         return $this->_query->query("SELECT parent.category_id, COUNT(discussions.discussion_content) as category_posts
  44.                              FROM categories AS node ,categories AS parent,discussions
  45.                              WHERE node.category_left BETWEEN parent.category_left
  46.                              AND parent.category_right AND node.category_id = discussions.discussion_category
  47.                              AND discussions.discussion_name IS NOT NULL
  48.                              GROUP BY parent.category_id
  49.                              ORDER BY node.category_left")->getResult();
  50.     }
  51.  
  52.     public function getFullCategoryTreeWithPostsCount() {
  53.         $cat1 = $this->getCategoryDepth();
  54.         $cat2 = $this->getCategoryWithPosts();
  55.         $newArray = array();
  56.         foreach ($cat1 as $k => $v) {
  57.  
  58.             $newArray[$v->category_id]['category_name'] = $v->category_name;
  59.             $newArray[$v->category_id]['category_depth'] = $v->category_depth;
  60.         }
  61.  
  62.         foreach ($cat2 as $k => $vv) {
  63.             $newArray[$vv->category_id]['category_posts'] = $vv->category_posts;
  64.         }
  65.  
  66.         foreach ($newArray as $key => $value) {
  67.             if (!isset($newArray[$key]['category_posts'])) {
  68.                 $newArray[$key]['category_posts'] = 0;
  69.             }
  70.         }
  71.         return $newArray;
  72.     }
  73.  
  74.     public function getAllPost() {
  75.         return $this->_query->query("SELECT count(discussion_id) as all_post FROM discussions WHERE discussion_name IS NOT NULL")->getResult();
  76.     }
  77.  
  78.     public function renameCategory($id, $newName) {
  79.         $sql = "UPDATE categories SET `category_name` = '$newName' WHERE category_id = $id;";
  80.         $params = array(
  81.             ':name' => array(
  82.                 'param' => $newName,
  83.                 'type' => 2
  84.             ),
  85.             ':id' => array(
  86.                 'param' => $id,
  87.                 'type' => 1
  88.             )
  89.         );
  90.         $this->_query->query($sql, $params);
  91.         return $this;
  92.     }
  93.  
  94.     public function deleteCategory($category_id) {
  95.         $params = array(
  96.             ':id' => array(
  97.                 'param' => $category_id,
  98.                 'type' => 1
  99.             )
  100.         );
  101.         $this->_query->_query("
  102.            LOCK TABLE categories WRITE;
  103.            SELECT @myLeft := category_left, @myRight := category_right, @myWidth := category_right - category_left + 1
  104.            FROM categories WHERE category_id = :id;
  105.            DELETE FROM categories WHERE category_left BETWEEN @myLeft AND @myRight;
  106.            UPDATE categories SET category_right = category_right - @myWidth WHERE category_right > @myRight;
  107.            UPDATE categories SET category_left = category_left - @myWidth WHERE category_left > @myRight;
  108.            UNLOCK TABLES;", $params);
  109.         return true;
  110.     }
  111.  
  112.     public function createCategory($parent_id, $category_name) {
  113.         $params = array(
  114.             ':parent' => array(
  115.                 'param' => $parent_id,
  116.                 'type' => 1
  117.             ),
  118.             ':name' => array(
  119.                 'param' => $category_name,
  120.                 'type' => 2
  121.             )
  122.         );
  123.         $sql = "
  124.            LOCK TABLE categories WRITE;
  125.            SELECT @myLeft := category_left FROM categories WHERE category_id = :parent;
  126.            UPDATE categories SET category_right = category_right + 2 WHERE category_right > @myLeft;
  127.            UPDATE categories SET category_left = category_left + 2 WHERE category_left > @myLeft;
  128.            INSERT INTO categories(category_name, category_left, category_right) VALUES(:name, @myLeft + 1, @myLeft + 2);
  129.            UNLOCK TABLES;";
  130.         $this->_query->_query($sql, $params);
  131.         return true;
  132.     }
  133.  
  134.     public function createRootCategory($category_name) {
  135.         $params = array(
  136.             ':name' => array(
  137.                 'param' => $category_name,
  138.                 'type' => 2
  139.             )
  140.         );
  141.         $sql = "
  142.            LOCK TABLE categories WRITE;
  143.            SELECT @myRight := MAX(category_right) FROM categories;
  144.            INSERT INTO categories(category_name, category_left, category_right) VALUES('GAME CONSOLES', @myRight + 1, @myRight + 2);
  145.            UNLOCK TABLES;";
  146.         $this->_query->_query($sql, $params);
  147.         return true;
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement