Пример #1
0
 function import($cachable = false, $urlparams = false)
 {
     require_once JPATH_COMPONENT . '/helpers/import.php';
     date_default_timezone_set('UTC');
     echo "<h1>Executing importation</h1>";
     // set the default timezone to use. Available since PHP 5.1
     $date = date("ym") . date("d") - 1;
     //$date= 140930;
     switch ($_GET["view"]) {
         case 'additions':
             $URL = 'http://www.myairlease.com/efu_media/MSNs_additions_' . $date . '-01.xls';
             break;
         case 'removals':
             $URL = 'http://www.myairlease.com/efu_media/MSNs_removals_' . $date . '-01.xls';
             break;
         case 'updates':
             $URL = 'http://www.myairlease.com/efu_media/MSNs_updates_' . $date . '-01.xls';
             break;
     }
     $dest = '/Applications/XAMPP/xamppfiles/temp/file.xls';
     echo $URL;
     $file_headers = @get_headers($URL);
     echo $file_headers[0];
     if ($file_headers[0] != 'HTTP/1.1 200 OK') {
         echo "<p>File NOT found! Today no new " . $_GET["view"] . " from " . $_POST["source"] . '</p>';
     } else {
         echo "<p>New update found, ready to import.<br />Source: " . $_POST["source"] . '</p>';
         if (copy('http://www.myairlease.com/efu_media/MSNs_updates_' . $date . '-01.xls', $dest)) {
             echo "<p>file found and copied to our server successfully</p>";
             $myObject = new ImportHelper($_GET["view"], $_POST["source"], $dest);
             $myObject->PrepareValues();
         } else {
             echo "Server could not download and copied succesfully the excel file from " . $_POST["source"];
         }
     }
     echo '<a href="' . JURI::root() . 'administrator/index.php?option=com_available_assets&view=aircraftlist" class="btn btn-success">Return to Aircarf List</a>';
 }
 public function importPass()
 {
     if (ImportHelper::is_a($this->targetClass, 'SiteTree')) {
         throw new InvalidArgumentException("Don't run TruncateImporter on a SiteTree class");
     }
     // Check extensions
     if (!Object::has_extension($this->targetClass, 'LegacyDataObject')) {
         throw new Exception($this->targetClass . " does not have the LegacyDataObject extension");
     }
     // Update remote table to include _ImportedID column
     $this->setupRemoteTable();
     // Delete all existing records
     $existingRecords = DataObject::get($this->targetClass);
     $existingCount = $existingRecords->count();
     // Get other records
     $query = $this->getRemoteObjectsQuery();
     $remoteObjects = $this->task->query($query);
     $remoteCount = $remoteObjects->numRecords();
     // Start
     $this->task->message(" * Replacing {$existingCount} records with {$remoteCount} ones");
     // Truncate all tables
     $tables = ClassInfo::dataClassesFor($this->targetClass);
     foreach ($tables as $table) {
         DB::query('TRUNCATE "' . $table . '"');
     }
     $this->task->message(" * " . count($tables) . " tables truncated");
     // Add all objects
     $total = 0;
     foreach ($remoteObjects as $remoteObject) {
         // Show progress indicator
         $this->task->progress(++$total, $remoteCount);
         // Make new object
         $class = isset($remoteObject['ClassName']) && ImportHelper::is_a($remoteObject['ClassName'], $this->targetClass) ? $remoteObject['ClassName'] : $this->targetClass;
         // Direct copy data into the new object
         $localObject = $class::create();
         foreach ($remoteObject as $field => $value) {
             $localObject->{$field} = $value;
         }
         $localObject->LegacyID = $remoteObject['ID'];
         $localObject->write(false, true);
     }
     // Bulk update remote table
     $conn = $this->task->getRemoteConnection();
     $baseTable = $this->getRemoteBaseTable();
     $conn->query('UPDATE "' . $baseTable . '" SET "_ImportedID" = "ID", "_ImportedDate" = NOW()');
     // Done!
     $this->task->message(" * Result: {$total} added");
 }
 public function run($request)
 {
     parent::run($request);
     $tables = explode(',', $request->getVar('tables'));
     if (empty($tables)) {
         throw new InvalidArgumentException("No 'tables' parameter specified");
     }
     // Check relation we want to keep field
     $keepRelations = $request->getVar('keeprelations');
     switch ($keepRelations) {
         case 'true':
             $keepRelations = true;
             break;
         case 'false':
         case '0':
             $keepRelations = false;
             break;
         default:
             $keepRelations = explode(',', $keepRelations);
     }
     $this->message("== Importing bulk data ==");
     // Create temp file
     $this->message(" * Creating temp file");
     $this->tempFile = tempnam(TEMP_FOLDER, get_class() . '_mysqldump' . date('Y-m-d'));
     // Run mysql export on the remote table
     $this->message(" * Exporting tables with mysqldump");
     $this->exportTables($tables);
     // Import data into local table
     $this->message(" * Importing tables into mysql");
     $this->importTables();
     // Rebuild DB
     $this->message("== Rebuilding database ==");
     singleton("DatabaseAdmin")->doBuild(false);
     // Fix relation IDs
     if ($keepRelations === true) {
         $this->message("Keeping relations; Not resetting RelationIDs to zero");
     } else {
         $this->message("Invalidate has_one fields (bypass this with keeprelations=relationid)");
         foreach ($tables as $table) {
             $hasOne = Config::inst()->get($table, 'has_one', Config::UNINHERITED);
             if (empty($hasOne)) {
                 continue;
             }
             foreach ($hasOne as $relation => $class) {
                 $field = $relation . "ID";
                 if (is_array($keepRelations) && in_array($field, $keepRelations)) {
                     $this->message(" * Keeping relation {$table}.{$field}");
                 } else {
                     $this->message(" * Resetting relation {$table}.{$field} to zero on migrated table");
                     DB::query('UPDATE "' . $table . '" SET "' . $field . '" = 0');
                 }
             }
         }
     }
     // Mark _ImportedID, _ImportedDate and LegacyID
     $this->connectToRemoteSite();
     $this->message("== Marking records as migrated ==");
     foreach ($tables as $table) {
         // Don't mark non-base tables (subclasses of Member, etc)
         if (ImportHelper::is_a($table, 'DataObject') && $table != ClassInfo::baseDataClass($table)) {
             continue;
         }
         $remoteConn = $this->getRemoteConnection();
         // Setup schema
         $this->ensureTableHasColumn(DB::getConn(), $table, 'LegacyID', 'int(11) not null default 0');
         $this->ensureTableHasColumn($remoteConn, $table, '_ImportedID', 'int(11) not null default 0');
         $this->ensureTableHasColumn($remoteConn, $table, '_ImportedDate', 'datetime');
         // Bulk update
         $this->message(" * Updating {$table}._ImportedID and {$table}._ImportedDate");
         $remoteConn->query('UPDATE "' . $table . '" SET "_ImportedID" = "ID", "_ImportedDate" = NOW()');
         $this->message(" * Updating {$table}.LegacyID");
         DB::query('UPDATE "' . $table . '" SET "LegacyID" = "ID"');
     }
     // Done
     $this->message("Done!");
 }
Пример #4
0
/**
 * sanitizeSettingsMenu
 *
 * Sanitization callback for settings functions
 * Used to run imports for shows and profiles as well.
 *
 * @param mixed $options
 * @access public
 * @return void
 */
function sanitizeSettingsMenu($options)
{
    if ($options['import'] == 1 || $options['update'] == 1) {
        $import = new ImportHelper();
        try {
            if ($options['update'] == 1) {
                $import->update();
            } else {
                $import->import();
            }
        } catch (\Exception $e) {
            $_SESSION['wpspin-exception'] = $e->getMessage();
        }
    }
    return $options;
}
 /**
  * Create a local dataobject for import from an external dataset
  *
  * @param ArrayData $remoteObject Data from remote object
  * @return DataObject New dataobject created from $remoteObject
  */
 protected function createLocalFromRemoteObject(ArrayData $remoteObject)
 {
     // Allow data to override class
     $class = $remoteObject->ClassName && ImportHelper::is_a($remoteObject->ClassName, $this->targetClass) ? $remoteObject->ClassName : $this->targetClass;
     // Populate
     $localObject = $class::create();
     $this->updateLocalObject($localObject, $remoteObject);
     return $localObject;
 }
 /**
  * Update all has_ones that are linked to assets
  *
  * @param \DataObject $localObject
  * @param \ArrayData $remoteObject
  * @return null
  */
 protected function updateLocaleAssetRelations(\DataObject $localObject, \ArrayData $remoteObject)
 {
     // Now update all has_one => file relations
     $changed = false;
     $relations = $localObject->has_one();
     if (empty($relations)) {
         $this->task->message(" ** No has_ones on {$localObject->Title}", 2);
         return;
     }
     foreach ($relations as $relation => $class) {
         $this->task->message(" *** Checking relation name {$relation}", 3);
         // Link file
         if (!ImportHelper::is_a($class, 'File')) {
             $this->task->message(" **** {$relation} is not a File", 4);
             continue;
         }
         // Don't link folders
         if (ImportHelper::is_a($class, 'Folder')) {
             $this->task->message(" **** {$relation} is a folder", 4);
             continue;
         }
         // No need to import if found in a previous step
         $field = $relation . "ID";
         if ($localObject->{$field}) {
             $this->task->message(" **** {$relation} already has value {$localObject->{$field}} on local object", 4);
             continue;
         }
         // If the remote object doesn't have this field then can also skip it
         $remoteFileID = intval($remoteObject->{$field});
         if (empty($remoteFileID)) {
             $this->task->message(" **** {$relation} has no value on remote object", 4);
             continue;
         }
         // Find remote file with this ID
         $remoteFile = $this->findRemoteFile(array(sprintf('"ID" = %d', intval($remoteFileID))));
         if (!$remoteFile) {
             $this->task->error("Could not find {$relation} file with id {$remoteFileID}");
             continue;
         }
         // Ensure that this file has a valid name
         if (!$this->isValidFile($remoteFile->Name)) {
             $this->task->error("Remote {$relation} file does not have a valid name '" . $remoteFile->Name . "'");
             continue;
         }
         // Copy file to filesystem and save
         $localFile = $this->findOrImportFile($remoteFile);
         if (!$localFile) {
             $this->task->error("Failed to import {$relation} file '" . $remoteFile->Name . "'");
             continue;
         }
         // Save new file
         $changed = true;
         $this->task->message(" *** {$relation} assigned local value {$localFile->ID}", 3);
         $localObject->{$field} = $localFile->ID;
     }
     if ($changed) {
         $localObject->write();
     } else {
         $this->task->message(" ** No changes made to relations on {$localObject->Title}", 2);
     }
 }