wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.ResourceListController.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.ResourceListController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/presentation/class.Controller.php");
20 require_once(BASE."wcmf/lib/util/class.URIUtil.php");
21 require_once(BASE."wcmf/lib/util/class.GraphicsUtil.php");
22 
23 include_once(BASE."wcmf/3rdparty/elfinder/php/elFinderConnector.class.php");
24 include_once(BASE."wcmf/3rdparty/elfinder/php/elFinder.class.php");
25 include_once(BASE."wcmf/3rdparty/elfinder/php/elFinderVolumeDriver.class.php");
26 include_once(BASE."wcmf/3rdparty/elfinder/php/elFinderVolumeLocalFileSystem.class.php");
27 
28 /**
29  * @class ResourceListController
30  * @ingroup Controller
31  * @brief ResourceListController is a controller used to manage resources from the
32  * server and displays it using the resourcelist.tpl template. For file resources
33  * it uses elFinder.
34  *
35  * <b>Input actions:</b>
36  * - unspecified: List all resources in the given directory
37  *
38  * <b>Output actions:</b>
39  * - @em ok In any case
40  *
41  * @param[in] directory The name of the directory to scan for resources (optinal, if not given the directory is
42  * retrieved using the ResourceListController::getResourceBaseDir() method).
43  * @param[in,out] type The main resource type to list (e.g. link or image, default is image if not given).
44  * @param[in,out] subtype The sub resource type to list (e.g. content or resource, default is resource if not given).
45  * @param[in,out] fieldName The name of the input field to set the resource value to.
46  * @param[out] rootPath The base path for resources.
47  * @param[out] rootUrl The root url for resources.
48  *
49  * @author ingo herwig <ingo@wemove.com>
50  */
52 {
53  // session name constants
54  var $DIRECTORY_VARNAME = 'ResourceListController.directory';
55 
56  // the resource directory
58 
59  /**
60  * @see Controller::initialize()
61  */
62  function initialize(&$request, &$response)
63  {
64  parent::initialize($request, $response);
65 
66  $baseDir = $this->getResourceBaseDir();
67 
68  // default directory is base dir
69  $this->_directory = $baseDir;
70 
71  // try to get a formerly selected directory from the session
72  $session = &SessionData::getInstance();
73  if ($session->exist($this->DIRECTORY_VARNAME)) {
74  $this->_directory = $session->get($this->DIRECTORY_VARNAME);
75  }
76 
77  // allow to override the directory with a request parameter
78  if ($request->hasValue('directory') && strpos($request->getValue('directory'), $baseDir) === 0) {
79  $this->_directory = $request->getValue('directory');
80  }
81 
82  // store the directory in the session
83  $session->set($this->DIRECTORY_VARNAME, $this->_directory);
84  }
85 
86  /**
87  * @see Controller::hasView()
88  */
89  function hasView()
90  {
91  if (in_array($this->_request->getAction(), array("browseresources", "crop"))) {
92  return true;
93  }
94  else {
95  return false;
96  }
97  }
98 
99  /**
100  * Process finder actions.
101  * @return True in every case.
102  * @see Controller::executeKernel()
103  */
104  function executeKernel()
105  {
106  // get root path and root url for the browser
107  $rootPath = $this->getResourceBaseDir();
108  $refURL = URIUtil::getProtocolStr().$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
109  $rootUrl = URIUtil::makeAbsolute($rootPath, $refURL);
110 
111  // set common response values
112  if ($this->_request->hasValue('fieldName')) {
113  $this->_response->setValue('fieldName', $this->_request->getValue('fieldName'));
114  }
115  $this->_response->setValue('rootUrl', $rootUrl);
116  $this->_response->setValue('rootPath', $rootPath);
117  $this->_response->setValue('type', $this->_request->getValue('type', 'image'));
118  $this->_response->setValue('subtype', $this->_request->getValue('subtype', 'resource'));
119  $this->_response->setValue('CKEditorFuncNum', $this->_request->getValue('CKEditorFuncNum'));
120 
121  if (!in_array($this->_request->getAction(), array("browseresources", "crop"))) {
122  $opts = array(
123  // 'debug' => true,
124  'roots' => array(
125  array(
126  'driver' => 'LocalFileSystem', // driver for accessing file system (REQUIRED)
127  'path' => $rootPath, // path to files (REQUIRED)
128  'URL' => $rootUrl, // URL to files (REQUIRED)
129  'alias' => 'Media',
130  'startPath' => $this->_directory
131  )
132  ),
133  'bind' => array(
134  'rename rm paste' => array($this, 'onFileMoved')
135  ),
136  );
137 
138  // run elFinder
139  $connector = new elFinderConnector(new elFinder($opts));
140  $connector->run();
141 
142  $this->_response->setAction('ok');
143  return true;
144  }
145  else {
146  // custom crop action
147  if ($this->_request->getAction() == 'crop') {
148  $file = $this->_request->getValue('oid');
149  $this->_response->setValue('oid', $file);
150  if ($this->_request->hasValue('cropX') && $this->_request->hasValue('cropY') &&
151  $this->_request->hasValue('cropWidth') && $this->_request->hasValue('cropHeight')) {
152  // extract crop info
153  $x = $this->_request->getValue('cropX');
154  $y = $this->_request->getValue('cropY');
155  $w = $this->_request->getValue('cropWidth');
156  $h = $this->_request->getValue('cropHeight');
157 
158  // define target file name
159  $cropInfo = 'x'.$x.'y'.$y.'w'.$w.'h'.$h;
160  $pathParts = pathinfo($file);
161  $targetFile = $pathParts['dirname'].'/'.$pathParts['filename'].'_'.$cropInfo.'.'.$pathParts['extension'];
162 
163  // crop the image
164  $graphicsUtil = new GraphicsUtil();
165  $graphicsUtil->cropImage($file, $targetFile, $w, $h, $x, $y);
166  $this->_response->setValue('fieldName', $this->_request->getValue('fieldName'));
167  $this->_response->setAction('browseresources');
168  return true;
169  }
170  }
171 
172  $this->_response->setAction('ok');
173  return false;
174  }
175  }
176 
177  /**
178  * Called when file is moved
179  * @param cmd elFinder command name
180  * @param result Command result as array
181  * @param args Command arguments from client
182  * @param elfinder elFinder instance
183  * @return void|true
184  **/
185  function onFileMoved($cmd, $result, $args, $elfinder)
186  {
187  $addedFiles = $result['added'];
188  $removedFiles = $result['removed'];
189  for ($i=0, $count=sizeof($removedFiles); $i<$count; $i++) {
190  $source = $removedFiles[$i]['realpath'];
191  $target = $elfinder->realpath($addedFiles[$i]['hash']);
192  }
193  Log::debug($cmd." file: ".$source." -> ".$target, __CLASS__);
194  }
195 
196 
197  /**
198  * Get the base directory for resources. The default implementation
199  * returns the directory configured by the 'uploadDir' key in section 'media'.
200  * @return The directory name
201  * @note Subclasses will override this method to implement special application requirements
202  */
204  {
205  $parser = &InifileParser::getInstance();
206  if(($uploadDir = $parser->getValue('uploadDir', 'media')) !== false)
207  {
208  // remove slash if nescessary
209  if (substr($uploadDir, -1) == '/')
210  $uploadDir = substr($uploadDir, 0, -1);
211 
212  return $uploadDir;
213  }
214  return '';
215  }
216 }
217 ?>
GraphicsUtil provides support for graphic manipulation.
debug($message, $category)
Definition: class.Log.php:39
ResourceListController is a controller used to manage resources from the server and displays it using...
makeAbsolute($rel_uri, $base, $REMOVE_LEADING_DOTS=true)
onFileMoved($cmd, $result, $args, $elfinder)
Controller is the base class of all controllers. If a Controller has a view it is expected to reside ...