1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 
<?php

/* --------------------------------------------------------------
   ProductCategoryLinker.inc.php 2016-01-28
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2016 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class ProductCategoryLinker
 *
 * @category   System
 * @package    Product
 * @subpackage Repositories
 */
class ProductCategoryLinker implements ProductCategoryLinkerInterface
{
    /**
     * @var CI_DB_query_builder
     */
    protected $db;

    /**
     * @var string
     */
    protected $tableName = 'products_to_categories';


    /**
     * Initialize the product category linker.
     *
     * @param CI_DB_query_builder $db Database connector.
     */
    public function __construct(CI_DB_query_builder $db)
    {
        $this->db = $db;
    }


    /**
     * Links a product to a category.
     *
     * @param IdType $productId        Product ID.
     * @param IdType $targetCategoryId Target category ID.
     *
     * @return $this|ProductCategoryLinker Same instance for chained method calls.
     */
    public function linkProduct(IdType $productId, IdType $targetCategoryId)
    {
        $this->db->replace($this->tableName,
                           array('products_id' => $productId->asInt(), 'categories_id' => $targetCategoryId->asInt()));
        
        return $this;
    }


    /**
     * Changes a link to a new category.
     *
     * @param IdType $productId         Product ID.
     * @param IdType $currentCategoryId Category ID which the product is linked to.
     * @param IdType $newCategoryId     New category to be linked to.
     *
     * @return $this|ProductCategoryLinker Same instance for chained method calls.
     */
    public function changeProductLink(IdType $productId, IdType $currentCategoryId, IdType $newCategoryId)
    {
        $dataArray  = array('categories_id' => $newCategoryId->asInt());
        $whereArray = array('products_id' => $productId->asInt(), 'categories_id' => $currentCategoryId->asInt());
        $this->db->update($this->tableName, $dataArray, $whereArray);

        return $this;
    }


    /**
     * Removes a link to a category.
     *
     * @param IdType $productId  Product ID.
     * @param IdType $categoryId Category ID which the link should be removed to.
     *
     * @return $this|ProductCategoryLinker Same instance for chained method calls.
     */
    public function deleteProductLink(IdType $productId, IdType $categoryId)
    {
        $whereArray = array('products_id' => $productId->asInt(), 'categories_id' => $categoryId->asInt());
        $this->db->delete($this->tableName, $whereArray);

        return $this;
    }


    /**
     * Removes all links from a product.
     *
     * @param IdType $productId Product ID.
     *
     * @return $this|ProductCategoryLinker Same instance for chained method calls.
     */
    public function deleteProductLinks(IdType $productId)
    {
        $whereArray = array('products_id' => $productId->asInt());
        $this->db->delete($this->tableName, $whereArray);

        return $this;
    }
    
    
    /**
     * Returns the category Ids which are linked with given product id.
     *
     * @param IdType $productId Product ID.
     *
     * @return IdCollection
     */
    public function getProductLinks(IdType $productId)
    {
        $fetchedCategories = array();
        $result            = $this->db->get_where($this->tableName, array('products_id' => $productId->asInt()));
        
        foreach($result->result_array() as $row)
        {
            $fetchedCategories[] = new IdType($row['categories_id']);
        }
        
        $collection = MainFactory::create('IdCollection', $fetchedCategories);
        
        return $collection;
    }
    
    
    /**
     * @param \IdType $productId
     *
     * @return ProductRepositoryInterface|$this Same instance for chained method calls.
     */
    public function removeProductFromStartPage(IdType $productId)
    {
        $this->db->update('products', ['products_startpage' => '0'], ['products_id' => $productId->asInt()]);
        
        return $this;
    }
    
    
    /**
     * @param \IdType $productId
     *
     * @return ProductRepositoryInterface|$this Same instance for chained method calls.
     */
    public function addProductToStartPage(IdType $productId)
    {
        $this->db->update('products', ['products_startpage' => '1'], ['products_id' => $productId->asInt()]);
        
        return $this;
    }
}