Пример #1
0
 /**
  * Import categories via XML
  * @param DOMNodeList Object $categories
  * @return bool
  */
 public function import_categories($categories)
 {
     /* Import individual categories*/
     foreach ($categories->getElementsByTagName('category') as $category) {
         // Increment category counter
         $this->totalcategories++;
         // Category Title
         $cat_title = xml::get_node_text($category, 'title');
         // Category Description
         $cat_description = xml::get_node_text($category, 'description');
         // If either the category title or description is not provided
         if (!$cat_title or !$cat_description) {
             $this->errors[] = Kohana::lang('import.xml.category_error') . $this->totalcategories;
         } else {
             // If this category does not already exist in the database
             if (!isset($this->existing_categories[utf8::strtoupper($cat_title)])) {
                 // Get category attributes
                 $cat_color = xml::get_node_text($category, 'color', FALSE);
                 $cat_visible = $category->getAttribute('visible');
                 $cat_trusted = $category->getAttribute('trusted');
                 /* Get other category elements */
                 // Parent Category
                 $cat_parent = xml::get_node_text($category, 'parent');
                 if ($cat_parent) {
                     $parent_id = isset($this->existing_categories[utf8::strtoupper($cat_parent)]) ? $this->existing_categories[utf8::strtoupper($cat_parent)] : 0;
                 }
                 // Save the Category
                 $new_category = new Category_Model();
                 $new_category->category_title = $cat_title;
                 $new_category->category_description = $cat_description ? $cat_description : NULL;
                 $new_category->parent_id = isset($parent_id) ? $parent_id : 0;
                 $new_category->category_color = $cat_color ? $cat_color : '000000';
                 $new_category->category_visible = (isset($cat_visible) and in_array($cat_visible, $this->allowable)) ? $cat_visible : 1;
                 $new_category->category_trusted = (isset($cat_trusted) and in_array($cat_trusted, $this->allowable)) ? $cat_trusted : 0;
                 $new_category->category_position = count($this->existing_categories);
                 $new_category->save();
                 // Add this new category to array of existing categories
                 $this->existing_categories[utf8::strtoupper($cat_title)] = $new_category->id;
                 // Also add it to the array of categories added during import
                 $this->categories_added[] = $new_category->id;
                 $this->notices[] = Kohana::lang('import.new_category') . html::escape($cat_title);
             }
             /* Category Translations */
             $c_translations = $category->getElementsByTagName('translations');
             // Get the current category's id
             $cat_id = $this->existing_categories[utf8::strtoupper($cat_title)];
             // If category translations exist
             if ($c_translations->length > 0) {
                 $cat_translations = $c_translations->item(0);
                 foreach ($cat_translations->getElementsByTagName('translation') as $translation) {
                     // Get Localization
                     $locale = xml::get_node_text($translation, 'locale', FALSE);
                     // Does the locale attribute exist in the document? And is it empty?
                     if ($locale) {
                         // Check if category translation exists for this localization
                         $existing_translations = ORM::factory('category_lang')->where('category_id', $cat_id)->where('locale', $locale)->find_all();
                         // If Category translation does not exist, save it
                         if (count($existing_translations) == 0) {
                             // Get category title for this localization
                             $trans_title = xml::get_node_text($translation, 'translation_title');
                             // Category Description
                             $trans_description = xml::get_node_text($translation, 'translation_description');
                             // If we're missing the translated category title
                             if (!$trans_title) {
                                 $this->notices[] = Kohana::lang('import.xml.translation_title') . $this->totalcategories . ': ' . utf8::strtoupper($locale);
                             } else {
                                 // Save Category Translations
                                 $cl = new Category_Lang_Model();
                                 $cl->locale = $locale;
                                 $cl->category_id = $cat_id;
                                 $cl->category_title = $trans_title;
                                 $cl->category_description = $trans_description ? $trans_description : NULL;
                                 $cl->save();
                                 // Add this to array of category translations added during import
                                 $this->category_translations_added[] = $cl->id;
                                 $this->notices[] = Kohana::lang('import.xml.translation_added') . '"' . utf8::strtoupper($locale) . '" for ' . $cat_title;
                             }
                         }
                     } else {
                         $this->notices[] = Kohana::lang('import.xml.missing_localization') . $this->totalcategories;
                     }
                 }
             }
         }
     }
     // End individual category import
     // If we have errors, return FALSE, else TRUE
     return count($this->errors) === 0;
 }