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 
<?php
/* --------------------------------------------------------------
   OrderTooltipController.inc.php 2015-10-01
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2015 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


MainFactory::load_class('AdminHttpViewController');

/**
 * Class OrderTooltipController
 * 
 * @extends    AdminHttpViewController
 * @category   System
 * @package    AdminHttpViewControllers
 */
class OrderTooltipController extends AdminHttpViewController
{
    /**
     * @var CI_DB_query_builder
     */
    protected $db;


    public function __construct(HttpContextReaderInterface $httpContextReader,
                                HttpResponseProcessorInterface $httpResponseProcessor,
                                ContentViewInterface $defaultContentView)
    {
        parent::__construct($httpContextReader, $httpResponseProcessor, $defaultContentView);
        
        $gxCoreLoader = MainFactory::create('GXCoreLoader', MainFactory::create('GXCoreLoaderSettings'));
        $this->db     = $gxCoreLoader->getDatabaseQueryBuilder();
    }
    
    
    /**
     * Collect order data and send them as JSON response
     * 
     * @return JsonHttpControllerResponse
     */
    public function actionDefault()
    {
        $order = array();
        $order['products'] = array();
        $order['total_price'] = '';
        
        $orderId = (int)$this->_getQueryParameter('orderId');
        
        $query = 'SELECT 
                        o.currency,
                        op.orders_products_id AS id,    
                        op.products_quantity AS quantity,   
                        op.products_name AS name,
                        op.products_model AS model,
                        op.final_price AS price
                    FROM
                        orders o,
                        orders_products op
                    WHERE
                        o.orders_id = ' . $orderId . ' AND
                        o.orders_id = op.orders_id';
        
        $result = $this->db->query($query);
        foreach($result->result_array() as $row)
        {
            $order['products'][(int)$row['id']] = array(
                'quantity' => (double)$row['quantity'],
                'name' => $row['name'],
                'model' => $row['model'],
                'price' => number_format((double)$row['price'], 2, ',', '.') . ' ' . $row['currency'],
                'attributes' => array()
            );
            
            $propertiesQuery = 'SELECT
                                    properties_name AS name,
                                    values_name AS value
                                FROM orders_products_properties
                                WHERE orders_products_id = ' . $row['id'];
            
            $propertiesResult = $this->db->query($propertiesQuery);
            foreach($propertiesResult->result_array() as $propertiesRow)
            {
                $order['products'][(int)$row['id']]['attributes'][] = $propertiesRow;
            }
            
            $attributesQuery = 'SELECT
                                    products_options AS name,
                                    products_options_values AS value
                                FROM orders_products_attributes
                                WHERE 
                                    orders_products_id = ' . $row['id'] . ' AND
                                    products_options != ""';

            $attributesResult = $this->db->query($attributesQuery);
            foreach($attributesResult->result_array() as $attributesRow)
            {
                $order['products'][(int)$row['id']]['attributes'][] = $attributesRow;
            }

            $gPrintContentManager = new GMGPrintContentManager();
            $gPrintResult = $gPrintContentManager->get_orders_products_content($row['id'], true);

            foreach($gPrintResult as $gPrintRow)
            {
                $order['products'][(int)$row['id']]['attributes'][] = array(
                    'name' => $gPrintRow['NAME'],
                    'value' => $gPrintRow['VALUE']
                );
            }
        }

        $query = 'SELECT title, text FROM orders_total WHERE orders_id = ' . $orderId . ' AND class = "ot_total"';
        $row = $this->db->query($query)->row_array();
        if(isset($row))
        {
            $order['total_price'] = trim(strip_tags($row['title'] . ' ' . $row['text']));
        }
        
        return MainFactory::create('JsonHttpControllerResponse', $order);
    }
}