public function __construct(TreeTable $ticket_table_object) { $this->ticket_table = $ticket_table_object->get(); }
public function __construct(RevisionTable $revision_table_obj) { parent::__construct($revision_table_obj); $this->generate(); }
/** * Импорт товаров из файла * @param XMLReader $reader * @param int $import_flag * @return array */ public function importProduction(XMLReader $reader, $import_flag = 1) { $ids = array(); // Получение получе таблицы $data = array(); $fields = $this->base->getTableFields('catalog_items', array('id')); foreach ($fields as $key => $value) { $data[$value['Field']] = $value['Default']; } // Получение свойств $features = array(); $tmp_features = $this->parseXML($reader, 'Свойства'); if (is_array($tmp_features)) { foreach ($tmp_features as $key => $value) { $features[$value['Ид']] = $value['Наименование']; } } // Получение товаров $items = $this->parseXML($reader, 'Товары'); $count_items = 0; $count_valid_items = 0; $inserted_items = 0; $updated_items = 0; if (is_array($items)) { foreach ($items as $key => $item) { $count_items++; $xml_id = isset($item['Ид']) ? $item['Ид'] : ''; $group_name = $item['Наименование']; $article = isset($item['Артикул']) ? $item['Артикул'] : ''; $group_name = preg_replace('/^(.+?)\\s(\\d+)$/', '$1', $group_name); // Получение идентификатора группы $group = $this->getRowByField('catalog_groups', 'name', $group_name); if (!isset($group['id'])) { // TODO: добавление группы // Объекст класса для работы с деревьями $catalog_groups_object = new TreeTable(DOC, $this->base->getLang(), PREF); $groups_data = array(); $groups_fields = $this->base->getTableFields('catalog_groups', array('id', 'date_add', 'date_edit', 'user_edit')); foreach ($groups_fields as $key => $value) { $groups_data[$value['Field']] = $value['Default']; } $groups_data['user_add'] = $_SESSION['user']['id']; $groups_data['name'] = $group_name; $groups_data['alias'] = Text::strtolow(translitIt($group_name)); $group_id = $catalog_groups_object->insertTableRow('catalog_groups', $groups_data, 1); $group = $this->getRowByField('catalog_groups', 'name', $group_name); } if ($article != '' && $xml_id != '') { $count_valid_items++; // Проверяем существует ли данная запись $id = $this->checkCatalogItem($xml_id, $article); if ($id == 0) { $inserted_items++; unset($data['date_add']); unset($data['date_edit']); unset($data['user_edit']); $data['user_add'] = 1; // Добавление записи $data['xml_id'] = $xml_id; $data['group_id'] = $group['id']; $data['name'] = $group_name; $data['article'] = $article; $data['alias'] = Text::strtolow(translitIt($article)); $item_features = array(); $producer = 0; $collection = 0; foreach ($item as $item_key => $item_value) { if (is_array($item_value)) { if (isset($item_value['Значение'])) { // Получение остатков if ($features[$item_value['Ид']] == 'Остаток') { $data['insight'] = $item_value['Значение']; } // Получение Бренда if ($features[$item_value['Ид']] == 'Бренд') { $producers = $this->getRowByField('catalog_producers', 'name', $item_value['Значение']); if (isset($producers['id'])) { $producer = $producers['id']; } else { $producer_data = array(); $producer_fields = $this->base->getTableFields('catalog_producers', array('id', 'date_add', 'date_edit', 'user_edit')); foreach ($producer_fields as $key => $value) { $producer_data[$value['Field']] = $value['Default']; } $producer_data['user_add'] = $_SESSION['user']['id']; $producer_data['name'] = $item_value['Значение']; $producer_data['alias'] = Text::strtolow(translitIt($item_value['Значение'])); $producer = parent::setRow('catalog_producers', $producer_data); } } // Получение Коллекции if ($features[$item_value['Ид']] == 'Коллекция' && $producer > 0) { $collections = $this->getRowByField('catalog_producers_collections', 'name', $item_value['Значение']); if (isset($collections['id'])) { $collection = $collections['id']; } else { $collection_data = array(); $collection_fields = $this->base->getTableFields('catalog_producers_collections', array('id', 'date_add', 'date_edit', 'user_edit')); foreach ($collection_fields as $key => $value) { $collection_data[$value['Field']] = $value['Default']; } $collection_data['user_add'] = $_SESSION['user']['id']; $collection_data['group_id'] = $producer; $collection_data['name'] = $item_value['Значение']; $collection_data['alias'] = Text::strtolow(translitIt($item_value['Значение'])); $collection = parent::setRow('catalog_producers_collections', $collection_data); } } // Получение Размера if ($features[$item_value['Ид']] == 'Вставка' || $features[$item_value['Ид']] == 'Металл' || $features[$item_value['Ид']] == 'Проба' || $features[$item_value['Ид']] == 'Размер' || $features[$item_value['Ид']] == 'Цвет') { $item_features[$features[$item_value['Ид']]] = $item_value['Значение']; } } else { foreach ($item_value as $i_key => $i_value) { if (strstr($i_key, 'ЗначенияСвойства') && isset($i_value['Значение'])) { $item_features[$features[$i_value['Ид']]] = $i_value['Значение']; } if (strstr($i_key, 'ЗначениеРеквизита') && isset($i_value['Значение'])) { $item_features[$i_value['Наименование']] = $i_value['Значение']; } } } } } $data['catalog_producers'] = $producer; $data['catalog_producers_collections'] = $collection; $id = parent::setRow('catalog_items', $data); // Добавление изображений $files_path = DOC . 'userfiles/original/'; $image_id = 0; if (file_exists($files_path . $article . '.JPG') || file_exists($files_path . $article . '.jpg')) { $image_data = array('group_id' => $id, 'name' => file_exists($files_path . $article . '.JPG') ? $article . '.JPG' : $article . '.jpg', 'image' => file_exists($files_path . $article . '.JPG') ? $article . '.JPG' : $article . '.jpg', 'position' => 1); $image_id = parent::setRow('catalog_images', $image_data); } $image_index = 1; while (file_exists($files_path . $article . '-' . $image_index . '.JPG') || file_exists($files_path . $article . '-' . $image_index . '.jpg')) { $image_data = array('group_id' => $id, 'name' => file_exists($files_path . $article . '-' . $image_index . '.JPG') ? $article . '-' . $image_index . '.JPG' : $article . '-' . $image_index . '.jpg', 'image' => file_exists($files_path . $article . '-' . $image_index . '.JPG') ? $article . '-' . $image_index . '.JPG' : $article . '-' . $image_index . '.jpg', 'position' => $image_id > 0 ? 0 : 1); $image_id = parent::setRow('catalog_images', $image_data); $image_index++; } // Определение комплектов $article_match = strlen($article) == 13 ? substr($article, -6) : substr($article, -4); $matches_items = $this->findItem($article_match); if (is_array($matches_items) && !empty($matches_items)) { for ($i = 0; $i < count($matches_items); $i++) { if ($matches_items[$i]['id'] != $id) { $this->setRelations(PREF . 'catalog_set', 'item_id1', 'item_id2', $id, $matches_items[$i]['id']); } } } // Добавление характеристик $feature_metall = '%'; foreach ($item_features as $if_key => $if_value) { if ($if_key == 'Металл' || $if_key == 'Проба' || $if_key == 'Цвет') { $feature_metall .= $if_value . '%'; } else { // Поиск характеристики $feature = $this->getRowByField('catalog_features', 'name', $if_key); if (isset($feature['id']) && $feature['tree_left'] == $feature['tree_right'] - 1) { $feature_data = array('item_id' => $id, 'feature_id' => $feature['id'], 'value' => $feature['type'] == 3 ? $feature['alias'] : $if_value); parent::setRow('catalog_features_values', $feature_data); } } } if ($feature_metall != '%') { $query = 'CALL search_feature(:word)'; $params = array(':word' => $feature_metall); $feature_metall_data = PdoWrap::selectRow($query, $params); if (isset($feature_metall_data['id'])) { $feature_data = array('item_id' => $id, 'feature_id' => $feature_metall_data['id'], 'value' => $feature_metall_data['alias']); parent::setRow('catalog_features_values', $feature_data); } } } else { $updated_items++; unset($data['date_add']); unset($data['date_edit']); unset($data['user_add']); $data['user_edit'] = 1; // Редактирование записи $data['xml_id'] = $xml_id; $data['group_id'] = $group['id']; $data['name'] = $group_name; $data['article'] = $article; $data['alias'] = Text::strtolow(translitIt($article)); $item_features = array(); $producer = 0; $collection = 0; foreach ($item as $item_key => $item_value) { if (is_array($item_value)) { if (isset($item_value['Значение'])) { // Получение остатков if ($features[$item_value['Ид']] == 'Остаток') { $data['insight'] = $item_value['Значение']; } // Получение Бренда if ($features[$item_value['Ид']] == 'Бренд') { $producers = $this->getRowByField('catalog_producers', 'name', $item_value['Значение']); if (isset($producers['id'])) { $producer = $producers['id']; } else { $producer_data = array(); $producer_fields = $this->base->getTableFields('catalog_producers', array('id', 'date_add', 'date_edit', 'user_edit')); foreach ($producer_fields as $key => $value) { $producer_data[$value['Field']] = $value['Default']; } $producer_data['user_add'] = $_SESSION['user']['id']; $producer_data['name'] = $item_value['Значение']; $producer_data['alias'] = Text::strtolow(translitIt($item_value['Значение'])); $producer = parent::setRow('catalog_producers', $producer_data); } } // Получение Коллекции if ($features[$item_value['Ид']] == 'Коллекция' && $producer > 0) { $collections = $this->getRowByField('catalog_producers_collections', 'name', $item_value['Значение']); if (isset($collections['id'])) { $collection = $collections['id']; } else { $collection_data = array(); $collection_fields = $this->base->getTableFields('catalog_producers_collections', array('id', 'date_add', 'date_edit', 'user_edit')); foreach ($collection_fields as $key => $value) { $collection_data[$value['Field']] = $value['Default']; } $collection_data['user_add'] = $_SESSION['user']['id']; $collection_data['group_id'] = $producer; $collection_data['name'] = $item_value['Значение']; $collection_data['alias'] = Text::strtolow(translitIt($item_value['Значение'])); $collection = parent::setRow('catalog_producers_collections', $collection_data); } } // Получение Размера if ($features[$item_value['Ид']] == 'Вставка' || $features[$item_value['Ид']] == 'Металл' || $features[$item_value['Ид']] == 'Проба' || $features[$item_value['Ид']] == 'Размер' || $features[$item_value['Ид']] == 'Цвет') { $item_features[$features[$item_value['Ид']]] = $item_value['Значение']; } } else { foreach ($item_value as $i_key => $i_value) { if (strstr($i_key, 'ЗначенияСвойства') && isset($i_value['Значение'])) { $item_features[$features[$i_value['Ид']]] = $i_value['Значение']; } if (strstr($i_key, 'ЗначениеРеквизита') && isset($i_value['Значение'])) { $item_features[$i_value['Наименование']] = $i_value['Значение']; } } } } } $data['catalog_producers'] = $producer; $data['catalog_producers_collections'] = $collection; parent::editRow('catalog_items', $id, $data); // Получение существующих изображений $current_images_tmp = $this->__getImages($id); $current_images = array(); for ($i = 0; $i < count($current_images_tmp); $i++) { $current_images[] = Text::strtolow($current_images_tmp[$i]['image']); } // Добавление изображений $files_path = DOC . 'userfiles/original/'; $image_id = 0; if (file_exists($files_path . $article . '.JPG') || file_exists($files_path . $article . '.jpg')) { if (!in_array(Text::strtolow($article . '.JPG'), $current_images)) { $image_data = array('group_id' => $id, 'name' => file_exists($files_path . $article . '.JPG') ? $article . '.JPG' : $article . '.jpg', 'image' => file_exists($files_path . $article . '.JPG') ? $article . '.JPG' : $article . '.jpg', 'position' => 1); $image_id = parent::setRow('catalog_images', $image_data); } else { $image_id = 1; } } $image_index = 1; while (file_exists($files_path . $article . '-' . $image_index . '.JPG') || file_exists($files_path . $article . '-' . $image_index . '.jpg')) { if (!in_array(Text::strtolow($article . '-' . $image_index . '.JPG'), $current_images)) { $image_data = array('group_id' => $id, 'name' => file_exists($files_path . $article . '-' . $image_index . '.JPG') ? $article . '-' . $image_index . '.JPG' : $article . '-' . $image_index . '.jpg', 'image' => file_exists($files_path . $article . '-' . $image_index . '.JPG') ? $article . '-' . $image_index . '.JPG' : $article . '-' . $image_index . '.jpg', 'position' => $image_id > 0 ? 0 : 1); $image_id = parent::setRow('catalog_images', $image_data); } $image_index++; } // Определение комплектов $article_match = strlen($article) == 13 ? substr($article, -6) : substr($article, -4); $matches_items = $this->findItem($article_match); if (is_array($matches_items) && !empty($matches_items)) { for ($i = 0; $i < count($matches_items); $i++) { if ($matches_items[$i]['id'] != $id) { $this->setRelations(PREF . 'catalog_set', 'item_id1', 'item_id2', $id, $matches_items[$i]['id']); } } } // Удаление существующих характреистик $query = 'CALL catalog_features_values_delete(:field_item_id)'; $params = array(':field_item_id' => $id); PdoWrap::execute($query, $params); // Добавление характеристик $feature_metall = '%'; foreach ($item_features as $if_key => $if_value) { if ($if_key == 'Металл' || $if_key == 'Проба' || $if_key == 'Цвет') { $feature_metall .= $if_value . '%'; } else { // Поиск характеристики $feature = $this->getRowByField('catalog_features', 'name', $if_key); if (isset($feature['id']) && $feature['tree_left'] == $feature['tree_right'] - 1) { $feature_data = array('item_id' => $id, 'feature_id' => $feature['id'], 'value' => $feature['type'] == 3 ? $feature['alias'] : $if_value); parent::setRow('catalog_features_values', $feature_data); } } } if ($feature_metall != '%') { $query = 'CALL search_feature(:word)'; $params = array(':word' => $feature_metall); $feature_metall_data = PdoWrap::selectRow($query, $params); if (isset($feature_metall_data['id'])) { $feature_data = array('item_id' => $id, 'feature_id' => $feature_metall_data['id'], 'value' => $feature_metall_data['alias']); parent::setRow('catalog_features_values', $feature_data); } } } } } } return array('count_items' => $count_items, 'count_valid_items' => $count_valid_items, 'inserted_items' => $inserted_items, 'updated_items' => $updated_items); }