wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.LongTaskController.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.LongTaskController.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.SessionData.php");
21 
22 /**
23  * @class LongTaskController
24  * @ingroup Controller
25  * @brief LongTaskController is a controller that may be used as
26  * base class for tasks, that require a long execution time
27  * such as construction of a list of pages.
28  *
29  * This is accomplished by breaking up the task into n pieces
30  * (steps: 1..n) and calling this controller recurringly until
31  * all n pieces are finished.
32  * The pieces are processed by the processPart() method that
33  * must be implemented by subclasses. Information about the
34  * the progress is provided by the methods getNumberOfSteps()
35  * and getStepNumber().
36  *
37  * To do the recurring calls without user interaction the controller needs
38  * a view that calls the submitAction('continue') function in the onLoad()
39  * event of the HTML page. If the parameter oneCall is set to true, the controller
40  * tries to accomplish the task in one call.
41  *
42  * A possible configuration could be:
43  *
44  * @code
45  * [actionmapping]
46  * ??longTask = MyLongTaskController
47  * MyLongTaskController??continue = MyLongTaskController
48  * MyLongTaskController??done = DisplayController
49  *
50  * [views]
51  * MyLongTaskController?? = progressbar.tpl
52  * @endcode
53  *
54  * <b>Input actions:</b>
55  * - @em continue Continue with the next step
56  * - unspecified: Initialize the task
57  *
58  * <b>Output actions:</b>
59  * - @em done If finished
60  *
61  * @param[in] oneCall True/False wether to accomplish the task in one call (optional, default: false)
62  * @param[out] stepNumber The current step starting with 1, ending with numberOfSteps+1
63  * @param[out] numberOfSteps Total number of steps
64  * @param[out] stepsArray An arry of dots which size is equal to stepNumber (useful for views to iterate over)
65  * @param[out] displayText The display text for the current step
66  * @param[out] summaryText The summary text (only available in the last step)
67  *
68  * @author ingo herwig <ingo@wemove.com>
69  */
71 {
72  // constants
73  var $STEP_SESSION_VARNAME = 'LongTaskController.curStep';
74  var $ONE_CALL_SESSION_VARNAME = 'LongTaskController.oneCall';
75 
76  // current step
77  var $_curStep = 1;
78 
79  /**
80  * @see Controller::initialize()
81  */
82  function initialize(&$request, &$response)
83  {
84  parent::initialize($request, $response);
85 
86  $session = &SessionData::getInstance();
87  if ($request->getAction() == 'continue')
88  {
89  // get step for current call from session
90  if ($session->exist($this->STEP_SESSION_VARNAME))
91  $this->_curStep = $session->get($this->STEP_SESSION_VARNAME);
92  else
93  WCMFException::throwEx("Error initializing LongTaskController: ".get_class($this), __FILE__, __LINE__);
94  }
95  else
96  {
97  // first call, initialize step session variable
98  $this->_curStep = 1;
99  $this->initializeTask();
100  $session->set($this->ONE_CALL_SESSION_VARNAME, $request->getBooleanValue('oneCall', false));
101  }
102  $step = $this->_curStep+1;
103  $session->set($this->STEP_SESSION_VARNAME, $step);
104  }
105  /**
106  * @see Controller::hasView()
107  */
108  function hasView()
109  {
110  return true;
111  }
112  /**
113  * Do processing and assign Node data to View.
114  * @return Array of given context and action 'done' if finished.
115  * False else.
116  * @see Controller::executeKernel()
117  */
118  function executeKernel()
119  {
120  // call processPart() in the second step,
121  // in the first step show status only
122  if ($this->_curStep > 1)
123  $this->processPart();
124 
125  if ($this->_curStep <= $this->getNumberOfSteps()+1)
126  {
127  $this->_response->setValue('stepNumber', $this->_curStep);
128  $this->_response->setValue('numberOfSteps', $this->getNumberOfSteps());
129  // assign an array holding number of steps elements for use with
130  // smarty section command
131  $stepsArray = array();
132  for ($i=0; $i<$this->getNumberOfSteps(); $i++)
133  array_push($stepsArray, '.');
134  $this->_response->setValue('stepsArray', $stepsArray);
135  $this->_response->setValue('displayText', $this->getDisplayText($this->_curStep));
136 
137  // add the summary message
138  $this->_response->setValue('summaryText', $this->getSummaryText());
139 
140  $session = &SessionData::getInstance();
141  if ($session->get($this->ONE_CALL_SESSION_VARNAME) == false) {
142  // show progress bar
143  return false;
144  }
145  else {
146  // proceed
147  $this->_response->setAction('continue');
148  return true;
149  }
150  }
151  else
152  {
153  // return control to application
154  $this->_response->setAction('done');
155  return true;
156  }
157  }
158  /**
159  * Get the number of the current step (1..number of steps).
160  * @return The number of the current step
161  */
162  function getStepNumber()
163  {
164  // since we actally call processPart() in the second step,
165  // return the real step number reduced by one
166  return $this->_curStep-1;
167  }
168  /**
169  * Get the total number of steps.
170  * @return The total number of steps
171  * @note subclasses must implement this method.
172  */
173  function getNumberOfSteps()
174  {
175  WCMFException::throwEx("getNumberOfSteps() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
176  }
177  /**
178  * Get the display text for a step.
179  * @param step The step to get the text for.
180  * @return The display text
181  * @note subclasses must implement this method.
182  */
183  function getDisplayText($step)
184  {
185  WCMFException::throwEx("getDisplayText() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
186  }
187  /**
188  * Get the summary text for the last step.
189  * @return The summary text
190  * @note subclasses must implement this method.
191  */
192  function getSummaryText()
193  {
194  WCMFException::throwEx("getSummaryText() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
195  }
196  /**
197  * Initialize the task e.g. store some configuration in the session.
198  * This method is called on start up.
199  * @note subclasses override this method to implement special application requirements.
200  */
201  function initializeTask() {}
202  /**
203  * Process one part of the task.
204  * @note subclasses must implement this method.
205  */
206  function processPart()
207  {
208  WCMFException::throwEx("processPart() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
209  }
210 }
211 ?>
LongTaskController is a controller that may be used as base class for tasks, that require a long exec...
throwEx($message, $file='', $line='')
Controller is the base class of all controllers. If a Controller has a view it is expected to reside ...
initialize(&$request, &$response)