wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.PageExportController.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.PageExportController.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/application/controller/class.BatchController.php");
20 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
21 require_once(BASE."wcmf/lib/presentation/class.WCMFInifileParser.php");
22 require_once(BASE."wcmf/lib/util/class.URIUtil.php");
23 
24 /**
25  * @class PageExportController
26  * @ingroup Controller
27  * @brief PageExportController is an abstract controller that is used as base class
28  * for Controller classes that export content to pages defined by templates.
29  * Export is triggered by any action except 'preview' and 'continue'.
30  * On 'preview' action PageExportController creates a preview corresponding to a given
31  * oid and context.
32  *
33  * To do this, subclasses define several work packages (see BatchController::getWorkPackage()).
34  * The callback functions may call base class methods to fulfill their tasks.
35  *
36  * An example callback could look like this:
37  * @code
38  function doIndexPage()
39  {
40  $filename = 'index.html';
41 
42  // initialize view
43  $outputView = &$this->initializeView($filename);
44 
45  // load and assign model
46  ...
47  $outputView->assign('message', 'Hello world');
48  ...
49 
50  // output page
51  $this->writeOutput($outputView, 'index');
52  }
53  * @endcode
54  *
55  * The corresponding configuration would look like this:
56  * @code
57 
58  [actionmapping]
59  ...
60  ??preview = PageExportController
61  ??export = PageExportController
62  PageExportController??continue = PageExportController
63  PageExportController??done = ViewController
64  ...
65 
66  [views]
67  ...
68  PageExportController?? = progressbar.tpl
69  PageExportController?index? = ../../templates/index_html.tpl
70  PageExportController?index?preview = ../../templates/index_html.tpl
71  ...
72 
73  * @endcode
74  *
75  * <b>Input actions:</b>
76  * - @em preview Show a preview of the given object
77  * - more actions see BatchController
78  *
79  * <b>Output actions:</b>
80  * - see BatchController
81  *
82  * @param[in] oid The object id of the object to display in preview mode
83  *
84  * @author ingo herwig <ingo@wemove.com>
85  */
87 {
88  // constants
89  var $FILENAME_VARNAME = 'PageExportController.filename';
90 
91  /**
92  * @see Controller::initialize()
93  */
94  function initialize(&$request, &$response)
95  {
96  Controller::initialize($request, $response);
97 
98  if ($request->getAction() != 'preview')
99  {
100  // do export batch
101  parent::initialize($request, $response);
102  }
103  }
104  /**
105  * @see Controller::executeKernel()
106  */
107  function executeKernel()
108  {
109  if ($this->_request->getAction() != 'preview')
110  {
111  // do export batch
112  return parent::executeKernel();
113  }
114  else
115  {
116  // do preview
117  $this->processPart();
118  // stop processing
119  return false;
120  }
121  }
122  /**
123  * If the given action is 'preview', this method calls - depending on the context - the
124  * preview callback method defined by the subclass (@see getPreviewCallback()).
125  * For any other action it delegates to the parent class processPart() method
126  * @see LongTaskController::processPart()
127  */
128  function processPart()
129  {
130  // do preview
131  if ($this->_request->getAction() == 'preview')
132  {
133  $previewItem = $this->_request->getValue('oid');
134 
135  $callback = $this->getPreviewCallback($this->_request->getContext());
136  if (!method_exists($this, $callback))
137  WCMFException::throwEx("Method ".$callback." must be implemented by ".get_class($this), __FILE__, __LINE__);
138  else
139  call_user_method($callback, &$this, array($previewItem));
140  }
141  else
142  parent::processPart();
143  }
144  /**
145  * Get the preview callback method for a given context.
146  * This method must have the same signature as one of the callbacks passed to BatchController::addWorkPackage().
147  * The oid array passed as argument to that method will only hold the oid passed as 'oid' parameter to the view.
148  * @note subclasses must implement this method.
149  */
150  function getPreviewCallback($context)
151  {
152  WCMFException::throwEx("getPreviewCallback() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
153  }
154 
155  /**
156  * HELPER FUNCTIONS
157  */
158 
159  /**
160  * Create and initialize the view. This is the main entrance to view generation.
161  * After creation this method calls the PageExportController::assignCommonValues()
162  * method, that subclasses may implement to assign common vallues to their views (such as page title).
163  * @param filename The filename of the exported page (relative to exportDir as provided by by getExportDir()),
164  * this value is even required for a preview to set the baseHref properly
165  * @return A reference to the created and initialized view
166  */
167  function &initializeView($filename)
168  {
169  $isPreview = ($this->_request->getAction() == 'preview');
170 
171  // create view
172  $outputView = &$this->createOutputView();
173 
174  // assign common values to view
175  if ($outputView != null)
176  {
177  // get export directory
178  $exportDir = $this->getExportDir();
179  if (!is_dir($exportDir))
180  mkdir($exportDir);
181  $outputView->assign($this->FILENAME_VARNAME, realpath($exportDir).'/'.$filename);
182 
183  if ($this->useBaseHref())
184  {
185  $refURL = UriUtil::getProtocolStr().$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
186  $baseHref = URIUtil::makeAbsolute($exportDir, $refURL).$filename;
187  $outputView->assign('baseHref', $baseHref);
188  }
189  // application specific values
190  $this->assignCommonValues($outputView);
191  }
192 
193  return $outputView;
194  }
195  /**
196  * Actually create the view for output.
197  * @return A reference to the created view
198  */
199  function &createOutputView()
200  {
201  $isPreview = ($this->_request->getAction() == 'preview');
202 
203  if (!$isPreview)
204  {
205  // for export we need to do view handling manually
206  // because we want to write the result to a file, not the browser window
207  $outputView = new View();
208  $outputView->setup();
209  $this->assignViewDefaults($outputView);
210  }
211  else
212  {
213  // for preview use regular view
214  $outputView = &$this->getView();
215  }
216  return $outputView;
217  }
218  /**
219  * Assign common values to the export view.
220  * This method is called when the view is initialized.
221  * @param view A reference to the view to assign the values to
222  */
223  function assignCommonValues(&$view) {}
224  /**
225  * Get the directory where the exported files should be placed.
226  * The default implementation gets the directory from the key 'exportDir' in the config section 'cms'
227  * @note subclasses override this method to implement special application requirements.
228  * @return The export directory name
229  */
230  function getExportDir()
231  {
232  $parser = &InifileParser::getInstance();
233  if (($exportDir = $parser->getValue('exportDir', 'cms')) === false)
234  WCMFException::throwEx($parser->getErrorMsg(), __FILE__, __LINE__);
235  return $exportDir;
236  }
237  /**
238  * Determine if a baseHref should be used in the html output. The baseHref metatag allows to interpret all
239  * resource paths used in the html code to be relative to the baseHref value. If you want to prevent this
240  * return false in this method.
241  * The default implementation returns true if the action is preview, else false
242  * @note subclasses override this method to implement special application requirements.
243  * @return True/False
244  */
245  function useBaseHref()
246  {
247  // we only need a base href for the preview pages because
248  // they don't exist in the filesystem
249  if ($this->_request->getAction() == 'preview')
250  return true;
251  else
252  return false;
253  }
254  /**
255  * Write the view content to a file.
256  * @param view A reference to the view to write
257  * @param context The context of the view template definition in the configuration file
258  */
259  function writeOutput(&$view, $context)
260  {
261  $isPreview = ($this->_request->getAction() == 'preview');
262  if ($isPreview)
263  return;
264 
265  $viewTemplate = '';
266  $parser = &WCMFInifileParser::getInstance();
267 
268  // get corresponding view
269  $actionKey = $parser->getBestActionKey('views', $this->_response->getSender(), $context, '');
270  if (($viewTemplate = BASE.$parser->getValue($actionKey, 'views')) === false)
271  WCMFException::throwEx("View definition missing for ".$this->_response->getSender()."?".$context.".", __FILE__, __LINE__);
272 
273  // assign datestamp to view
274  $view->assign('dateStamp', date("Y")."/".date("m"));
275 
276  // capture output into file
277  $filename = $view->get_template_vars($this->FILENAME_VARNAME);
278  $path = dirname($filename);
279  if (!file_exists($path))
280  mkdir($path);
281  $fp = fopen($filename, "w");
282  fputs($fp, $view->fetch($viewTemplate));
283  fclose($fp);
284  chmod($filename, 0755);
285  }
286 }
287 ?>
initialize(&$request, &$response)
throwEx($message, $file='', $line='')
makeAbsolute($rel_uri, $base, $REMOVE_LEADING_DOTS=true)
initialize(&$request, &$response)
assignViewDefaults(&$view)
PageExportController is an abstract controller that is used as base class for Controller classes that...
View is used by Controller to handle the view presentation in MVC pattern. View is a subclass of Smar...
Definition: class.View.php:31
BatchController allows to define work packages that will be processed in a sequence. It simplifies the usage of LongTaskController functionality for splitting different bigger tasks into many smaller (similar) tasks where the whole number of tasks isn't known at designtime.