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

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

/**
 * Class OrderStatusReader
 *
 * @category   System
 * @package    OrderStatus
 * @subpackage Repositories
 */
class OrderStatusReader implements OrderStatusReaderInterface
{
    /**
     * @var CI_DB_query_builder
     */
    protected $queryBuilder;
    
    /**
     * @var string
     */
    protected $table = 'orders_status';
    
    /**
     * @var string
     */
    protected $id = 'orders_status_id';
    
    
    /**
     * OrderStatusReader constructor.
     *
     * @param \CI_DB_query_builder $queryBuilder Active record instance for data access.
     */
    public function __construct(CI_DB_query_builder $queryBuilder)
    {
        $this->queryBuilder = $queryBuilder;
    }
    
    
    /**
     * Returns the data of the expected order status entity by the given order status id.
     *
     * @param \IntType $orderStatusId Id of expected order status entity
     *
     * @return array Data of order status entity or empty array, if no data was found.
     */
    public function getOrderStatusById(IntType $orderStatusId)
    {
        $resultData = $this->queryBuilder->select()
                                         ->from($this->table)
                                         ->where($this->id, $orderStatusId->asInt())
                                         ->order_by($this->id, 'asc')
                                         ->order_by('language_id', 'asc')
                                         ->get()
                                         ->result_array();
        
        $names = [];
        if(count($resultData) === 0)
        {
            return $resultData;
        }
        foreach($resultData as $data)
        {
            $names[(int)$data['language_id']] = $data['orders_status_name'];
        }
        
        return [
            'id'    => $resultData[0][$this->id],
            'names' => $names,
            'color' => $resultData[0]['color']
        ];
    }
    
    
    /**
     * Returns the data of all order status resources in the storage.
     *
     * @return array Data of all order status entities.
     */
    public function getAllOrderStatus()
    {
        $resultData  = $this->queryBuilder->select()
                                          ->from($this->table)
                                          ->order_by($this->id, 'asc')
                                          ->order_by('language_id', 'asc')
                                          ->get()
                                          ->result_array();
        $data        = [];
        $lastId      = null;
        $orderStatus = null;
        $names       = [];
        
        foreach($resultData as $result)
        {
            if($lastId !== (int)$result[$this->id])
            {
                $names = [];
            }
            $names[(int)$result['language_id']] = $result['orders_status_name'];
            if($lastId === (int)$result[$this->id])
            {
                $data[] = [
                    'id'    => (int)$result[$this->id],
                    'names' => $names,
                    'color' => $result['color']
                ];
            }
            $lastId = (int)$result[$this->id];
        }
        
        return $data;
    }
}