Example #1
0
 public function import()
 {
     if (!JSession::checkToken('post') && !JSession::checkToken('get')) {
         jexit('COM_DJCATALOG2_INVALID_TOKEN');
     }
     require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers' . DS . 'import.php';
     $app = JFactory::getApplication();
     $model = $this->getModel('item');
     $files = $app->input->files;
     $db = JFactory::getDbo();
     $user = JFactory::getUser();
     $date = JFactory::getDate();
     $file = $files->get('csvfile');
     $enclosure = $app->input->get('enclosure', 0) == 0 ? "\"" : "'";
     $separator = $app->input->get('separator', 0) == 0 ? "," : ";";
     $defaults = array();
     $defaults['cat_id'] = $app->input->get('cat_id', 0);
     $defaults['producer_id'] = $app->input->get('producer_id', 0);
     $defaults['group_id'] = $app->input->get('group_id', 0);
     $defaults['price'] = $app->input->get('price', 0.0);
     $defaults['special_price'] = $app->input->get('special_price', 0.0);
     $defaults['published'] = $app->input->get('published', 0);
     $defaults['created_by'] = $app->input->get('created_by', $user->id);
     $defaults['created'] = $app->input->get('created', $date->toSql());
     if ($defaults['cat_id'] > 0) {
         $db->setQuery('select count(*) from #__djc2_categories where id =' . $defaults['cat_id']);
         $result = (bool) $db->loadResult();
         if ($result == false) {
             $defaults['cat_id'] = 0;
         }
     }
     if ($defaults['producer_id'] > 0) {
         $db->setQuery('select count(*) from #__djc2_producers where id =' . $defaults['producer_id']);
         $result = (bool) $db->loadResult();
         if ($result == false) {
             $defaults['producer_id'] = 0;
         }
     }
     if ($defaults['group_id'] > 0) {
         $db->setQuery('select count(*) from #__djc2_items_extra_fields_groups where id =' . $defaults['group_id']);
         $result = (bool) $db->loadResult();
         if ($result == false) {
             $defaults['group_id'] = 0;
         }
     }
     $messages = array();
     if (!empty($file)) {
         if (!$file['error']) {
             $tempname = $file['tmp_name'];
             if (mb_check_encoding(file_get_contents($file['tmp_name']), 'UTF-8') == false) {
                 $this->setRedirect(JRoute::_('index.php?option=com_djcatalog2&view=import', false), JText::_('COM_DJCATALOG2_ERROR_INVALID_ENCODING'), 'error');
                 return false;
             }
             $rows = Djcatalog2ImportHelper::parseCSV(realpath($tempname), $separator, $enclosure);
             if (!empty($rows)) {
                 $messages = Djcatalog2ImportHelper::storeRecords($rows, $model, 'item', $defaults);
             }
         }
     }
     foreach ($messages as $type => $arr) {
         if (!empty($arr)) {
             foreach ($arr as $message) {
                 $app->enqueueMessage($message, $type);
             }
         }
     }
     $this->setRedirect(JRoute::_('index.php?option=com_djcatalog2&view=import', false));
     return true;
 }
Example #2
0
 public static function getStateByName($name, $country_id = 0)
 {
     if (empty($name)) {
         return false;
     }
     $name = JString::strtolower(JString::trim($name));
     $where = (int) $country_id > 0 ? ' country_id=' . (int) $country_id . ' ' : ' 1 ';
     if (empty(self::$states)) {
         $db = JFactory::getDbo();
         $db->setQuery('select lower(name) as name, id from #__djc2_countries_states WHERE ' . $where . ' order by name asc');
         self::$states = $db->loadObjectList('name');
     }
     if (isset(self::$states[$name])) {
         return self::$states[$name]->id;
     }
     $db->setQuery('select country_name, id from #__djc2_countries where ' . $where . ' AND lower(country_name) like' . $db->quote('%' . $db->escape($name) . '%') . ' LIMIT 1');
     $result = $db->loadObject();
     if (!empty($result)) {
         self::$countries[$name] = $result;
         return self::$countries[$name]->id;
     }
     return false;
 }