$this->query("\n    CREATE TABLE `promotion` (\n        `promotion_id` int(11) NOT NULL AUTO_INCREMENT,\n        `value_id` int(11) unsigned NOT NULL,\n        `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,\n        `picture` VARCHAR(255) NULL DEFAULT NULL,\n        `description` text COLLATE utf8_unicode_ci NOT NULL,\n        `conditions` varchar(100) COLLATE utf8_unicode_ci NOT NULL,\n        `is_unique` tinyint(1) NOT NULL DEFAULT '0',\n        `end_at` date DEFAULT NULL,\n        `force_validation` tinyint(1) unsigned NOT NULL DEFAULT '0',\n        `is_active` tinyint(1) NOT NULL DEFAULT '1',\n        `condition_type` varchar(9) COLLATE utf8_unicode_ci DEFAULT NULL,\n        `condition_number_of_points` tinyint(2) DEFAULT NULL,\n        `condition_period_number` tinyint(2) DEFAULT NULL,\n        `condition_period_type` varchar(5) COLLATE utf8_unicode_ci DEFAULT NULL,\n        `is_shared` tinyint(1) NOT NULL DEFAULT '0',\n        `owner` tinyint(1) NOT NULL DEFAULT '1',\n        `unlock_by` enum('account', 'qrcode') NOT NULL DEFAULT 'account',\n        `unlock_code` VARCHAR(10) NULL DEFAULT NULL,\n        `created_at` datetime NOT NULL,\n        `updated_at` datetime NOT NULL,\n        PRIMARY KEY (`promotion_id`),\n        KEY `KEY_VALUE_ID` (`value_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n\n    CREATE TABLE `promotion_customer` (\n        `promotion_customer_id` int(11) NOT NULL AUTO_INCREMENT,\n        `promotion_id` int(11) NOT NULL,\n        `pos_id` int(11) DEFAULT NULL,\n        `customer_id` int(11) UNSIGNED NOT NULL,\n        `is_used` tinyint(1) unsigned NOT NULL DEFAULT '0',\n        `number_of_error` tinyint(1) unsigned NOT NULL DEFAULT '0',\n        `created_at` datetime NOT NULL,\n        `last_error` datetime DEFAULT NULL,\n        PRIMARY KEY (`promotion_customer_id`),\n        KEY `KEY_PROMOTION_ID` (`promotion_id`)\n    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n");
$this->query("\n    ALTER TABLE `promotion`\n        ADD FOREIGN KEY `FK_VALUE_ID` (`value_id`) REFERENCES `application_option_value` (`value_id`) ON DELETE CASCADE ON UPDATE CASCADE;\n");
$this->query("\n    ALTER TABLE `promotion_customer`\n        ADD FOREIGN KEY `FK_PROMOTION_ID` (`promotion_id`) REFERENCES `promotion` (`promotion_id`) ON DELETE CASCADE ON UPDATE CASCADE;\n");
$library = new Media_Model_Library();
$library->setName('Discount')->save();
$icon_paths = array('/discount/discount1.png', '/discount/discount2.png', '/discount/discount3.png', '/discount/discount4.png', '/discount/discount5.png', '/loyalty/loyalty6.png');
$icon_id = 0;
foreach ($icon_paths as $key => $icon_path) {
    $datas = array('library_id' => $library->getId(), 'link' => $icon_path, 'can_be_colorized' => 1);
    $image = new Media_Model_Library_Image();
    $image->setData($datas)->save();
    if ($key == 0) {
        $icon_id = $image->getId();
    }
}
$datas = array('library_id' => $library->getId(), 'icon_id' => $icon_id, 'code' => 'discount', 'name' => 'Discount', 'model' => 'Promotion_Model_Promotion', 'desktop_uri' => 'promotion/application/', 'mobile_uri' => "promotion/mobile_list/", 'only_once' => 0, 'is_ajax' => 1, 'position' => 20, 'social_sharing_is_available' => 1);
$option = new Application_Model_Option();
$option->setData($datas)->save();
$layouts = array(array("code" => 1, "option_id" => $option->getId(), "name" => "Layout 1", "preview" => "/customization/layout/promotion/layout-1.png", "position" => 1), array("code" => 2, "option_id" => $option->getId(), "name" => "Layout 2", "preview" => "/customization/layout/promotion/layout-2.png", "position" => 2));
foreach ($layouts as $data) {
    $this->_db->insert("application_option_layout", $data);
}
$media_library = new Media_Model_Library();
$media_library->find("Code Scan", "name");
$last_icon = count($media_library->getIcons()) - 1;
$icons = $media_library->getIcons();
$icon_id = $icons[$last_icon]->getId();
$option = new Application_Model_Option();
$all_options = $option->findAll("", "position DESC");
$option_data = array("code" => "qr_discount", "name" => "QR Coupons", "model" => "Promotion_Model_Promotion", "library_id" => $media_library->getId(), "icon_id" => $icon_id, "desktop_uri" => "promotion/application/", "mobile_uri" => "promotion/mobile_list/", "position" => 25);
$option->setData($option_data)->save();
 public function uploadiconAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             $uploader = new Core_Model_Lib_Uploader();
             $file = $uploader->savecrop($datas);
             if (!empty($file)) {
                 $option_value = new Application_Model_Option_Value();
                 $option_value->find($datas['option_id']);
                 $lib = new Media_Model_Library();
                 $lib->find($option_value->getLibraryId());
                 $library_name = $lib->getName();
                 $formated_library_name = Core_Model_Lib_String::format($library_name, true);
                 $base_lib_path = Media_Model_Library_Image::getBaseImagePathTo($formated_library_name);
                 $files = Core_Model_Directory::getTmpDirectory(true) . '/' . $file;
                 $CanBeColorized = $datas['is_colorized'] == 'true' ? 1 : 0;
                 if (!is_dir($base_lib_path)) {
                     mkdir($base_lib_path, 0777, true);
                 }
                 if (!copy($files, $base_lib_path . '/' . $file)) {
                     throw new exception($this->_('An error occurred while saving your picture. Please try againg later.'));
                 } else {
                     $icon_lib = new Media_Model_Library_Image();
                     $icon_lib->setLibraryId($option_value->getLibraryId())->setLink('/' . $formated_library_name . '/' . $file)->setOptionId($option_value->getOptionId())->setAdminId($this->getSession()->getAdmin()->getId())->setCanBeColorized($CanBeColorized)->setPosition(0)->save();
                     $option_value->setIcon('/' . $formated_library_name . '/' . $file)->setIconId($icon_lib->getImageId())->save();
                     $icon_saved = $this->setIcon($icon_lib->getImageId(), $datas['option_id']);
                     // Charge l'option_value
                     $option_value = new Application_Model_Option_Value();
                     $option_value->find($datas['option_id']);
                     $icon_color = $this->getApplication()->getDesignBlock('tabbar')->getImageColor();
                     $html = array('success' => 1, 'file' => '/' . $formated_library_name . '/' . $file, 'icon_id' => $icon_lib->getImageId(), 'colorizable' => $CanBeColorized, 'icon_url' => Media_Model_Library_Image::getImagePathTo($formated_library_name . '/' . $file), 'colored_icon_url' => $this->getUrl('template/block/colorize', array('id' => $option_value->getIconId(), 'color' => str_replace('#', '', $icon_color))), 'colored_header_icon_url' => $icon_saved['colored_header_icon_url'], 'message' => '', 'message_button' => 1, 'message_loader' => 1);
                 }
             }
         } catch (Exception $e) {
             $html = array('message' => $e->getMessage(), 'message_button' => 1, 'message_loader' => 1);
         }
         $this->getLayout()->setHtml(Zend_Json::encode($html));
     }
 }
 public function savesliderimagesAction()
 {
     if ($datas = $this->getRequest()->getPost()) {
         try {
             $url = "";
             $image_id = null;
             $application = $this->getApplication();
             $relative_path = '/' . $application->getId() . '/slider_images/';
             $folder = Application_Model_Application::getBaseImagePath() . $relative_path;
             $datas['dest_folder'] = $folder;
             $uploader = new Core_Model_Lib_Uploader();
             if ($file = $uploader->savecrop($datas)) {
                 $url = Application_Model_Application::getImagePath() . $relative_path . $file;
                 $library = new Media_Model_Library();
                 $library->find($application->getHomepageSliderLibraryId());
                 if (!$library->getId()) {
                     $library->setName('homepage_slider_' . $application->getId())->save();
                     $application->setHomepageSliderLibraryId($library->getId())->save();
                 }
                 $image = new Media_Model_Library_Image();
                 $image->setLibraryId($library->getId())->setLink($url)->setAppId($application->getId())->save();
                 $image_id = $image->getId();
             }
             $datas = array('success' => 1, 'file' => array("id" => $image_id, "url" => $url));
         } catch (Exception $e) {
             $datas = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->getLayout()->setHtml(Zend_Json::encode($datas));
     }
 }
$catalog_option->find('catalog', 'code');
$library = new Media_Model_Library();
if (!$catalog_option->getId()) {
    $library->setName('Set Meal')->save();
    $icon_paths = array('/catalog/catalog1.png', '/catalog/catalog2.png', '/catalog/catalog3.png', '/catalog/catalog4.png', '/catalog/catalog5.png', '/catalog/catalog6.png', '/catalog/catalog7.png', '/promotion/discount4.png', '/catalog/catalog8.png', '/catalog/catalog9.png', '/catalog/catalog10.png', '/catalog/catalog11.png');
    $icon_id = 0;
    foreach ($icon_paths as $key => $icon_path) {
        $datas = array('library_id' => $library->getId(), 'link' => $icon_path, 'can_be_colorized' => 1);
        $image = new Media_Model_Library_Image();
        $image->setData($datas)->save();
        if ($key == 0) {
            $icon_id = $image->getId();
        }
    }
} else {
    $library->find($catalog_option->getLibraryId());
    $icons = $library->getIcons();
    $icons->next();
    $icon_id = $icons->current()->getId();
}
$datas = array('category_id' => $category->getId(), 'library_id' => $library->getId(), 'icon_id' => $icon_id, 'code' => 'set_meal', 'name' => 'Set Meal', 'model' => 'Catalog_Model_Product', 'desktop_uri' => 'catalog/application_menu/', 'mobile_uri' => 'catalog/mobile_setmeal_list/', "mobile_view_uri" => "catalog/mobile_setmeal_view/", "mobile_view_uri_parameter" => "set_meal_id", 'only_once' => 0, 'is_ajax' => 1, 'position' => 35, 'social_sharing_is_available' => 1);
$option = new Application_Model_Option();
$option->setData($datas)->save();
$set_meal_value_id = $option->getId();
$layout_data = array(array("value_id" => $catalog_value_id, "image_path" => "/customization/layout/catalog"), array("value_id" => $set_meal_value_id, "image_path" => "/customization/layout/set-meal"));
foreach ($layout_data as $data) {
    $layouts = array();
    $option = new Application_Model_Option();
    $option->find($data["value_id"]);
    foreach (array(1, 2, 3) as $layout_code) {
        $layouts[] = array("code" => $layout_code, "option_id" => $option->getId(), "name" => "Layout {$layout_code}", "preview" => "{$data["image_path"]}/layout-{$layout_code}.png", "position" => $layout_code);
 public function getSliderImages()
 {
     try {
         $library = new Media_Model_Library();
         $images = $library->find($this->getApplication()->getHomepageSliderLibraryId())->getImages();
     } catch (Exception $e) {
         $images = array();
     }
     return $images;
 }