Example #1
0
 /**
  * Attempts to import data from a file
  */
 public static function importUrl($model, $base_url = null, $auto_backup = true)
 {
     // Try to back up the DB first
     if ($auto_backup) {
         static::clearAutoBackups();
         try {
             $result = Project::backupDatabase('pre_import', true);
         } catch (\Exception $e) {
         }
     }
     // Base URL fallback
     if (empty($base_url)) {
         $base_url = \CMF\Model\DevSettings::instance()->parent_site;
     }
     // Can't continue if the base URL is empty
     if (empty($base_url)) {
         return array('success' => false, 'message' => 'No URL was provided for import');
     }
     // Create the request
     $type = $model::importType();
     $type_plural = \Inflector::pluralize($type);
     $url = rtrim($base_url, '/') . '/api/' . $type_plural . '.json';
     // Add parameters
     $params = $model::importParameters();
     if (!empty($params) && is_array($params)) {
         $url .= '?' . http_build_query($params);
     }
     try {
         $loaded = static::getDataFromUrl($url);
     } catch (\Exception $e) {
         return array('success' => false, 'message' => $e->getMessage());
     }
     $data = null;
     $lang = null;
     if (is_array($loaded) && isset($loaded['body'])) {
         $data = $loaded['body'];
         if (isset($loaded['lang'])) {
             $lang = $loaded['lang'];
         }
     }
     // Standardise the root level meta object
     if (!isset($data['meta'])) {
         $data['meta'] = array();
     }
     if (!isset($data['meta']['type'])) {
         $data['meta']['type'] = $type_plural;
     }
     // Standardise the root level links object
     if (!isset($data['links'])) {
         $data['links'] = array();
     }
     if (!isset($data['links']['self'])) {
         $data['links']['self'] = $url;
     }
     // Import the data
     return static::importData($data, $model, false, $lang);
 }
Example #2
0
 /**
  * Generates a SQL dump of the database
  */
 public function db_backup()
 {
     $result = Project::backupDatabase(\Cli::option('name'), !!\Cli::option('gzip'));
     \Cli::write("Database '" . $result['dbname'] . "' backed up to " . $result['file'] . ". Time taken: " . $result['time'] . "s", 'green');
 }