Example #1
0
 public function create()
 {
     $this->document->addScript('/javascript/bootstrap-datepicker.js');
     // Redirect if user is already logged
     if ($this->auth->isLogged()) {
         $this->response->redirect($this->url->link('account/account', '', 'SSL'));
     }
     // Validate & save incoming data
     if ('POST' == $this->request->getRequestMethod() && $this->_validateCreate()) {
         // Generate email approval link
         $approval_code = md5(rand() . microtime() . $this->request->post['email']);
         // Create new user
         if ($this->model_account_user->createUser($this->request->post['username'], $this->request->post['email'], $this->request->post['password'], 1, 1, NEW_USER_STATUS, NEW_USER_VERIFIED, QUOTA_FILE_SIZE_BY_DEFAULT, $approval_code)) {
             // Clear any previous login attempts for unregistered accounts.
             $this->model_account_user->deleteLoginAttempts($this->request->post['email']);
             // Try to login
             if ($this->auth->login($this->request->post['email'], $this->request->post['password'], true)) {
                 // Generate identicon
                 $identicon = new Identicon();
                 $image = new Image($identicon->generateImageResource(sha1($this->request->post['username']), USER_IMAGE_ORIGINAL_WIDTH, USER_IMAGE_ORIGINAL_HEIGHT, IMG_FILTER_GRAYSCALE), true);
                 $image->save(DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR . 'thumb.' . ALLOWED_IMAGE_EXTENSION);
                 // Send user email
                 $mail = new Mail();
                 $mail->setTo($this->request->post['email']);
                 $mail->setFrom(MAIL_FROM);
                 $mail->setReplyTo(MAIL_INFO);
                 $mail->setSender(MAIL_SENDER);
                 $mail->setSubject(tt('Welcome to the BitsyBay Store!'));
                 $mail->setText(tt("Welcome and thank you for registering!\n\n") . sprintf(tt("Here is your BitsyBay account information:\n\nUsername: %s\nE-mail: %s\nPassword: %s\n\n"), $this->request->post['username'], $this->request->post['email'], $this->request->post['password']) . sprintf(tt("Please, approve your email at the following URL: \n%s"), $this->url->link('account/account/approve', 'approval_code=' . $approval_code, 'SSL')));
                 $mail->send();
                 // Send admin notice
                 $mail = new Mail();
                 $mail->setTo(MAIL_INFO);
                 $mail->setFrom(MAIL_FROM);
                 $mail->setReplyTo(MAIL_INFO);
                 $mail->setSender(MAIL_SENDER);
                 $mail->setSubject(tt('A new customer join us'));
                 $mail->setText(tt('Yes yes yes'));
                 $mail->send();
                 // Redirect to account page
                 //if (isset($this->request->get['redirect'])) {
                 //    $this->response->redirect(base64_decode($this->request->get['redirect']));
                 //} else {
                 $this->response->redirect($this->url->link('account/account', '', 'SSL'));
                 //}
             }
         }
     }
     // Set view variables
     $this->document->setTitle(tt('Create an Account'));
     $data = array();
     $data['error'] = $this->_error;
     $captcha = new Captcha();
     $this->session->setCaptcha($captcha->getCode());
     $data['captcha'] = $this->url->link('account/account/captcha', '', 'SSL');
     $data['action'] = $this->url->link('account/account/create', isset($this->request->get['redirect']) ? 'redirect=' . $this->request->get['redirect'] : false, 'SSL');
     $data['href_account_account_login'] = $this->url->link('account/account/login', '', 'SSL');
     $data['href_account_account_forgot'] = $this->url->link('account/account/forgot', '', 'SSL');
     $data['href_common_information_terms'] = $this->url->link('common/information/terms');
     $data['href_common_information_licenses'] = $this->url->link('common/information/licenses');
     $data['href_common_information_faq'] = $this->url->link('common/information/faq');
     $data['href_common_contact'] = $this->url->link('common/contact');
     $data['username'] = isset($this->request->post['username']) ? $this->request->post['username'] : false;
     $data['email'] = isset($this->request->post['email']) ? $this->request->post['email'] : false;
     $data['accept'] = isset($this->request->post['accept']) ? $this->request->post['accept'] : false;
     $data['alert_danger'] = $this->load->controller('common/alert/danger');
     $data['alert_success'] = $this->load->controller('common/alert/success');
     $data['footer'] = $this->load->controller('common/footer');
     $data['header'] = $this->load->controller('common/header');
     $data['module_breadcrumbs'] = $this->load->controller('module/breadcrumbs', array(array('name' => tt('Home'), 'href' => $this->url->link('common/home'), 'active' => false), array('name' => tt('Account'), 'href' => $this->url->link('account/account', '', 'SSL'), 'active' => false), array('name' => tt('Create'), 'href' => $this->url->link('account/account/create', '', 'SSL'), 'active' => true)));
     // Renter the template
     $this->response->setOutput($this->load->view('account/account/create.tpl', $data));
 }
Example #2
0
 public function update()
 {
     $product_id = 0;
     // Redirect to product create if product_id is not exists
     if (isset($this->request->get['product_id'])) {
         $product_id = (int) $this->request->get['product_id'];
     } else {
         // Log hack attempt
         $this->security_log->write('Try to get product without product_id param');
         $this->response->redirect($this->url->link('account/product/create'));
     }
     // Redirect to login page if user is not logged
     if (!$this->auth->isLogged()) {
         $this->response->redirect($this->url->link('account/account/login', 'redirect=' . base64_encode($this->url->link('account/product/update', 'product_id=' . $product_id))));
     }
     // Check if user has product
     if (!$this->model_catalog_product->userHasProduct($this->auth->getId(), $product_id)) {
         // Log hack attempt
         $this->security_log->write('Try to get not own\'s product_id #' . $product_id);
         // Redirect to safe page
         $this->response->redirect($this->url->link('account/product'));
     }
     if ('POST' == $this->request->getRequestMethod() && $this->_validateProductForm()) {
         // Start transaction
         $this->db->beginTransaction();
         // Add product
         $this->model_catalog_product->updateProduct($product_id, $this->request->post['category_id'], $this->request->post['currency_id'], $this->request->post['regular_price'], $this->request->post['exclusive_price'], $this->request->post['withdraw_address'], FilterUri::alias($this->request->post['product_description'][DEFAULT_LANGUAGE_ID]['title']), (int) $this->auth->isVerified());
         // Add 301 rule if product has new URI
         $url = new Url($this->db, $this->request, $this->response, URL_BASE);
         $old_url = $this->url->link('catalog/product', 'product_id=' . $product_id);
         $new_url = $url->link('catalog/product', 'product_id=' . $product_id);
         if ($old_url != $new_url) {
             $this->model_common_redirect->createRedirect(301, str_replace(URL_BASE, $old_url), str_replace(URL_BASE, $new_url));
         }
         // Add product description
         $this->model_catalog_product->deleteProductDescriptions($product_id);
         foreach ($this->request->post['product_description'] as $language_id => $product_description) {
             $this->model_catalog_product->createProductDescription($product_id, $language_id, $product_description['title'], $product_description['description']);
         }
         // Add Tags
         $this->model_catalog_product->deleteProductToTagByProductId($product_id);
         foreach ($this->request->post['product_description'] as $language_id => $product_description) {
             if (!empty($product_description['tags'])) {
                 $tags = explode(',', $product_description['tags']);
                 foreach ($tags as $tag) {
                     // Add a new global tag if not exists
                     $tag_id = $this->model_catalog_tag->createTag(mb_strtolower(trim($tag)), $language_id);
                     // Add product to tag relation
                     $this->model_catalog_product->addProductToTag($product_id, $tag_id);
                 }
             }
         }
         // Add file
         $directory = DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR;
         if ($file_content = file_get_contents($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION)) {
             $this->model_catalog_product->deleteProductFiles($product_id);
             $product_file_id = $this->model_catalog_product->createProductFile($product_id, md5($file_content), sha1($file_content));
             rename($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION, $directory . $product_file_id . '.' . STORAGE_FILE_EXTENSION);
         }
         // Add demos
         $this->model_catalog_product->deleteProductDemos($product_id);
         if (isset($this->request->post['demo'])) {
             foreach ($this->request->post['demo'] as $row => $demo) {
                 $product_demo_id = $this->model_catalog_product->createProductDemo($product_id, $demo['sort_order'], $demo['url'], $this->request->post['main_demo'] == $row ? 1 : 0);
                 foreach ($demo['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductDemoDescription($product_demo_id, $language_id, $title);
                 }
             }
         }
         // Update images
         $this->model_catalog_product->deleteProductImages($product_id);
         if (isset($this->request->post['image'])) {
             foreach ($this->request->post['image'] as $row => $image) {
                 $product_image_id = $this->model_catalog_product->createProductImage($product_id, $image['sort_order'], $this->request->post['main_image'] == $row ? 1 : 0, isset($image['watermark']) ? 1 : 0);
                 // Generate image titles
                 foreach ($image['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, $title);
                 }
                 // Rename temporary file
                 $directory = DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR;
                 rename($directory . $image['product_image_id'] . '.' . STORAGE_IMAGE_EXTENSION, $directory . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION);
             }
             // Generate unique image if others images is not exists
         } else {
             $product_image_id = $this->model_catalog_product->createProductImage($product_id, 1, 1, 0, 1);
             // Generate image titles from product title
             foreach ($this->request->post['product_description'] as $language_id => $product_description) {
                 $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, $product_description['title']);
             }
             $identicon = new Identicon();
             $image = new Image($identicon->generateImageResource(sha1($product_id), PRODUCT_IMAGE_ORIGINAL_WIDTH, PRODUCT_IMAGE_ORIGINAL_HEIGHT), true);
             $image->save(DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION);
         }
         // Add videos
         $this->model_catalog_product->deleteProductVideos($product_id);
         if (isset($this->request->post['video'])) {
             foreach ($this->request->post['video'] as $video) {
                 $product_video_id = $this->model_catalog_product->createProductVideo($product_id, $video['source'], $video['sort_order'], $video['id']);
                 foreach ($video['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductVideoDescription($product_video_id, $language_id, $title);
                 }
             }
         }
         // Add audios
         $this->model_catalog_product->deleteProductAudios($product_id);
         if (isset($this->request->post['audio'])) {
             foreach ($this->request->post['audio'] as $audio) {
                 $product_audio_id = $this->model_catalog_product->createProductAudio($product_id, $audio['source'], $audio['sort_order'], $audio['id']);
                 foreach ($audio['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductAudioDescription($product_audio_id, $language_id, $title);
                 }
             }
         }
         // Add specials
         $this->model_catalog_product->deleteProductSpecials($product_id);
         if (isset($this->request->post['special'])) {
             foreach ($this->request->post['special'] as $special) {
                 $this->model_catalog_product->createProductSpecial($product_id, $special['regular_price'], $special['exclusive_price'], $special['date_start'], $special['date_end'], $special['sort_order']);
             }
         }
         $this->db->commit();
         // Cleaning
         $this->cache->clean($this->auth->getId());
         $this->storage->clean($this->auth->getId());
         // Set success message
         $this->session->setUserMessage(array('success' => tt('Product successfully updated!')));
         // Admin alert if current user is not verified (updated product has been disabled)
         if (!$this->auth->isVerified()) {
             $this->mail->setTo(MAIL_EMAIL_SUPPORT_ADDRESS);
             $this->mail->setSubject(sprintf(tt('Product has been updated - %s'), PROJECT_NAME));
             $this->mail->setText(sprintf(tt('Product ID %s by %s has been updated and waiting for approving!'), $product_id, $this->auth->getUsername()));
             $this->mail->send();
         }
         $this->response->redirect($this->url->link('account/product'));
     }
     $data = $this->_populateForm($this->url->link('account/product/update', 'product_id=' . $product_id));
     $data['footer'] = $this->load->controller('common/footer');
     $data['header'] = $this->load->controller('common/header');
     $data['module_breadcrumbs'] = $this->load->controller('module/breadcrumbs', array(array('name' => tt('Home'), 'href' => $this->url->link('common/home'), 'active' => false), array('name' => tt('Product list'), 'href' => $this->url->link('account/product'), 'active' => false), array('name' => tt('Update product'), 'href' => $this->url->link('account/product/update', 'product_id=' . $product_id), 'active' => true)));
     // Renter the template
     $this->response->setOutput($this->load->view('account/product/product_form.tpl', $data));
 }
Example #3
0
function avatar($user_name, $uid = 0, $size = 120)
{
    global $_G;
    if (!$user_name) {
        return '/assets/global/images/avatar.png';
    }
    if ($uid > 0 && $uid == $_G['uid']) {
        if ($_G['member']['picurl']) {
            return $_G['member']['picurl'];
        }
    }
    $name = $uid;
    if (!$uid) {
        $name = $_G['uid'];
    }
    if (!$uid) {
        $name = cutstr(md5($user_name), 5, '');
    }
    $path = '/assets/' . $_G['setting']['template'] . '/avatars/' . $name . '.png';
    if (file_exists(ROOT_PATH . $path)) {
        return $path;
    }
    $dir = dirname(ROOT_PATH . $path);
    if (!is_dir($dir)) {
        dmkdir($dir);
    }
    $atavar_type = $_G['setting']['avatar_type'];
    if ($atavar_type == 1) {
        if (!class_exists('MDAvtars')) {
            require_once ROOT_PATH . "web/lib/md_avtars/MaterialDesign.Avatars.class.php";
        }
        $Avatar = new MDAvtars($user_name, $size);
        $Avatar->Save(ROOT_PATH . $path, $size);
        $Avatar->Free();
    } elseif ($atavar_type == 2) {
        if (!class_exists('Identicon')) {
            require_once ROOT_PATH . "web/lib/generator_avatar/Identicon.php";
        }
        $identicon = new Identicon();
        $identicon->save($user_name, ROOT_PATH . $path, $size);
    }
    return $path;
}
Example #4
0
 public function create()
 {
     $this->document->addScript('/javascript/bootstrap-datepicker.js');
     // Redirect if user is already logged
     if ($this->auth->isLogged()) {
         $this->response->redirect($this->url->link('account/account'));
     }
     // Validate & save incoming data
     if ('POST' == $this->request->getRequestMethod() && $this->_validateCreate()) {
         // Generate email approval link
         $approval_code = md5(rand() . microtime() . $this->request->post['email']);
         $approved = 0;
         // Create new user
         if ($user_id = $this->model_account_user->createUser($this->request->post['username'], $this->request->post['email'], $this->request->post['password'], 1, 1, NEW_USER_STATUS, NEW_USER_VERIFIED, QUOTA_FILE_SIZE_BY_DEFAULT, $approval_code, $approved, isset($this->request->cookie['referrer']) && $this->model_account_user->getUser((int) $this->request->cookie['referrer']) ? (int) $this->request->cookie['referrer'] : false)) {
             // Clear previous login attempts for unregistered accounts.
             $this->model_account_user->deleteLoginAttempts($this->request->post['email']);
             // Try to login
             if ($this->auth->login($this->request->post['email'], $this->request->post['password'], true)) {
                 // Generate identicon
                 $identicon = new Identicon();
                 $image = new Image($identicon->generateImageResource(sha1($this->request->post['username']), USER_IMAGE_ORIGINAL_WIDTH, USER_IMAGE_ORIGINAL_HEIGHT, IMG_FILTER_GRAYSCALE), true);
                 $image->save(DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR . 'thumb.' . STORAGE_IMAGE_EXTENSION);
                 // Subscription
                 $subscriptions = $this->model_account_subscription->getSubscriptions($this->language->getId());
                 foreach ($subscriptions as $subscription) {
                     $this->model_account_subscription->addUserSubscription($this->auth->getId(), $subscription->subscription_id);
                 }
                 // Add welcome notification
                 $this->model_account_notification->addNotification($user_id, DEFAULT_LANGUAGE_ID, 'common', sprintf(tt('Welcome to %s!'), PROJECT_NAME), tt("We're so happy you've joined us.\n") . tt("Make every day awesome with inspired finds!"));
                 // Send greetings email with verification code
                 $mail_data['project_name'] = PROJECT_NAME;
                 $mail_data['subject'] = sprintf(tt('Welcome to %s!'), PROJECT_NAME);
                 $mail_data['message'] = tt('Welcome and thank you for registering!');
                 $mail_data['href_home'] = $this->url->link('common/home');
                 $mail_data['href_contact'] = $this->url->link('common/contact');
                 $mail_data['href_subscription'] = $this->url->link('account/account/subscription');
                 $mail_data['href_approve'] = $this->url->link('account/account/approve', 'code=' . $approval_code);
                 $mail_data['href_facebook'] = URL_FACEBOOK;
                 $mail_data['href_twitter'] = URL_TWITTER;
                 $mail_data['href_tumblr'] = URL_TUMBLR;
                 $mail_data['href_github'] = URL_GITHUB;
                 $mail_data['module'] = $this->load->view('email/module/approve.tpl', $mail_data);
                 $this->mail->setTo($this->request->post['email']);
                 $this->mail->setSubject($mail_data['subject']);
                 $this->mail->setHtml($this->load->view('email/common.tpl', $mail_data));
                 $this->mail->send();
                 $this->response->redirect($this->url->link('account/account'));
             }
         }
     }
     // Set view variables
     $this->document->setTitle(tt('Create an Account'));
     $data = array();
     $data['error'] = $this->_error;
     $captcha = new Captcha();
     $this->session->setCaptcha($captcha->getCode());
     $data['captcha'] = $this->url->link('account/account/captcha');
     $data['action'] = $this->url->link('account/account/create', isset($this->request->get['redirect']) ? 'redirect=' . $this->request->get['redirect'] : false);
     $data['href_account_account_login'] = $this->url->link('account/account/login');
     $data['href_account_account_forgot'] = $this->url->link('account/account/forgot');
     $data['href_common_information_terms'] = $this->url->link('common/information/terms');
     $data['href_common_information_licenses'] = $this->url->link('common/information/licenses');
     $data['href_common_information_faq'] = $this->url->link('common/information/faq');
     $data['href_common_contact'] = $this->url->link('common/contact');
     $data['username'] = isset($this->request->post['username']) ? $this->request->post['username'] : false;
     $data['email'] = isset($this->request->post['email']) ? $this->request->post['email'] : false;
     $data['accept'] = isset($this->request->post['accept']) ? $this->request->post['accept'] : false;
     $data['alert_danger'] = $this->load->controller('common/alert/danger');
     $data['alert_success'] = $this->load->controller('common/alert/success');
     $data['footer'] = $this->load->controller('common/footer');
     $data['header'] = $this->load->controller('common/header');
     $data['module_breadcrumbs'] = $this->load->controller('module/breadcrumbs', array(array('name' => tt('Home'), 'href' => $this->url->link('common/home'), 'active' => false), array('name' => tt('Account'), 'href' => $this->url->link('account/account'), 'active' => false), array('name' => tt('Create'), 'href' => $this->url->link('account/account/create'), 'active' => true)));
     // Renter the template
     $this->response->setOutput($this->load->view('account/account/create.tpl', $data));
 }
Example #5
0
 public function update()
 {
     $product_id = 0;
     // Redirect to product create if product_id is not exists
     if (isset($this->request->get['product_id'])) {
         $product_id = (int) $this->request->get['product_id'];
     } else {
         // Log hack attempt
         $this->security_log->write('Try to get product without product_id param');
         $this->response->redirect($this->url->link('account/product/create'));
     }
     // Redirect to login page if user is not logged
     if (!$this->auth->isLogged()) {
         $this->response->redirect($this->url->link('account/account/login', 'redirect=' . urlencode($this->url->link('account/product/update', 'product_id=' . $product_id))));
     }
     // Check if user has product
     if (!$this->model_catalog_product->userHasProduct($this->auth->getId(), $product_id)) {
         // Log hack attempt
         $this->security_log->write('Try to get not own\'s product_id #' . $product_id);
         // Redirect to safe page
         $this->response->redirect($this->url->link('account/product'));
     }
     if ('POST' == $this->request->getRequestMethod() && $this->_validateProductForm()) {
         // Load dependencies
         $translate = new Translate();
         $color = new Color();
         // Create languages registry
         $languages = array();
         foreach ($this->model_common_language->getLanguages() as $language) {
             $languages[$language->language_id] = $language->code;
         }
         // Set active directory
         $directory = DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR;
         // Start transaction
         $this->db->beginTransaction();
         // Add product
         $this->model_catalog_product->updateProduct($product_id, $this->request->post['category_id'], $this->request->post['currency_id'], $this->request->post['regular_price'], $this->request->post['exclusive_price'], $this->request->post['withdraw_address'], FilterUri::alias($this->request->post['product_description'][$this->language->getId()]['title']), (int) $this->auth->isVerified());
         // Add 301 rule if product has new URI
         $url = new Url($this->db, $this->request, $this->response, $this->url->link('common/home'));
         $old_url = $this->url->link('catalog/product', 'product_id=' . $product_id);
         $new_url = $url->link('catalog/product', 'product_id=' . $product_id);
         if ($old_url != $new_url) {
             $this->model_common_redirect->createRedirect(301, str_replace($this->url->link('common/home'), false, $old_url), str_replace($this->url->link('common/home'), false, $new_url));
         }
         // Add product description
         $this->model_catalog_product->deleteProductDescriptions($product_id);
         foreach ($this->request->post['product_description'] as $language_id => $product_description) {
             $this->model_catalog_product->createProductDescription($product_id, $language_id, empty(trim($product_description['title'])) ? $translate->string($this->request->post['product_description'][$this->language->getId()]['title'], $this->language->getCode(), $languages[$language_id]) : $product_description['title'], empty(trim($product_description['description'])) ? $translate->string($this->request->post['product_description'][$this->language->getId()]['description'], $this->language->getCode(), $languages[$language_id]) : $product_description['description']);
         }
         // Add Tags
         $this->model_catalog_product->deleteProductToTagByProductId($product_id);
         // Prepare tags from request
         foreach ($this->request->post['product_description'] as $language_id => $product_description) {
             // Process current language not empty field only
             if (!empty($product_description['tags']) && $language_id == $this->language->getId()) {
                 // Separate a tags string and create multilingual registry
                 foreach (explode(',', $product_description['tags']) as $name) {
                     // Get tag id
                     $name = mb_strtolower(trim($name));
                     // Saved tags registry
                     if ($tag = $this->model_catalog_tag->getTagByName($name)) {
                         $tag_id = $tag->tag_id;
                     } else {
                         // Create new tag
                         $tag_id = $this->model_catalog_tag->addTag();
                         // Create descriptions for each language
                         foreach ($languages as $language_id => $code) {
                             $this->model_catalog_tag->addTagDescription($tag_id, $language_id, $translate->string($name, $this->language->getCode(), $code));
                         }
                     }
                     // Save new relations
                     $this->model_catalog_product->addProductToTag($product_id, $tag_id);
                 }
             }
         }
         // Add file
         if ($file_content = file_get_contents($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION)) {
             $this->model_catalog_product->deleteProductFiles($product_id);
             $product_file_id = $this->model_catalog_product->createProductFile($product_id, md5($file_content), sha1($file_content));
             rename($directory . $this->request->post['product_file_id'] . '.' . STORAGE_FILE_EXTENSION, $directory . $product_file_id . '.' . STORAGE_FILE_EXTENSION);
         }
         // Add demos
         $this->model_catalog_product->deleteProductDemos($product_id);
         if (isset($this->request->post['demo'])) {
             foreach ($this->request->post['demo'] as $row => $demo) {
                 $product_demo_id = $this->model_catalog_product->createProductDemo($product_id, $demo['sort_order'], $demo['url'], $this->request->post['main_demo'] == $row ? 1 : 0);
                 foreach ($demo['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductDemoDescription($product_demo_id, $language_id, empty(trim($title)) ? $translate->string($demo['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title);
                 }
             }
         }
         // Update images
         $this->model_catalog_product->deleteProductImages($product_id);
         if (isset($this->request->post['image'])) {
             foreach ($this->request->post['image'] as $row => $image) {
                 // Add new images
                 $product_image_id = $this->model_catalog_product->createProductImage($product_id, $image['sort_order'], $this->request->post['main_image'] == $row ? 1 : 0, isset($image['watermark']) ? 1 : 0);
                 // Generate image titles
                 foreach ($image['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, empty(trim($title)) ? $translate->string($image['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title);
                 }
                 // Extract image colors
                 if ($color->setImage($directory . $image['product_image_id'] . '.' . STORAGE_IMAGE_EXTENSION) && ($colors = $color->getColors())) {
                     foreach ($colors as $key => $value) {
                         $this->model_catalog_product->createProductImageColor($product_image_id, $value['hex'], $value['hue'], $value['saturation'], $value['value'], $value['red'], $value['green'], $value['blue'], $value['frequency']);
                     }
                 }
                 rename($directory . $image['product_image_id'] . '.' . STORAGE_IMAGE_EXTENSION, $directory . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION);
             }
             // Generate unique image if others images is not exists
         } else {
             $product_image_id = $this->model_catalog_product->createProductImage($product_id, 1, 1, 0, 1);
             // Generate image titles from product title
             foreach ($this->request->post['product_description'] as $language_id => $product_description) {
                 $this->model_catalog_product->createProductImageDescription($product_image_id, $language_id, empty(trim($product_description['title'])) ? $translate->string($this->request->post['product_description'][$this->language->getId()]['title'], $this->language->getCode(), $languages[$language_id]) : $product_description['title']);
             }
             $identicon = new Identicon();
             $image = new Image($identicon->generateImageResource(sha1($product_id), PRODUCT_IMAGE_ORIGINAL_WIDTH, PRODUCT_IMAGE_ORIGINAL_HEIGHT), true);
             $image->save(DIR_STORAGE . $this->auth->getId() . DIR_SEPARATOR . $product_image_id . '.' . STORAGE_IMAGE_EXTENSION);
         }
         // Add videos
         $this->model_catalog_product->deleteProductVideos($product_id);
         if (isset($this->request->post['video'])) {
             foreach ($this->request->post['video'] as $video) {
                 $product_video_id = $this->model_catalog_product->createProductVideo($product_id, isset($video['reduce']) ? 1 : 0, $video['sort_order']);
                 foreach ($video['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductVideoDescription($product_video_id, $language_id, empty(trim($title)) ? $translate->string($video['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title);
                 }
                 rename($directory . $video['product_video_id'] . '.' . STORAGE_VIDEO_EXTENSION, $directory . $product_video_id . '.' . STORAGE_VIDEO_EXTENSION);
             }
         }
         // Add audios
         $this->model_catalog_product->deleteProductAudios($product_id);
         // Add audios
         if (isset($this->request->post['audio'])) {
             foreach ($this->request->post['audio'] as $audio) {
                 $product_audio_id = $this->model_catalog_product->createProductAudio($product_id, isset($audio['cut']) ? 1 : 0, $audio['sort_order']);
                 foreach ($audio['title'] as $language_id => $title) {
                     $this->model_catalog_product->createProductAudioDescription($product_audio_id, $language_id, empty(trim($title)) ? $translate->string($audio['title'][$this->language->getId()], $this->language->getCode(), $languages[$language_id]) : $title);
                 }
                 rename($directory . $audio['product_audio_id'] . '.' . STORAGE_AUDIO_EXTENSION, $directory . $product_audio_id . '.' . STORAGE_AUDIO_EXTENSION);
             }
         }
         // Add specials
         $this->model_catalog_product->deleteProductSpecials($product_id);
         if (isset($this->request->post['special'])) {
             foreach ($this->request->post['special'] as $special) {
                 $this->model_catalog_product->createProductSpecial($product_id, $special['regular_price'], $special['exclusive_price'], $special['date_start'], $special['date_end'], $special['sort_order']);
             }
         }
         // Add license conditions
         $this->model_catalog_product->deleteLicenseConditions($product_id);
         if (isset($this->request->post['license_conditions'])) {
             foreach ($this->request->post['license_conditions'] as $license_condition_id => $value) {
                 $this->model_catalog_product->addLicenseConditionValue($product_id, $license_condition_id);
             }
         }
         $this->db->commit();
         // Cleaning
         $this->cache->clean($this->auth->getId());
         $this->storage->clean($this->auth->getId());
         // Set success message
         $this->session->setUserMessage(array('success' => tt('Product successfully updated!')));
         // Admin alert if current user is not verified (updated product has been disabled)
         if (!$this->auth->isVerified()) {
             $this->mail->setSender($this->auth->getEmail());
             $this->mail->setFrom($this->auth->getEmail());
             $this->mail->setTo(MAIL_EMAIL_SUPPORT_ADDRESS);
             $this->mail->setSubject(sprintf(tt('Product has been updated - %s'), PROJECT_NAME));
             $this->mail->setText(sprintf(tt('Product ID %s by %s has been updated and waiting for approving!'), $product_id, $this->auth->getUsername()));
             $this->mail->send();
         }
         $this->response->redirect($this->url->link('account/product'));
     }
     $data = $this->_populateForm($this->url->link('account/product/update', 'product_id=' . $product_id));
     $data['footer'] = $this->load->controller('common/footer');
     $data['header'] = $this->load->controller('common/header');
     $data['module_breadcrumbs'] = $this->load->controller('module/breadcrumbs', array(array('name' => tt('Home'), 'href' => $this->url->link('common/home'), 'active' => false), array('name' => tt('Product list'), 'href' => $this->url->link('account/product'), 'active' => false), array('name' => tt('Update product'), 'href' => $this->url->link('account/product/update', 'product_id=' . $product_id), 'active' => true)));
     // Renter the template
     $this->response->setOutput($this->load->view('account/product/product_form.tpl', $data));
 }