Example #1
0
 /**
  * @param $new_name
  * @param $file
  * @return string $url_name
  */
 protected function createAsset($new_name, $file, $remote = true)
 {
     //$this->modx->importx->log('error', 'Create Asset ');
     if (is_object($this->mediaSource)) {
         //$this->modx->importx->log('error', 'Yes: '.$file);
         if ($remote && strpos($file, 'http') !== 0) {
             $file = rtrim($this->config['link'], '/') . '/' . ltrim($file, '/');
         }
         // directory:
         $object_path = '';
         if (in_array(strtolower(pathinfo($new_name, PATHINFO_EXTENSION)), array('doc', 'pdf', 'docx', 'csv', 'xlsx'))) {
             $object_path = 'docs' . DIRECTORY_SEPARATOR;
         }
         $content = $remote ? $this->getRemoteData($file) : file_get_contents($file);
         $file = rawurldecode($this->mediaSource->createObject($object_path, $new_name, $content));
         $basePath = $this->mediaSource->getBasePath($file);
         $baseUrl = $this->mediaSource->getBaseUrl($file);
         $file = str_replace($basePath, $baseUrl, $file);
         if ($file !== false) {
             $this->modx->importx->log('info', 'File transferred to: ' . $file);
         }
         return $file;
     }
     return false;
 }