Beispiel #1
0
 /**
  * Sync assets to CDN
  */
 public function sync()
 {
     \Cli::write("Syncing user files...");
     Storage::syncFileFields();
     \Cli::write("Syncing static assets...");
     Storage::syncAssets();
     \Cli::write("Done!", 'green');
 }
Beispiel #2
0
 protected function outputImage($path, $params)
 {
     try {
         // Output the image using glide server
         ob_clean();
         \CMF\Image::server()->outputImage($path, $this->getImageParams($params));
     } catch (\Exception $e) {
         // Output a placeholder - exception could mean file doesn't exist, or some other environment issue
         if (!\CMF\Image::server()->getSource()->has('assets/images/placeholder.png')) {
             \CMF\Image::server()->setSource(\CMF\Storage::adapter(CMFPATH . 'modules/image/public'));
         }
         \CMF\Image::server()->outputImage('assets/images/placeholder.png', array('w' => intval($params['w']), 'h' => intval($params['h']), 'bg' => 'efefef', 'fit' => 'fill'));
     }
 }
Beispiel #3
0
 /**
  * Get a url to an asset
  */
 public static function asset($url = null)
 {
     if (empty($url)) {
         return '';
     }
     if (stripos($url, 'http://') !== 0 && \Config::get('cmf.cdn.enabled')) {
         return \CMF\Storage::getCDNAssetUrl($url);
     }
     return \Uri::create($url);
 }
Beispiel #4
0
 /**
  * Syncs the files table with file and image fields
  */
 public function action_sync_files_table()
 {
     \CMF\Storage::syncFileFields();
     $this->heading = 'All files were synced';
     $this->template = 'admin/generic.twig';
 }
Beispiel #5
0
 /**
  * Gets an image URL (cdn if needed) from a bunch of params
  */
 public static function getUrlFromParams($params)
 {
     if (!is_array($params)) {
         return null;
     }
     $url = $params['url'];
     unset($params['url']);
     // Do some CDN magic if needed
     if (\Config::get('cmf.cdn.enabled') && ($cdn = \CMF\Storage::getCDNAdapter())) {
         $original = isset($params['path']) ? $params['path'] : ltrim($url, '/');
         $ext = '';
         // Generate a resized copy of the image locally
         if (isset($params['path'])) {
             unset($params['path']);
             try {
                 $resized = $remote = static::server()->makeImage($original, $params);
                 $sourceAdapter = static::server()->getCache();
                 $ext = @pathinfo(DOCROOT . $original, PATHINFO_EXTENSION) ?: '';
                 if (!empty($ext)) {
                     $remote .= '.' . $ext;
                 }
             } catch (\Exception $e) {
                 return $url;
             }
         } else {
             $resized = $original;
             $remote = $original . '/' . pathinfo(DOCROOT . $original, PATHINFO_BASENAME);
             $sourceAdapter = static::server()->getSource();
         }
         // Try and find a cached version of this file
         $paths = array($original, $resized);
         $files = \DB::query("SELECT * FROM `_files` AS f WHERE f.path IN:paths ORDER BY f.id DESC")->bind('paths', $paths)->execute()->as_array();
         $originalInfo = null;
         $resizedInfo = null;
         foreach ($files as $file) {
             if (is_null($originalInfo) && @$file['storage'] == 'local') {
                 $originalInfo = $file;
                 if (!is_null($resizedInfo)) {
                     break;
                 }
             }
             if (is_null($resizedInfo) && @$file['storage'] == 'cdn' && $file['path'] == $resized) {
                 $resizedInfo = $file;
                 if (!is_null($originalInfo)) {
                     break;
                 }
             }
         }
         // If there's no file in the DB, we might need to push to the CDN
         if (is_null($resizedInfo)) {
             // Write the file to CDN if it doesn't exist there
             if (!$cdn->has($remote)) {
                 $cdn->write($remote, $sourceAdapter->read($resized), array('visibility' => 'public'));
             }
             $url = '/' . $remote;
             // Write a file entry to the database
             \DB::insert('_files')->set(array('path' => $resized, 'url' => $url, 'type' => !empty($originalInfo) ? $originalInfo['type'] : null, 'field' => !empty($originalInfo) ? $originalInfo['field'] : null, 'storage' => 'cdn', 'params' => !empty($params) ? serialize($params) : null, 'parent' => !empty($originalInfo) ? intval($originalInfo['id']) : null, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')))->execute();
         } else {
             // Fill in missing information to the file record if we have it now
             if (!is_null($originalInfo) && empty($resizedInfo['parent'])) {
                 \DB::update('_files')->set(array('parent' => intval(@$originalInfo['id']), 'type' => @$originalInfo['type'], 'field' => @$originalInfo['field']))->where('id', '=', $resizedInfo['id'])->execute();
             }
             $url = $resizedInfo['url'];
         }
         return rtrim(\Config::get('cmf.cdn.base_url', ''), '/') . $url;
     }
     return $url;
 }
Beispiel #6
0
 /**
  * Either creates or updates a model depending on whether an ID is passed in.
  */
 public function action_save($table_name, $id = null, $method = null)
 {
     // Find class name and metadata etc
     $class_name = \Admin::getClassForTable($table_name);
     if ($class_name === false) {
         return $this->customPageOr404(array($table_name, $method), "type");
     }
     $can_edit = \CMF\Auth::can('edit', $class_name);
     if (!$can_edit) {
         return $this->show403('action_singular', array('action' => \Lang::get('admin.verbs.edit'), 'resource' => strtolower($class_name::singular())));
     }
     $metadata = $class_name::metadata();
     $plural = $class_name::plural();
     $singular = $class_name::singular();
     $list_page_segment = $metadata->table['name'];
     if ($metadata->name != $metadata->rootEntityName) {
         $rootClass = $metadata->rootEntityName;
         $rootMeta = $rootClass::metadata();
         $list_page_segment = $rootMeta->table['name'];
     }
     if (\Input::param('alias', false) !== false) {
         $plural = 'Links';
         $singular = 'Link';
     }
     \Admin::setCurrentClass($class_name);
     $message = \Lang::get('admin.messages.item_save_success', array('resource' => $singular));
     // Find the model, or create a new one if there's no ID
     if ($exists = isset($id) && !empty($id)) {
         $model = $class_name::find($id);
         if (is_null($model)) {
             return $this->show404(null, "type");
         }
     } else {
         $model = new $class_name();
         $message = \Lang::get('admin.messages.item_create_success', array('resource' => $singular));
     }
     $create_new = \Input::post('create_new', false) !== false;
     $save_and_close = \Input::post('saveAndClose', false) !== false;
     $old_url = $model->url;
     // Populate the model with posted data
     $model->populate(\Input::post());
     // Validate the model
     if ($model->validate(null, null, array('id', 'pos'))) {
         $em = \D::manager();
         $em->persist($model);
         $em->flush();
         if (!$exists && $model->_has_processing_errors) {
             // Try populating the model again if we've had errors - they could be to do with unit of work
             $model->populate(\Input::post());
             $em->persist($model);
             $em->flush();
         }
         // Sync file fields to DB
         try {
             \CMF\Storage::syncFileFieldsFor($model);
         } catch (\Exception $e) {
         }
         // Do something depending on what mode we're in...
         switch (\Input::param('_mode', 'default')) {
             // Renders a page with some JS that will close the fancybox popup
             case 'inline':
                 $options = $class_name::options();
                 $ids = array_keys($options);
                 $pos = array_search(strval($model->id), $ids);
                 if ($pos === false) {
                     $pos = 0;
                 }
                 $this->options = $options;
                 $this->pos = $pos;
                 $this->model = $model;
                 $this->className = $metadata->name;
                 $this->template = 'admin/item/saved-inline.twig';
                 return;
                 break;
             default:
                 $qs = \Uri::build_query_string(\Input::get());
                 if (strlen($qs) > 0) {
                     $qs = '?' . $qs;
                 }
                 \Session::set_flash('main_alert', array('attributes' => array('class' => 'alert-success'), 'msg' => $message));
                 if ($create_new) {
                     \Response::redirect("/admin/{$table_name}/create{$qs}", 'location');
                 }
                 if ($save_and_close) {
                     \Response::redirect("/admin/{$list_page_segment}" . $qs, 'location');
                 }
                 \Response::redirect("/admin/{$table_name}/" . $model->get('id') . "/edit{$qs}", 'location');
                 break;
         }
     }
     // If it's come this far, we have a problem. Render out the form with the errors...
     $this->actions = $class_name::actions();
     $this->form = new ModelForm($metadata, $model);
     $this->table_name = $metadata->table['name'];
     $this->model = $model;
     $this->qs = \Uri::build_query_string(\Input::get());
     $this->template = 'admin/item/edit.twig';
     // Permissions
     $this->can_edit = $can_edit;
     $this->can_create = \CMF\Auth::can('create', $class_name);
     $this->can_delete = \CMF\Auth::can('delete', $class_name);
     if ($exists) {
         $this->js['model'] = $class_name;
         $this->js['item_id'] = $model->id;
         $this->js['table_name'] = $table_name;
     }
     \Session::set_flash('main_alert', array('attributes' => array('class' => 'alert-danger'), 'msg' => \Lang::get('admin.errors.actions.save', array('resource' => strtolower($class_name::singular())))));
 }