Example #1
0
 /**
  * Import rows from CSV file and return the number of inserted rows
  *
  * @param string $file
  * @param string $tableName
  * @return int
  */
 function importFromCSV($file = '', $tableName = '')
 {
     $row = 0;
     if (($handle = fopen($file, 'r')) !== FALSE) {
         $db = JFactory::getDbo();
         $tableName = $db->escape($tableName);
         $table = new FeaturesFactory($tableName, 'id', $db);
         $cols = array_keys($table->getProperties());
         $query = $db->getQuery(true);
         $query->select('*')->from($tableName);
         $db->setQuery($query);
         $ids = $db->loadObjectList('id');
         $query->select('ordering')->from($tableName)->order('ordering DESC');
         $db->setQuery($query);
         $maxOrdering = $db->loadResult();
         if ($maxOrdering == null) {
             $maxOrdering = 1;
         }
         while (($data = fgetcsv($handle, 1000, ';', '"')) !== FALSE) {
             $num = count($data);
             $bind = array();
             for ($c = 0; $c < $num; $c++) {
                 if (isset($cols[$c])) {
                     $bind[$cols[$c]] = $data[$c];
                 }
             }
             try {
                 if (isset($bind['id']) && isset($ids[$bind['id']])) {
                     // Load row to update
                     $table->load((int) $bind['id']);
                 } elseif (isset($bind['ordering'])) {
                     $bind['ordering'] = $maxOrdering;
                     $maxOrdering++;
                 }
                 $table->save($bind, '', 'id');
             } catch (Exception $e) {
                 $this->setError($e->getMessage());
                 continue;
             }
             // To force new insertion
             $table->id = null;
             $row++;
         }
     }
     return $row;
 }
Example #2
0
 protected function getFeatureValue($featureId = 0, $featureTable = '')
 {
     $db = JFactory::getDbo();
     $table = new FeaturesFactory('#__jea_' . $db->escape($featureTable), 'id', $db);
     $table->load((int) $featureId);
     return $table->value;
 }