Exemplo n.º 1
0
    public function init()
    {
        $this->links = array();
        $this->product_types = array();
        $this->module_name = "product";
        $this->module_position = 31;
        $this->version = 2.155;
        // 2.155 - 2015-07-18 - product search
        // 2.154 - 2015-06-28 - started work on product API
        // 2.153 - 2015-02-12 - ui fix and product defaults (tax/bill/type)
        // 2.152 - 2014-01-23 - new quote feature
        // 2.151 - 2013-11-15 - working on new UI
        // 2.15 - 2013-10-02 - bulk product delete and product category import fix
        // 2.149 - 2013-09-08 - faq permission fix
        // 2.148 - 2013-08-07 - css improvement
        // 2.147 - 2013-06-16 - javascript fix
        // 2.146 - 2013-06-07 - further work on product categories
        // 2.145 - 2013-05-28 - further work on product categories
        // 2.144 - 2013-05-28 - started work on product categories
        // 2.143 - 2013-04-27 - css fix for large product list
        // 2.142 - 2013-04-16 - product fix in invoice
        // 2.141 - 2013-04-05 - product support in invoices
        // 2.14 - product import via CSV
        // 2.13 - permission fix
        // 2.12 - product permissions
        // 2.11 - initial release
        hook_add('api_callback_product', 'module_product::api_filter_product');
        if (module_security::is_logged_in() && self::can_i('view', 'Products')) {
            module_config::register_css('product', 'product.css');
            module_config::register_js('product', 'product.js');
            if (isset($_REQUEST['_products_ajax'])) {
                switch ($_REQUEST['_products_ajax']) {
                    case 'products_ajax_search':
                        //                        $sent = headers_sent($file, $line);
                        //                        echo 'here';
                        //                        print_r($sent);
                        //                        print_r($file);
                        //                        print_r($line);
                        if (self::$_product_count === false) {
                            self::$_product_count = count(self::get_products());
                        }
                        $product_name = isset($_REQUEST['product_name']) ? $_REQUEST['product_name'] : '';
                        if (self::$_product_count > 0) {
                            $search = array();
                            if (strlen($product_name) > 2) {
                                $search['general'] = $product_name;
                            }
                            $products = self::get_products($search);
                            if (count($products) > 0) {
                                // sort products by categories.
                                $products_in_categories = array();
                                foreach ($products as $product_id => $product) {
                                    if ($product['product_category_id'] && $product['product_category_name']) {
                                        if (!isset($products_in_categories[$product['product_category_name']])) {
                                            $products_in_categories[$product['product_category_name']] = array();
                                        }
                                        $products_in_categories[$product['product_category_name']][] = $product;
                                        unset($products[$product_id]);
                                    } else {
                                    }
                                }
                                $cat_id = 1;
                                ?>

                                <ul>
                                    <?php 
                                foreach ($products_in_categories as $category_name => $cat_products) {
                                    ?>

                                        <li>
                                            <a href="#" class="product_category_parent"><?php 
                                    echo htmlspecialchars($category_name);
                                    ?>
</a> (<?php 
                                    _e('%s products', count($cat_products));
                                    ?>
)
                                            <ul style="display:none;" id="product_category_<?php 
                                    echo $cat_id++;
                                    ?>
">
                                                <?php 
                                    foreach ($cat_products as $product) {
                                        ?>

                                                    <li>
                                                       <a href="#" onclick="return ucm.product.select_product(<?php 
                                        echo $product['product_id'];
                                        ?>
);"> <?php 
                                        echo htmlspecialchars($product['name']);
                                        ?>
</a>
                                                    </li>
                                                <?php 
                                    }
                                    ?>

                                            </ul>

                                    <?php 
                                }
                                ?>

                                    <?php 
                                foreach ($products as $product) {
                                    ?>

                                    <li>
                                        <a href="#" onclick="return ucm.product.select_product(<?php 
                                    echo $product['product_id'];
                                    ?>
);"><?php 
                                    /*if($product['product_category_name']){
                                          echo htmlspecialchars($product['product_category_name']);
                                          echo ' &raquo; ';
                                      }*/
                                    echo htmlspecialchars($product['name']);
                                    ?>
</a>
                                    </li>
                                    <?php 
                                }
                                ?>

                                </ul>
                                <?php 
                            }
                        } else {
                            if (!strlen($product_name)) {
                                _e('Pleae create Products first by going to Settings > Products');
                            }
                        }
                        exit;
                    case 'products_ajax_get':
                        $product_id = (int) $_REQUEST['product_id'];
                        if ($product_id) {
                            $product = self::get_product($product_id);
                        } else {
                            $product = array();
                        }
                        echo json_encode($product);
                        exit;
                }
            }
        }
    }