/** * Import items from uploaded file. */ private function import_from_file() { global $db, $site_path; $gallery = gallery::getInstance(); $gallery_manager = GalleryManager::getInstance(); $languages = Language::getLanguages(false); $item_manager = ShopItemManager::getInstance(); $category_manager = ShopCategoryManager::getInstance(); $property_manager = \Modules\Shop\Property\Manager::getInstance(); $membership_manager = \ShopItemMembershipManager::getInstance(); // load categories $categories = array(); $raw_categories = $category_manager->getItems($category_manager->getFieldNames(), array()); if (count($raw_categories) > 0) { foreach ($raw_categories as $category) { $categories[$category->id] = $category->title[self::DEFAULT_LANGUAGE]; } } // load existing items $existing_items = array(); $items = $item_manager->getItems(array('id', 'uid'), array()); foreach ($items as $item) { $existing_items[$item->uid] = $item->id; } // load existing images $existing_images = array(); $images = $gallery_manager->getItems(array('group', 'text_id', 'id'), array()); if (count($images) > 0) { foreach ($images as $image) { // make sure we have storage array if (!array_key_exists($image->group, $existing_images)) { $existing_images[$image->group] = array(); } // add image to the list $existing_images[$image->group][$image->id] = $image->text_id; } } $image_list = scandir(_BASEPATH . '/' . $site_path . 'import/'); // load csv file $csv_data = $this->load_csv_file($_FILES['import']['tmp_name']); array_shift($csv_data); // remove header $number_to_import = isset($_REQUEST['number_to_import']) && !empty($_REQUEST['number_to_import']) ? fix_id($_REQUEST['number_to_import']) : count($csv_data); $counter = 0; foreach ($csv_data as $row) { // make sure we are within our limits if (++$counter > $number_to_import) { break; } // get item name and description $item_name = array_fill(0, count($languages), ''); $item_name = array_combine($languages, $item_name); $item_description = $item_name; $item_name['he'] = $db->escape_string($row[self::COL_NAME_HE]); $item_name['ru'] = $db->escape_string($row[self::COL_NAME_RU]); $item_description['he'] = $db->escape_string($row[self::COL_DESCRIPTION_HE]); $item_description['ru'] = $db->escape_string($row[self::COL_DESCRIPTION_RU]); // unpack price values $prices = explode(',', $row[self::COL_PRICE]); $price_names = explode(',', $row[self::COL_SIZE_LABELS]); // generate uid and check if item exists in database $uid = hash('sha256', 'item_' . $row[self::COL_ID]); if (array_key_exists($uid, $existing_items)) { $data = array('name' => $item_name, 'description' => $item_description, 'price' => count($prices) > 0 ? floatval($prices[0]) : 0); $item_id = $existing_items[$uid]; $item_manager->updateData($data, array('id' => $item_id)); $gallery_id = $item_manager->getItemValue('gallery', array('id' => $item_id)); } else { // prepare data $data = array('name' => $item_name, 'description' => $item_description, 'price' => count($prices) > 0 ? floatval($prices[0]) : 0, 'colors' => '', 'tax' => 0, 'weight' => 0, 'manufacturer' => 0, 'uid' => $uid); // store author of the uploaded item $data['author'] = $_SESSION['uid']; // create item gallery $gallery_id = $gallery->createGallery($item_name); $data['gallery'] = $gallery_id; // add item to the database $item_manager->insertData($data); $item_id = $item_manager->getInsertedID(); } // remove existing prices $property_manager->deleteData(array('item' => $item_id, 'text_id' => array('operator' => 'LIKE', 'value' => 'price_%'))); // create price properties if (count($prices) > 1) { // generate default name $price_name = array_fill(0, count($languages), ''); $price_name = array_combine($languages, $price_name); for ($i = 1; $i < count($prices); $i++) { // set and reset specified name if (isset($price_names[$i])) { $price_name[self::DEFAULT_LANGUAGE] = $price_names[$i]; } else { $price_name[self::DEFAULT_LANGUAGE] = ''; } // prepare data for insertion $price_data = array('item' => $item_id, 'name' => $price_name, 'text_id' => 'price_' . $this->size_names[$i - 1], 'type' => 'decimal', 'value' => serialize(floatval($prices[$i]))); // insert new price property $property_manager->insertData($price_data); } } // remove existing category membership $membership_manager->deleteData(array('item' => $item_id)); // assign category membership for ($i = self::COL_FIRST_CATEGORY; $i < count($row); $i++) { $category_name = $row[$i]; $category_id = $this->get_category_for_name($categories, $category_name, self::DEFAULT_THRESHOLD); if (!is_null($category_id)) { $membership_manager->insertData(array('category' => $category_id, 'item' => $item_id)); } } // upload images $image_file = mb_strtolower($row[self::COL_IMAGE]); $matched_file = $this->match_image_file($image_list, $image_file, 6); // we require a valid match if (!is_null($matched_file)) { $matched_hash = hash('md5', $matched_file); $source_path = _BASEPATH . '/' . $site_path . 'import/' . $matched_file; $destination_file = hash('md5', $matched_file . strval(time())) . '.' . pathinfo(strtolower($matched_file), PATHINFO_EXTENSION); $destination_path = _BASEPATH . '/' . $site_path . 'gallery/images/' . $destination_file; $image_already_uploaded = array_key_exists($gallery_id, $existing_images) && !in_array($matched_hash, $existing_images[$gallery_id]); $file_size = filesize($source_path); // only upload image if it wasn't uploaded already if (!$image_already_uploaded && copy($source_path, $destination_path)) { $gallery_manager->insertData(array('group' => $gallery_id, 'title' => $item_name, 'text_id' => $matched_hash, 'size' => $file_size, 'filename' => $destination_file, 'visible' => 1, 'slideshow' => 0, 'protected' => 0)); } } } // show result message $template = new TemplateHandler('message.xml', $this->path . 'templates/'); $template->setMappedModule($this->name); $params = array('message' => $this->getLanguageConstant('message_import_complete'), 'button' => $this->getLanguageConstant('close'), 'action' => window_Close('shop_import_items')); $template->restoreXML(); $template->setLocalParams($params); $template->parse(); }
/** * Handle drawing list of manufacturers tag * * @param array $tag_params * @param array $children */ public function tag_ManufacturerList($tag_params, $children) { $manager = ShopManufacturerManager::getInstance(); $conditions = array(); $selected = -1; if (class_exists('gallery')) { $use_images = true; $gallery = gallery::getInstance(); $gallery_manager = GalleryManager::getInstance(); } else { $use_images = false; } if (isset($tag_params['selected'])) { $selected = fix_id($tag_params['selected']); } $items = $manager->getItems($manager->getFieldNames(), $conditions); $template = $this->_parent->loadTemplate($tag_params, 'manufacturer_list_item.xml'); if (count($items) > 0) { foreach ($items as $item) { // get image $image = ''; if ($use_images && !empty($item->logo)) { $image_item = $gallery_manager->getSingleItem($gallery_manager->getFieldNames(), array('id' => $item->logo)); if (is_object($image_item)) { $image = $gallery->getImageURL($image_item); } } // prepare parameters $params = array('id' => $item->id, 'name' => $item->name, 'web_site' => $item->web_site, 'logo' => $image, 'selected' => $selected == $item->id ? 1 : 0, 'item_change' => url_MakeHyperlink($this->_parent->getLanguageConstant('change'), window_Open('shop_manufacturer_change', 360, $this->_parent->getLanguageConstant('title_manufacturer_change'), true, true, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'manufacturers'), array('sub_action', 'change'), array('id', $item->id)))), 'item_delete' => url_MakeHyperlink($this->_parent->getLanguageConstant('delete'), window_Open('shop_manufacturer_delete', 400, $this->_parent->getLanguageConstant('title_manufacturer_delete'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'manufacturers'), array('sub_action', 'delete'), array('id', $item->id))))); // parse template $template->setLocalParams($params); $template->restoreXML(); $template->parse(); } } }
/** * Create JSON object containing links with specified characteristics */ private function json_LinkList() { define('_OMIT_STATS', 1); $groups = array(); $conditions = array(); $limit = isset($tag_params['limit']) ? fix_id($tag_params['limit']) : null; $order_by = isset($tag_params['order_by']) ? explode(',', fix_chars($tag_params['order_by'])) : array('id'); $order_asc = isset($tag_params['order_asc']) && $tag_params['order_asc'] == 'yes' ? true : false; $grouped = isset($_REQUEST['grouped']) && $_REQUEST['grouped'] == 'yes' ? true : false; $manager = LinksManager::getInstance(); $group_manager = LinkGroupsManager::getInstance(); $membership_manager = LinkMembershipManager::getInstance(); if (isset($_REQUEST['group'])) { $group_list = explode(',', fix_chars($_REQUEST['group'])); $list = $group_manager->getItems(array('id'), array('name' => $group_list)); if (count($list) > 0) { foreach ($list as $list_item) { $groups[] = $list_item->id; } } } if (isset($_REQUEST['group_id'])) { $groups = array_merge($groups, fix_id(explode(',', $_REQUEST['group_id']))); } if (isset($_REQUEST['sponsored'])) { $sponsored = $_REQUEST['sponsored'] == 'yes' ? 1 : 0; $conditions['sponsored'] = $sponsored; } // fetch ids for specified groups if (!empty($groups)) { $list = $membership_manager->getItems(array('link'), array('group' => $groups)); $id_list = array(); if (count($list) > 0) { foreach ($list as $list_item) { $id_list[] = $list_item->link; } } else { // in case no members of specified group were found, ensure no items are retrieved $id_list = '-1'; } $conditions['id'] = $id_list; } // save some CPU time by getting this early if (class_exists('gallery')) { $use_images = true; $gallery = gallery::getInstance(); $gallery_manager = GalleryManager::getInstance(); } else { $use_images = false; } $items = $manager->getItems($manager->getFieldNames(), $conditions, $order_by, $order_asc, $limit); $result = array('error' => false, 'error_message' => '', 'items' => array()); if (count($items) > 0) { foreach ($items as $item) { $result['items'][] = array('id' => $item->id, 'text' => $item->text, 'url' => $item->url, 'redirect_url' => url_Make('redirect', $this->name, array('id', $item->id)), 'external' => $item->external, 'sponsored' => $item->sponsored, 'display_limit' => $item->display_limit, 'sponsored_clicks' => $item->sponsored_clicks, 'total_clicks' => $item->total_clicks, 'image' => null); } } else { } print json_encode($result); }
/** * Tag handler for category list * * @param array $tag_params * @param array $children */ public function tag_CategoryList($tag_params, $children) { global $language; $manager = ShopCategoryManager::getInstance(); $conditions = array(); $order_by = array(); $order_asc = true; $item_category_ids = array(); $item_id = isset($tag_params['item_id']) ? fix_id($tag_params['item_id']) : null; // create conditions if (isset($tag_params['parent_id'])) { // set parent from tag parameter $conditions['parent'] = fix_id($tag_params['parent_id']); } else { if (isset($tag_params['parent'])) { // get parent id from specified text id $text_id = fix_chars($tag_params['parent']); $parent = $manager->getSingleItem(array('id'), array('text_id' => $text_id)); if (is_object($parent)) { $conditions['parent'] = $parent->id; } else { $conditions['parent'] = -1; } } else { if (!isset($tag_params['show_all'])) { $conditions['parent'] = 0; } } } if (isset($tag_params['level'])) { $level = fix_id($tag_params['level']); } else { $level = 0; } if (isset($tag_params['exclude'])) { $list = fix_id(explode(',', $tag_params['exclude'])); $conditions['id'] = array('operator' => 'NOT IN', 'value' => $list); } if (!is_null($item_id)) { $membership_manager = ShopItemMembershipManager::getInstance(); $membership_items = $membership_manager->getItems(array('category'), array('item' => $item_id)); if (count($membership_items) > 0) { foreach ($membership_items as $membership) { $item_category_ids[] = $membership->category; } } } // get order list if (isset($tag_params['order_by'])) { $order_by = fix_chars(split(',', $tag_params['order_by'])); } else { $order_by = array('title_' . $language); } if (isset($tag_params['order_ascending'])) { $order_asc = $tag_params['order_asc'] == '1' or $tag_params['order_asc'] == 'yes'; } else { // get items from database $items = $manager->getItems($manager->getFieldNames(), $conditions, $order_by, $order_asc); } // create template handler $template = $this->_parent->loadTemplate($tag_params, 'category_list_item.xml'); $template->registerTagHandler('_children', $this, 'tag_CategoryList'); // initialize index $index = 0; // parse template if (count($items) > 0) { foreach ($items as $item) { $image_url = ''; $thumbnail_url = ''; if (class_exists('gallery')) { $gallery = gallery::getInstance(); $gallery_manager = GalleryManager::getInstance(); $image = $gallery_manager->getSingleItem(array('filename'), array('id' => $item->image)); if (!is_null($image)) { $image_url = $gallery->getImageURL($image); $thumbnail_url = $gallery->getThumbnailURL($image); } } $params = array('id' => $item->id, 'index' => $index++, 'item_id' => $item_id, 'parent' => $item->parent, 'image_id' => $item->image, 'image' => $image_url, 'thumbnail' => $thumbnail_url, 'text_id' => $item->text_id, 'title' => $item->title, 'description' => $item->description, 'level' => $level, 'in_category' => in_array($item->id, $item_category_ids) ? 1 : 0, 'selected' => isset($tag_params['selected']) ? fix_id($tag_params['selected']) : 0, 'item_change' => url_MakeHyperlink($this->_parent->getLanguageConstant('change'), window_Open('shop_category_change', 400, $this->_parent->getLanguageConstant('title_category_change'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'categories'), array('sub_action', 'change'), array('id', $item->id)))), 'item_delete' => url_MakeHyperlink($this->_parent->getLanguageConstant('delete'), window_Open('shop_category_delete', 270, $this->_parent->getLanguageConstant('title_category_delete'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'categories'), array('sub_action', 'delete'), array('id', $item->id)))), 'item_add' => url_MakeHyperlink($this->_parent->getLanguageConstant('add'), window_Open('shop_category_add', 400, $this->_parent->getLanguageConstant('title_category_add'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'categories'), array('sub_action', 'add'), array('parent', $item->id))))); $template->restoreXML(); $template->setLocalParams($params); $template->parse(); } } }
/** * Create gallery from specified uploaded field names * * @param array $name Multi-language name for newly created gallery * @return integer Newly created gallery Id */ public function createGallery($name) { $image_manager = GalleryManager::getInstance(); $gallery_manager = GalleryGroupManager::getInstance(); // create gallery $gallery_manager->insertData(array('name' => $name)); $result = $gallery_manager->getInsertedID(); return $result; }