/** * Public function that creates a single instance */ public static function getInstance() { if (!isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
/** * 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 item list * * @param array $tag_params * @param array $chilren */ public function tag_ItemList($tag_params, $children) { global $language; $manager = ShopItemManager::getInstance(); $conditions = array(); $page_switch = null; $order_by = array('id'); $order_asc = true; $limit = null; // create conditions if (isset($tag_params['category'])) { if (is_numeric($tag_params['category'])) { $category_id = fix_id($tag_params['category']); } else { // specified id is actually text_id, get real one $category_manager = ShopCategoryManager::getInstance(); $category = $category_manager->getSingleItem(array('id'), array('text_id' => fix_chars($tag_params['category']))); if (!is_object($category)) { return; } $category_id = $category->id; } $membership_manager = ShopItemMembershipManager::getInstance(); $membership_items = $membership_manager->getItems(array('item'), array('category' => $category_id)); $item_ids = array(); if (count($membership_items) > 0) { foreach ($membership_items as $membership) { $item_ids[] = $membership->item; } } if (count($item_ids) > 0) { $conditions['id'] = $item_ids; } else { $conditions['id'] = -1; } // make sure nothing is returned if category is empty } if (isset($tag_params['related'])) { $relation_manager = ShopRelatedItemsManager::getInstance(); $item_id = fix_id($tag_params['related']); $related_items = $relation_manager->getItems(array('related'), array('item' => $item_id)); $related_item_ids = array(); if (count($related_items) > 0) { foreach ($related_items as $relationship) { $related_item_ids[] = $relationship->related; } } if (count($related_item_ids) > 0) { $conditions['id'] = $related_item_ids; } else { $conditions['id'] = -1; } } if (!(isset($tag_params['show_deleted']) && $tag_params['show_deleted'] == 1)) { // force hiding deleted items $conditions['deleted'] = 0; } if (isset($tag_params['filter']) && !empty($tag_params['filter'])) { // filter items with name matching $conditions['name_' . $language] = array('operator' => 'LIKE', 'value' => '%' . fix_chars($tag_params['filter']) . '%'); } if (isset($tag_params['paginate'])) { $per_page = is_numeric($tag_params['paginate']) ? $tag_params['paginate'] : 10; $param = isset($tag_params['page_param']) ? fix_chars($tag_params['page_param']) : null; $item_count = $manager->getItemValue('COUNT(id)', $conditions); $page_switch = new PageSwitch($param); $page_switch->setCurrentAsBaseURL(); $page_switch->setItemsPerPage($per_page); $page_switch->setTotalItems($item_count); // get filter params $limit = $page_switch->getFilterParams(); } if (isset($tag_params['order_by'])) { $order_by = array(fix_chars($tag_params['order_by'])); } // get items $items = $manager->getItems($manager->getFieldNames(), $conditions, $order_by, $order_asc, $limit); // create template $template = $this->_parent->loadTemplate($tag_params, 'item_list_item.xml'); $template->registerTagHandler('_color_list', $this, 'tag_ColorList'); if (count($items) > 0) { $gallery = null; if (class_exists('gallery')) { $gallery = gallery::getInstance(); } $manufacturer_manager = ShopManufacturerManager::getInstance(); foreach ($items as $item) { if (!is_null($gallery)) { // get manufacturer logo $manufacturer_logo_url = ''; if ($item->manufacturer != 0) { $manufacturer = $manufacturer_manager->getSingleItem($manufacturer_manager->getFieldNames(), array('id' => $item->manufacturer)); if (is_object($manufacturer)) { $manufacturer_logo_url = $gallery->getImageURL($manufacturer->logo); } } // get urls for image and thumbnail $image_url = $gallery->getGroupThumbnailURL($item->gallery, true); $thumbnail_url = $gallery->getGroupThumbnailURL($item->gallery); } else { // default values if gallery is not enabled $image_url = ''; $thumbnail_url = ''; $manufacturer_logo_url = ''; } $rating = 0; $params = array('id' => $item->id, 'uid' => $item->uid, 'name' => $item->name, 'description' => $item->description, 'gallery' => $item->gallery, 'size_definition' => $item->size_definition, 'colors' => $item->colors, 'image' => $image_url, 'thumbnail' => $thumbnail_url, 'manufacturer_logo_url' => $manufacturer_logo_url, 'author' => $item->author, 'views' => $item->views, 'price' => $item->price, 'tax' => $item->tax, 'currency' => $this->_parent->settings['default_currency'], 'weight' => $item->weight, 'votes_up' => $item->votes_up, 'votes_down' => $item->votes_down, 'rating' => $rating, 'priority' => $item->priority, 'timestamp' => $item->timestamp, 'visible' => $item->visible, 'deleted' => $item->deleted, 'item_change' => url_MakeHyperlink($this->_parent->getLanguageConstant('change'), window_Open('shop_item_change', 505, $this->_parent->getLanguageConstant('title_item_change'), true, true, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'items'), array('sub_action', 'change'), array('id', $item->id)))), 'item_delete' => url_MakeHyperlink($this->_parent->getLanguageConstant('delete'), window_Open('shop_item_delete', 400, $this->_parent->getLanguageConstant('title_item_delete'), false, false, url_Make('transfer_control', 'backend_module', array('module', $this->name), array('backend_action', 'items'), array('sub_action', 'delete'), array('id', $item->id))))); $template->restoreXML(); $template->setLocalParams($params); $template->parse(); } } // draw page switch if needed if (!is_null($page_switch)) { $params = array(); $children = array(); // pick up parameters from original array foreach ($tag_params as $key => $value) { if (substr($key, 0, 12) == 'page_switch_') { $params[substr($key, 12)] = $value; } } $page_switch->tag_PageSwitch($params, $children); } }
/** * 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(); } } }
/** * Get search results when asked by search module * * @param array $query * @param integer $threshold * @return array */ public function getSearchResults($query, $threshold) { global $language; $manager = ShopItemManager::getInstance(); $result = array(); $conditions = array('visible' => 1, 'deleted' => 0); $query = mb_strtolower($query); $query_words = mb_split("\\s", $query); // include pre-configured options if (isset($this->search_params['category'])) { $membership_manager = ShopItemMembershipManager::getInstance(); $category = $this->search_params['category']; $item_ids = array(); if (!is_numeric($category)) { $category_manager = ShopCategoryManager::getInstance(); $raw_category = $category_manager->getSingleItem(array('id'), array('text_id' => $category)); if (is_object($raw_category)) { $category = $raw_category->id; } else { $category = -1; } } // get list of item ids $membership_list = $membership_manager->getItems(array('item'), array('category' => $category)); if (count($membership_list) > 0) { foreach ($membership_list as $membership) { $item_ids[] = $membership->item; } $conditions['id'] = $item_ids; } } // get all items and process them $items = $manager->getItems(array('id', 'name'), $conditions); // search through items if (count($items) > 0) { foreach ($items as $item) { $title = mb_strtolower($item->name[$language]); $score = 0; foreach ($query_words as $query_word) { if (is_numeric(mb_strpos($title, $query_word))) { $score += 10; } } // add item to result list if ($score >= $threshold) { $result[] = array('score' => $score, 'title' => $title, 'description' => limit_words($item->description[$language], 200), 'id' => $item->id, 'type' => 'item', 'module' => $this->name); } } } return $result; }