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 
<?php

/* --------------------------------------------------------------
   CategoryRepositoryWriter.php 2016-06-23
   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 CategoryRepositoryWriter
 * 
 * This class provides methods for creating and updating specific category records in the database and is used in the category
 * repository among the classes for reading and deleting category records.
 *
 * @category   System
 * @package    Category
 * @subpackage Repositories
 */
class CategoryRepositoryWriter implements CategoryRepositoryWriterInterface
{
    /**
     * @var CI_DB_query_builder
     */
    protected $db;

    /**
     * @var string
     */
    protected $categoriesTable = 'categories';

    /**
     * @var LanguageProviderInterface $languageProvider
     */
    protected $languageProvider;


    /**
     * CategoryRepositoryWriter constructor.
     *
     * @param CI_DB_query_builder       $db Database connector.
     * @param LanguageProviderInterface $languageProvider
     */
    public function __construct(CI_DB_query_builder $db, LanguageProviderInterface $languageProvider)
    {
        $this->db               = $db;
        $this->languageProvider = $languageProvider;
    }


    /**
     * Inserts a category record into the database accordingly to the provided category object and returns the ID from
     * the saved entity.
     *
     * @param CategoryInterface $category The category to insert.
     *
     * @return int Returns the ID of the new category.
     *             
     * @throws UnexpectedValueException When no language id was found by the given language code.
     * @throws InvalidArgumentException
     */
    public function insert(CategoryInterface $category)
    {
        // Insert the category. 
        $categoryDataArray = $this->_parseCategoryData($category);

        $this->db->insert($this->categoriesTable, $categoryDataArray);

        $categoryId = $this->db->insert_id();

        // Insert the category description. 
        foreach($this->languageProvider->getCodes() as $languageCode)
        {
            $languageId = $this->languageProvider->getIdByCode($languageCode);

            $categoryDescriptionDataArray                  = $this->_parseCategoryDescriptionData($category, $languageCode);
            $categoryDescriptionDataArray['language_id']   = $languageId;
            $categoryDescriptionDataArray['categories_id'] = $categoryId;

            $this->db->insert('categories_description', $categoryDescriptionDataArray);
        }

        return $categoryId;
    }


    /**
     * Updates an existing category record accordingly to the provided category object.
     *
     * @param StoredCategoryInterface $category The category to update.
     *
     * @return CategoryRepositoryWriter Same instance for chained method calls.
     *                                  
     * @throws UnexpectedValueException When no language id was found by the given language code.
     * @throws InvalidArgumentException
     */
    public function update(StoredCategoryInterface $category)
    {
        // Update the category. 
        $categoryDataArray = $this->_parseCategoryData($category);
        $this->db->update($this->categoriesTable,
                          $categoryDataArray,
                          array('categories_id' => $category->getCategoryId()));

        // Update the category descriptions. 
        foreach($this->languageProvider->getCodes() as $languageCode)
        {
            $languageId = $this->languageProvider->getIdByCode($languageCode);

            $categoryDescriptionDataArray = array_merge(array(
                                                           'categories_id' => $category->getCategoryId(),
                                                           'language_id' => $languageId
                                                       ), $this->_parseCategoryDescriptionData($category, $languageCode));
            
            $this->db->replace('categories_description', $categoryDescriptionDataArray);
        }

        return $this;
    }


    protected function _parseCategoryData(CategoryInterface $category)
    {
        $categoryDataArray = array(
            'categories_image'     => $category->getImage(),
            'parent_id'            => $category->getParentId(),
            'categories_status'    => $category->isActive(),
            'sort_order'           => $category->getSortOrder(),
            'date_added'           => $category->getAddedDateTime()->format('Y-m-d H:i:s'),
            'last_modified'        => $category->getLastModifiedDateTime()->format('Y-m-d H:i:s'),
            'categories_icon'      => $category->getIcon()
        );

        return $categoryDataArray;
    }


    protected function _parseCategoryDescriptionData(CategoryInterface $category, LanguageCode $languageCode)
    {
        $categoryDescriptionDataArray = array(
            'categories_name'             => $category->getName($languageCode),
            'categories_heading_title'    => $category->getHeadingTitle($languageCode),
            'categories_description'      => $category->getDescription($languageCode),
            'categories_meta_title'       => $category->getMetaTitle($languageCode),
            'categories_meta_description' => $category->getMetaDescription($languageCode),
            'categories_meta_keywords'    => $category->getMetaKeywords($languageCode),
            'gm_alt_text'                 => $category->getImageAltText($languageCode),
            'gm_url_keywords'             => $category->getUrlKeywords($languageCode),
        );

        return $categoryDescriptionDataArray;
    }
}