public function process($loadReferencedRecords = array())
 {
     set_time_limit(0);
     ignore_user_abort(true);
     $this->deleteCancelFile();
     $filter = $this->grid->getFilter();
     $filter->setLimit(0);
     $ids = array();
     foreach (ActiveRecordModel::getFieldValues($this->grid->getModelClass(), $filter, array('ID'), ActiveRecordModel::LOAD_REFERENCES) as $row) {
         $ids[] = $row['ID'];
     }
     $totalCount = count($ids);
     $progress = 0;
     $response = new JSONResponse(array('act' => $this->request->get('act')), 'success', $this->completionMessage);
     ActiveRecord::beginTransaction();
     $chunkSize = count($ids) / self::MASS_ACTION_CHUNK_SIZE > 5 ? self::MASS_ACTION_CHUNK_SIZE : ceil(count($ids) / 5);
     foreach (array_chunk($ids, $chunkSize) as $chunk) {
         $response->flush('|' . base64_encode(json_encode(array('total' => $totalCount, 'progress' => $progress, 'pid' => $this->pid))));
         $this->processSet(ActiveRecordModel::getRecordSet($this->grid->getModelClass(), new ARSelectFilter(new INCond(new ARFieldHandle($this->grid->getModelClass(), 'ID'), $chunk)), $loadReferencedRecords));
         $progress += count($chunk);
     }
     ActiveRecord::commit();
     $response->flush('|');
     return $response;
 }
 public function import()
 {
     //ignore_user_abort(true);
     set_time_limit(0);
     $validator = $this->buildValidator();
     if (!$validator->isValid()) {
         return new JSONResponse(array('errors' => $validator->getErrorList()));
     }
     $dsn = $this->request->get('dbType') . '://' . $this->request->get('dbUser') . ($this->request->get('dbPass') ? ':' . $this->request->get('dbPass') : '') . '@' . $this->request->get('dbServer') . '/' . $this->request->get('dbName');
     try {
         $cart = $this->request->get('cart');
         ClassLoader::import('library.import.driver.' . $cart);
         $driver = new $cart($dsn, $this->request->get('filePath'));
     } catch (SQLException $e) {
         $validator->triggerError('dbServer', $e->getNativeError());
         $validator->saveState();
         return new JSONResponse(array('errors' => $validator->getErrorList()));
     }
     if (!$driver->isDatabaseValid()) {
         $validator->triggerError('dbName', $this->maketext('_invalid_database', $driver->getName()));
         $validator->saveState();
         return new JSONResponse(array('errors' => $validator->getErrorList()));
     }
     if (!$driver->isPathValid()) {
         $validator->triggerError('filePath', $this->maketext('_invalid_path', $driver->getName()));
         $validator->saveState();
         return new JSONResponse(array('errors' => $validator->getErrorList()));
     }
     $importer = new LiveCartImporter($driver);
     $response = new JSONResponse(null);
     // get importable data types
     $response->flush($this->getResponse(array('types' => $importer->getItemTypes())));
     ActiveRecord::beginTransaction();
     // process import
     try {
         while (true) {
             $result = $importer->process();
             $response->flush($this->getResponse($result));
             //echo '|' . round(memory_get_usage() / (1024*1024), 1) . " ($result[type] : " . array_shift(array_shift(ActiveRecord::getDataBySQL("SELECT COUNT(*) FROM " . $result['type']))) . ")<br> \n";
             if (is_null($result)) {
                 break;
             }
         }
     } catch (Exception $e) {
         print_r($e->getMessage());
         ActiveRecord::rollback();
     }
     if (!$this->application->isDevMode() || 1) {
         ActiveRecord::commit();
     } else {
         ActiveRecord::rollback();
     }
     $importer->reset();
     return $response;
 }
 private function send(NewsletterMessage $newsletter)
 {
     set_time_limit(0);
     $response = new JSONResponse(null);
     $data = $this->getRecipientData($this->request->toArray());
     $total = count($data);
     $subscribers = $users = array();
     foreach ($data as $row) {
         if ($row['userID']) {
             $users[] = $row['userID'];
         } else {
             $subscribers[] = $row['subscriberID'];
         }
     }
     $progress = 0;
     foreach (array('User' => $users, 'NewsletterSubscriber' => $subscribers) as $table => $ids) {
         foreach (array_chunk($ids, self::PROGRESS_FLUSH_INTERVAL) as $chunk) {
             foreach (ActiveRecordModel::getRecordSet($table, new ARSelectFilter(new InCond(new ARFieldHandle($table, 'ID'), $chunk))) as $recipient) {
                 $progress++;
                 $newsletter->send($recipient, $this->application);
                 if ($progress % self::PROGRESS_FLUSH_INTERVAL == 0 || $total == $progress) {
                     $response->flush($this->getJsonResponse(array('progress' => $progress, 'total' => $total)));
                 }
             }
             ActiveRecord::clearPool();
         }
     }
     $newsletter->markAsSent();
     $response->flush($this->getJsonResponse(array('progress' => 0, 'total' => $total)));
     exit;
 }
 public function import()
 {
     $options = unserialize(base64_decode($this->request->get('options')));
     $response = new JSONResponse(null);
     if (file_exists($this->getCancelFile())) {
         unlink($this->getCancelFile());
     }
     if (!$this->request->get('continue')) {
         $this->clearCacheProgress();
     }
     $import = $this->getImportInstance();
     set_time_limit(0);
     ignore_user_abort(true);
     $profile = new CsvImportProfile($import->getClassName());
     // map CSV fields to LiveCart fields
     $params = $this->request->get('params');
     foreach ($this->request->get('column') as $key => $value) {
         if ($value) {
             $fieldParams = !empty($params[$key]) ? $params[$key] : array();
             $profile->setField($key, $value, array_filter($fieldParams));
         }
     }
     $profile->setParam('isHead', $this->request->get('firstHeader'));
     if ($this->request->get('saveProfile')) {
         $path = $this->getProfileDirectory($import) . $this->request->get('profileName') . '.ini';
         $profile->setFileName($path);
         $profile->save();
     }
     // get import root category
     if ($import->isRootCategory()) {
         $profile->setParam('category', $this->request->get('category'));
     }
     $import->beforeImport($profile);
     $csv = new CsvFile($this->request->get('file'), $this->request->get('delimiter'));
     $total = $csv->getRecordCount();
     if ($this->request->get('firstHeader')) {
         $total -= 1;
     }
     if ($this->request->get('firstHeader')) {
         $import->skipHeader($csv);
         $import->skipHeader($csv);
     }
     $progress = 0;
     $processed = 0;
     if ($this->request->get('continue')) {
         $import->setImportPosition($csv, $this->getCacheProgress() + 1);
         $progress = $this->getCacheProgress();
     } else {
         if (!empty($options['transaction'])) {
             ActiveRecord::beginTransaction();
         }
     }
     if (empty($options['transaction'])) {
         $this->request->set('continue', true);
     }
     $import->setOptions($options);
     if ($uid = $this->request->get('uid')) {
         $import->setUID($uid);
     }
     do {
         $progress += $import->importFileChunk($csv, $profile, 1);
         // continue timed-out import
         if ($this->request->get('continue')) {
             $this->setCacheProgress($progress);
         }
         ActiveRecord::clearPool();
         if ($progress % self::PROGRESS_FLUSH_INTERVAL == 0 || $total == $progress) {
             $response->flush($this->getResponse(array('progress' => $progress, 'total' => $total, 'uid' => $import->getUID(), 'lastName' => $import->getLastImportedRecordName())));
             //echo '|' . round(memory_get_usage() / (1024*1024), 1) . '|' . count($categories) . "\n";
         }
         // test non-transactional mode
         //if (!$this->request->get('continue')) exit;
         if (connection_aborted()) {
             if ($this->request->get('continue')) {
                 exit;
             } else {
                 $this->cancel();
             }
         }
     } while (!$import->isCompleted($csv));
     if (!empty($options['missing']) && 'keep' != $options['missing']) {
         $filter = $import->getMissingRecordFilter($profile);
         if ('disable' == $options['missing']) {
             $import->disableRecords($filter);
         } else {
             if ('delete' == $options['missing']) {
                 $import->deleteRecords($filter);
             }
         }
     }
     $import->afterImport();
     if (!$this->request->get('continue')) {
         //ActiveRecord::rollback();
         ActiveRecord::commit();
     }
     $response->flush($this->getResponse(array('progress' => 0, 'total' => $total)));
     //echo '|' . round(memory_get_usage() / (1024*1024), 1);
     exit;
 }
 public function step5()
 {
     Loader::model('file_set');
     Loader::model('file_list');
     $response = new JSONResponse();
     $response->setStatus(false);
     $user = $this->post("user");
     $repos = $this->post("repos");
     $file = $this->post("file");
     $plugin = new PluginArchive();
     $pluginDir = $plugin->unzip($file);
     $u = new User();
     $fs = FileSet::createAndGetSet($repos, 1, $u->getUserID());
     $importer = new MootoolsPluginImporter($fs);
     $importFiles = $importer->getComponentFiles($pluginDir . "/Source/");
     $resultFiles = array();
     foreach ($importFiles as $file) {
         $result = $importer->canImport($file);
         if ($result) {
             $resultFiles[$file] = $importer->addFile($file);
         }
     }
     $response->setMessage(t("Plugin taking was completed."));
     $response->setParameter("files", $resultFiles);
     $response->setStatus(true);
     $response->flush();
 }