<?php
class ModuleQuestion extends Controller
{
    public $error = array();

    public function fetch()
    {
        $request = &$this->locator->get('request');
        $database = &$this->locator->get('database');
        $url = &$this->locator->get('url');
        $customer =& $this->locator->get('customer');
        $template = $this->locator->create('template');

        $product_id = $request->get('product_id');
        $template->set('product_id', $product_id);
        $ajax_questionaction = $url->ssl('product', 'ajax_addquestion', array('product_id' => $product_id));
        $template->set('ajax_questionaction', $ajax_questionaction);

        $question_data = $database->getRows("select q.* from question q left join product p on (q.product_id = p.product_id) where q.product_id = $product_id order by q.question_id desc");
        $template->set('product_questions', $question_data);
        if ($customer -> isLogged()) {
            $template->set('customer_name', $customer -> getFullName() );
            $template->set('customer_email', $customer -> getEmail() );
        } else {
            $template->set('customer_name', '' );
            $template->set('customer_email', '' );
        }

        return $template->fetch('module/question.tpl');
    }

    public function getUploadImages()
    {
        $request = &$this->locator->get('request');
        $language = &$this->locator->get('language');
        $user = &$this->locator->get('user');
        $upload = &$this->locator->get('upload');
        $image_module = &$this->locator->get('image');

        $image_list = array();
        $image_list[0] = 'image';
        $image_list[1] = 'image_1';
        $image_list[2] = 'image_2';
        $image_list[3] = 'image_3';

        $post_images = array();
        $i = 0;
        while ($i < 4) {
            if ($upload->has($image_list[$i])) {
                if ($upload->getName($image_list[$i]) == '') {
                    $i = $i + 1;
                    continue;
                }
                $image_name = '';
                $image_path = '';
                if ($request->get('OriginalFileName', 'post')) {
                    $image_name = $upload->getName($image_list[$i]);
                } else {
                    $image_name = $upload->getSize($image_list[$i]).'-'.time().'-'.$upload->getName($image_list[$i]);
                }
                $image_path = CDN_QUESTION_IMAGE.$image_name;
                if (!$upload->save($image_list[$i], $image_path)) {
                    $this->error['file'] = $language->get('error_upload');
                    $i = $i + 1;
                    continue;
                }
                if ((strlen($upload->getName($image_list[$i])) < 3) || (strlen($upload->getName($image_list[$i])) > 128)) {
                    $this->error['file'] = $language->get('error_filename');
                    $i = $i + 1;
                    continue;
                }
                $allowed = array(
                    'image/jpeg',
                    'image/pjpeg',
                    'image/gif',
                    'image/png',
                    'image/x-png',
                );
                if (!in_array($upload->getType($image_list[$i]), $allowed)) {
                    $this->error['file'] = $language->get('error_filetype');
                    $i = $i + 1;
                    continue;
                }
                if ($upload->hasError($image_list[$i])) {
                    $this->error['message'] = $upload->getError('image');
                    $i = $i + 1;
                    continue;
                }
                if ($request->has('image_title_'.$i, 'post')) {
                    $post_images[] = array('name' => $image_name,
                                         'path' => $image_path,
                                         'id' => $image_path,
                                         'title' => $request->get('image_title_'.$i, 'post'), );
                } else {
                    $post_images[] = array('name' => $image_name,
                                         'path' => $image_path,
                                         'id' => $image_path, );
                }
            }
            $i = $i + 1;
        }

        return $post_images;
    }

}
