Exemple #1
0
 /**
  * Parse any media group placeholders
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function parseMedia(AbstractController $controller, Application $application)
 {
     if ($controller->hasView() && ($controller instanceof \Phire\Categories\Controller\IndexController || $controller instanceof \Phire\Content\Controller\IndexController)) {
         $data = $controller->view()->getData();
         foreach ($data as $key => $value) {
             if (is_string($value)) {
                 $subIds = self::parseLibraryIds($value);
                 if (count($subIds) > 0) {
                     $media = new Model\Media();
                     foreach ($subIds as $sid) {
                         $view = new \Pop\View\View($value, ['media_' . $sid => $media->getAllByLibraryId($sid)]);
                         $controller->view()->{$key} = $view->render();
                     }
                 }
             }
         }
         $body = $controller->response()->getBody();
         $ids = self::parseLibraryIds($body);
         if (count($ids) > 0) {
             $media = new Model\Media();
             foreach ($ids as $id) {
                 $key = 'media_' . $id;
                 $controller->view()->{$key} = $media->getAllByLibraryId($id);
             }
         }
     }
 }
 /**
  * Browser action method
  *
  * @param  int $lid
  * @return void
  */
 public function browser($lid = null)
 {
     if (null !== $this->request->getQuery('editor') && null !== $this->request->getQuery('type')) {
         $this->prepareView('media/browser.phtml');
         $this->view->title = 'Media Browser';
         if ($this->request->isPost()) {
             $library = new Model\MediaLibrary();
             $library->getById($lid);
             $settings = $library->getSettings();
             if (count($settings) == 4) {
                 $upload = new \Pop\File\Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
                 if ($upload->test($_FILES['file'])) {
                     $media = new Model\Media();
                     $media->save($_FILES['file'], $this->request->getPost());
                     $this->sess->setRequestValue('saved', true);
                     $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI']));
                 } else {
                     $this->redirect(str_replace('&error=1', '', $_SERVER['REQUEST_URI']) . '&error=1');
                 }
             }
         }
         if (null !== $lid && null !== $this->request->getQuery('asset') && null !== $this->request->getQuery('asset_type')) {
             $assets = [];
             $limit = $this->config->pagination;
             $page = $this->request->getQuery('page');
             $pages = null;
             $library = new Model\MediaLibrary();
             if ($this->request->getQuery('asset_type') == 'content' && $this->application->isRegistered('phire-content')) {
                 $type = \Phire\Content\Table\ContentTypes::findById($lid);
                 $content = \Phire\Content\Table\Content::findBy(['type_id' => $lid], ['order' => 'order, id ASC']);
                 foreach ($content->rows() as $c) {
                     $assets[] = ['id' => $c->id, 'title' => $c->title, 'uri' => BASE_PATH . $c->uri];
                 }
                 if (isset($type->id)) {
                     $this->view->assetType = $type->name;
                 }
             } else {
                 if ($this->request->getQuery('asset_type') == 'media') {
                     $library->getById($lid);
                     $media = new Model\Media(['lid' => $lid]);
                     if ($this->request->getQuery('asset') == 'file') {
                         $assets = $media->getAll();
                     } else {
                         if ($this->request->getQuery('asset') == 'image') {
                             $assets = $media->getAllImages();
                         }
                     }
                     $this->view->assetType = $library->name;
                 }
             }
             if (count($assets) > $limit) {
                 $pages = new Paginator(count($assets), $limit);
                 $pages->useInput(true);
                 $offset = null !== $page && (int) $page > 1 ? $page * $limit - $limit : 0;
                 $assets = array_slice($assets, $offset, $limit, true);
             }
             $this->view->title = 'Media' . (null !== $this->view->assetType ? ' : ' . $this->view->assetType : null);
             $this->view->lid = $lid;
             $this->view->folder = $library->folder;
             $this->view->sizes = null !== $library->actions ? array_keys($library->actions) : [];
             $this->view->pages = $pages;
             $this->view->browserAssets = $assets;
         } else {
             $libraries = [];
             $limit = null;
             $pages = null;
             if ($this->request->getQuery('type') == 'file' && $this->application->isRegistered('phire-content')) {
                 $types = \Phire\Content\Table\ContentTypes::findAll(['order' => 'order ASC']);
                 if ($types->hasRows()) {
                     $libraries['Content'] = [];
                     foreach ($types->rows() as $type) {
                         $libraries['Content'][$type->id] = $type->name;
                     }
                 }
             }
             $libraries['Media'] = [];
             $library = new Model\MediaLibrary();
             $libs = $library->getAll();
             foreach ($libs as $lib) {
                 $libraries['Media'][$lib->id] = $lib->name;
             }
             $this->view->title = 'Media';
             $this->view->pages = $pages;
             $this->view->lid = $lid;
             $this->view->libraries = $libraries;
         }
         $this->send();
     } else {
         $this->redirect(BASE_PATH . APP_URI . '/media');
     }
 }
 /**
  * Remove a library
  *
  * @param  array $fields
  * @return void
  */
 public function process(array $fields)
 {
     if (isset($fields['rm_media_libraries'])) {
         foreach ($fields['rm_media_libraries'] as $id) {
             $library = Table\MediaLibraries::findById((int) $id);
             if (isset($library->id)) {
                 $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder);
                 $dir->emptyDir(true);
                 $library->delete();
             }
         }
     } else {
         if (isset($fields['process_media_libraries'])) {
             foreach ($fields['process_media_libraries'] as $id) {
                 $library = new self();
                 $library->getById((int) $id);
                 if (isset($library->id)) {
                     $class = 'Pop\\Image\\' . $library->adapter;
                     $formats = array_keys($class::getFormats());
                     $media = Table\Media::findBy(['library_id' => $library->id]);
                     if ($media->count() > 0) {
                         $med = new Media();
                         foreach ($media->rows() as $m) {
                             $fileParts = pathinfo($m->file);
                             if (!empty($fileParts['extension']) && in_array($fileParts['extension'], $formats)) {
                                 $med->processImage($m->file, $library);
                             }
                         }
                     }
                 }
             }
         }
     }
 }