Ejemplo n.º 1
0
 /**
  * Method to import data in features tables
  */
 public function import()
 {
     $application = JFactory::getApplication();
     $upload = JeaUpload::getUpload('csv');
     $validExtensions = array('csv', 'CSV', 'txt', 'TXT');
     $xmlPath = JPATH_COMPONENT . '/models/forms/features/';
     $xmlFiles = JFolder::files($xmlPath);
     $model = $this->getModel();
     $tables = array();
     // Retrieve the table names
     foreach ($xmlFiles as $filename) {
         if (preg_match('/^[0-9]{2}-([a-z]*).xml/', $filename, $matches)) {
             $feature = $matches[1];
             if (!isset($tables[$feature])) {
                 $form = simplexml_load_file($xmlPath . DS . $filename);
                 $tables[$feature] = (string) $form['table'];
             }
         }
     }
     foreach ($upload as $file) {
         if ($file->isPosted() && isset($tables[$file->key])) {
             $file->setValidExtensions($validExtensions);
             $fileErrors = $file->getErrors();
             if (!$fileErrors) {
                 $rows = $model->importFromCSV($file->temp_name, $tables[$file->key]);
                 $msg = JText::sprintf('COM_JEA_NUM_LINES_IMPORTED_ON_TABLE', $rows, $tables[$file->key]);
                 $application->enqueueMessage($msg);
                 $errors = $model->getErrors();
                 if ($errors) {
                     foreach ($errors as $error) {
                         $application->enqueueMessage($error, 'warning');
                     }
                 }
             } else {
                 foreach ($fileErrors as $error) {
                     $application->enqueueMessage($error, 'warning');
                 }
             }
         }
     }
     $this->setRedirect('index.php?option=com_jea&view=features');
 }
Ejemplo n.º 2
0
 /**
  * Method to manage new uploaded images and
  * to remove non existing images from the gallery
  * @return true on success otherwise false
  */
 public function processImages()
 {
     $upload = JeaUpload::getUpload('newimages');
     $item = $this->getItem();
     $images = json_decode($item->images);
     if (empty($images)) {
         $images = array();
     }
     $imageNames = array();
     foreach ($images as $row) {
         $imageNames[$row->name] = $row->name;
     }
     $baseUploadDir = JPATH_ROOT . DS . 'images' . DS . 'com_jea' . DS . 'images';
     $validExtensions = array('jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF', 'png', 'PNG');
     if (!JFolder::exists($baseUploadDir)) {
         JFolder::create($baseUploadDir);
     }
     $uploadDir = $baseUploadDir . DS . $item->id;
     if (is_array($upload)) {
         foreach ($upload as $file) {
             if ($file->isPosted()) {
                 $file->setValidExtensions($validExtensions)->nameToSafe();
                 if (!JFolder::exists($uploadDir)) {
                     JFolder::create($uploadDir);
                 }
                 if ($file->moveTo($uploadDir)) {
                     if (!isset($imageNames[$file->name])) {
                         $image = new stdClass();
                         $image->name = $file->name;
                         $image->title = '';
                         $image->description = '';
                         $images[] = $image;
                         // Update the list of image names
                         $imageNames[$image->name] = $image->name;
                     }
                 } else {
                     $errors = $file->getErrors();
                     foreach ($errors as $error) {
                         $this->setError(JText::_($error));
                     }
                     return false;
                 }
             }
         }
         $table = $this->getTable();
         $table->load($item->id);
         $table->images = json_encode($images);
         $table->store();
     }
     if (JFolder::exists($uploadDir)) {
         // Remove image files
         $list = JFolder::files($uploadDir);
         foreach ($list as $filename) {
             if (strpos($filename, 'thumb') === 0) {
                 continue;
             }
             if (!isset($imageNames[$filename])) {
                 $removeList = JFolder::files($uploadDir, $filename . '$', false, true);
                 foreach ($removeList as $removeFile) {
                     JFile::delete($removeFile);
                 }
             }
         }
     }
     return true;
 }