From: <gem...@li...> - 2012-04-23 14:13:54
|
Revision: 632 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=632&view=rev Author: matijsdejong Date: 2012-04-23 14:13:47 +0000 (Mon, 23 Apr 2012) Log Message: ----------- After survey completion the user always returns to his/her own site, regardless of later url changes. Fixed UserLoader issue when the database does not exist Added documentation and utility functions to Html and Html_Attributes Modified Paths: -------------- trunk/library/classes/Gems/Default/AskAction.php trunk/library/classes/Gems/Tracker/Token.php trunk/library/classes/Gems/User/UserLoader.php trunk/library/classes/MUtil/Html/ArrayAttribute.php trunk/library/classes/MUtil/Html/AttributeAbstract.php trunk/library/classes/MUtil/Html/AttributeInterface.php trunk/library/classes/MUtil/Html/UrlArrayAttribute.php trunk/library/classes/MUtil/Html.php trunk/library/configs/db/patches.sql trunk/library/configs/db/tables/gems__tokens.200.sql Modified: trunk/library/classes/Gems/Default/AskAction.php =================================================================== --- trunk/library/classes/Gems/Default/AskAction.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/Gems/Default/AskAction.php 2012-04-23 14:13:47 UTC (rev 632) @@ -112,14 +112,15 @@ if ($tokenId = $this->_getParam(MUtil_Model::REQUEST_ID)) { $tracker = $this->loader->getTracker(); $tokenId = $tracker->filterToken($tokenId); + $token = $tracker->getToken($tokenId); - if ($token = $tracker->getToken($tokenId)) { + if ($token->exists) { /**************************** * Update open tokens first * ****************************/ $respId = $token->getRespondentId(); - $tracker->processCompletedTokens($respId, $respId); + $tracker->processCompletedTokens($respId, $token->getChangedBy()); // Display token when possible if ($this->html->snippet($this->forwardSnippets, 'token', $token)) { @@ -141,7 +142,7 @@ } } - $this->_forward('token'); + $this->_forward('index'); } /** @@ -168,31 +169,58 @@ } /** + * Common handler utility to initialize tokens from parameters + */ + protected function initToken() + { + $this->tracker = $this->loader->getTracker(); + $this->tokenId = $this->tracker->filterToken($this->_getParam(MUtil_Model::REQUEST_ID)); + $this->token = $this->tracker->getToken($this->tokenId); + } + + /** * The action where survey sources should return to after survey completion */ public function returnAction() { - $user = $this->loader->getCurrentUser(); + $tracker = $this->loader->getTracker(); - if ($user->isActive() && ($parameters = $user->getSurveyReturn())) { - $tracker = $this->loader->getTracker(); - $token = $tracker->getToken($tracker->filterToken($this->_getParam(MUtil_Model::REQUEST_ID))); + if ($tokenId = $this->_getParam(MUtil_Model::REQUEST_ID)) { + $tokenId = $tracker->filterToken($tokenId); + $token = $tracker->getToken($tokenId); - // Check for completed tokens - $this->loader->getTracker()->processCompletedTokens($token->getRespondentId(), $user->getUserId()); + if ($url = $token->getReturnUrl()) { + // Check for completed tokens + $this->loader->getTracker()->processCompletedTokens($token->getRespondentId(), $token->getChangedBy()); - if (! $parameters) { - // Default - $request = $this->getRequest(); - $parameters[$request->getControllerKey()] = 'respondent'; - $parameters[$request->getActionKey()] = 'show'; - $parameters[MUtil_Model::REQUEST_ID] = $token->getPatientNumber(); + // Redirect at once, might be another site url + header('Location: ' . $url); + exit(); } - $this->_reroute($parameters, true); - } else { - $this->_forward('forward'); + // Nor return? Check for old style user based return + $user = $this->loader->getCurrentUser(); + + if ($user->isActive() && ($parameters = $user->getSurveyReturn())) { + + // Check for completed tokens + $this->loader->getTracker()->processCompletedTokens($token->getRespondentId(), $user->getUserId()); + + if (! $parameters) { + // Default + $request = $this->getRequest(); + $parameters[$request->getControllerKey()] = 'respondent'; + $parameters[$request->getActionKey()] = 'show'; + $parameters[MUtil_Model::REQUEST_ID] = $token->getPatientNumber(); + } + + $this->_reroute($parameters, true); + return; + } } + + // In all other cases: the action that generates meaningfull warnings as is reachable for everyone + $this->_forward('forward'); } /** Modified: trunk/library/classes/Gems/Tracker/Token.php =================================================================== --- trunk/library/classes/Gems/Tracker/Token.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/Gems/Tracker/Token.php 2012-04-23 14:13:47 UTC (rev 632) @@ -272,7 +272,104 @@ return $this; } + /** + * Retrieve a certain $key from the local cache + * + * For speeding up things the token can hold a local cache, living as long as the + * token object exists in memory. Sources can use this to store reusable information. + * + * To reset the cache on an update, the source can use the cacheReset method or the + * setCache method to update the changed value. + * + * @param string $key The key used in the cache + * @param mixed $defaultValue The optional default value to use when it is not present + * @return mixed + */ + public function cacheGet($key, $defaultValue = null) { + if ($this->cacheHas($key)) { + return $this->_cache[$key]; + } else { + return $defaultValue; + } + } + + /** + * find out if a certain key is present in the cache + * + * @param string $key + * @return boolean + */ + public function cacheHas($key) { + return isset($this->_cache[$key]); + + } + + /** + * Reset the local cache for this token + * + * You can pass in an optional $key parameter to reset just that key, otherwise all + * the cache will be reset + * + * @param string|null $key The key to reset + */ + public function cacheReset($key = null) { + if (is_null($key)) { + $this->_cache = array(); + } else { + unset($this->_cache[$key]); + } + } + + /** + * Set a $key in the local cache + * + * @param string $key + * @param mixed $value + */ + public function cacheSet($key, $value) { + $this->_cache[$key] = $value; + } + + /** + * Returns the full url Gems should forward to after survey completion. + * + * This fix allows multiple sites with multiple url's to share a single + * installation. + * + * @return string + */ + protected function calculateReturnUrl() + { + $currentUri = $this->util->getCurrentURI(); + + /* + // Referrer would be powerful when someone is usng multiple windows, but + // the loop does not always provide a correct referrer. + $referrer = $_SERVER["HTTP_REFERER"]; + + // If a referrer was specified and that referral is from the current site, then use it + // as it is more dependable when the user has multiple windows open on the application. + if ($referrer && (0 == strncasecmp($referrer, $currentUri, strlen($currentUri)))) { + return $referrer; + // MUtil_Echo::track($referrer); + } // */ + + // Use the survey return if available. + $surveyReturn = $this->loader->getCurrentUser()->getSurveyReturn(); + if ($surveyReturn) { + // Do not show the base url as it is in $currentUri + $surveyReturn['NoBase'] = true; + + return $currentUri . MUtil_Html::urlString($surveyReturn); + // MUtil_Echo::track($currentUri . MUtil_Html::urlString($surveyReturn)); + } + + // Ultimate backup solution for reutrn + return $currentUri . '/ask/forward/' . MUtil_Model::REQUEST_ID . '/' . urlencode($this->getTokenId()); + } + + /** * Should be called after answering the request to allow the Target * to check if all required registry values have been set correctly. * @@ -455,7 +552,18 @@ } /** + * Returns the staff or respondent id of the person + * who last changed this token. * + * @return int + */ + public function getChangedBy() + { + return $this->_gemsData['gto_changed_by']; + } + + /** + * * @return MUtil_Date Completion time as a date or null */ public function getCompletionTime() @@ -734,6 +842,15 @@ return $this->_gemsData['gto_id_respondent_track']; } + /** + * The full return url for a redirect + * + * @return string + */ + public function getReturnUrl() + { + return $this->_gemsData['gto_return_url']; + } /** * @@ -889,7 +1006,7 @@ $values['gto_in_source'] = 1; } $values['gto_by'] = $userId; - // 1.5.4 $values['gto_return_url'] = $_SERVER["HTTP_REFERER"]; + $values['gto_return_url'] = $this->calculateReturnUrl(); $this->_updateToken($values, $userId); @@ -1189,62 +1306,4 @@ return $this->_updateToken($values, $userId); } - - /** - * Retrieve a certain $key from the local cache - * - * For speeding up things the token can hold a local cache, living as long as the - * token object exists in memory. Sources can use this to store reusable information. - * - * To reset the cache on an update, the source can use the cacheReset method or the - * setCache method to update the changed value. - * - * @param string $key The key used in the cache - * @param mixed $defaultValue The optional default value to use when it is not present - * @return mixed - */ - public function cacheGet($key, $defaultValue = null) { - if ($this->cacheHas($key)) { - return $this->_cache[$key]; - } else { - return $defaultValue; - } - } - - /** - * find out if a certain key is present in the cache - * - * @param string $key - * @return boolean - */ - public function cacheHas($key) { - return isset($this->_cache[$key]); - - } - - /** - * Reset the local cache for this token - * - * You can pass in an optional $key parameter to reset just that key, otherwise all - * the cache will be reset - * - * @param string|null $key The key to reset - */ - public function cacheReset($key = null) { - if (is_null($key)) { - $this->_cache = array(); - } else { - unset($this->_cache[$key]); - } - } - - /** - * Set a $key in the local cache - * - * @param string $key - * @param mixed $value - */ - public function cacheSet($key, $value) { - $this->_cache[$key] = $value; - } -} \ No newline at end of file +} Modified: trunk/library/classes/Gems/User/UserLoader.php =================================================================== --- trunk/library/classes/Gems/User/UserLoader.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/Gems/User/UserLoader.php 2012-04-23 14:13:47 UTC (rev 632) @@ -357,7 +357,12 @@ } if (! $urls) { - $data = $this->db->fetchPairs("SELECT gor_id_organization, gor_url_base FROM gems__organizations WHERE gor_active=1 AND gor_url_base IS NOT NULL"); + try { + $data = $this->db->fetchPairs("SELECT gor_id_organization, gor_url_base FROM gems__organizations WHERE gor_active=1 AND gor_url_base IS NOT NULL"); + } catch (Zend_Db_Exception $zde) { + // Table might not be filled + $data = array(); + } $urls = array(); foreach ($data as $orgId => $urlsBase) { foreach (explode(' ', $urlsBase) as $url) { Modified: trunk/library/classes/MUtil/Html/ArrayAttribute.php =================================================================== --- trunk/library/classes/MUtil/Html/ArrayAttribute.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/MUtil/Html/ArrayAttribute.php 2012-04-23 14:13:47 UTC (rev 632) @@ -1,41 +1,55 @@ <?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - /** - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 - * @package MUtil + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil * @subpackage Html + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ */ +/** + * Parent class for all array based attribute classes. + * + * Useable as is, using spaces as value separators by default. + * + * Parameter setting checks for the addition of special types, + * just as MUtil_Html_HtmlElement. + * + * @package MUtil + * @subpackage Html + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 + */ + class MUtil_Html_ArrayAttribute extends MUtil_Html_AttributeAbstract implements ArrayAccess, Countable, IteratorAggregate { @@ -44,7 +58,8 @@ protected $_specialTypes; private $_specialTypesDefault = array( - 'setView' => 'Zend_View', + 'setRequest' => 'Zend_Controller_Request_Abstract', + 'setView' => 'Zend_View', ); protected $_values; Modified: trunk/library/classes/MUtil/Html/AttributeAbstract.php =================================================================== --- trunk/library/classes/MUtil/Html/AttributeAbstract.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/MUtil/Html/AttributeAbstract.php 2012-04-23 14:13:47 UTC (rev 632) @@ -1,6 +1,5 @@ <?php - /** * Copyright (c) 2011, Erasmus MC * All rights reserved. @@ -48,35 +47,84 @@ */ abstract class MUtil_Html_AttributeAbstract implements MUtil_Html_AttributeInterface { + /** + * + * @var type + */ + public $name; + + /** + * + * @var Zend_Controller_Request_Abstract + */ + public $request; + + /** + * + * @var Zend_View_Abstract + */ public $view; - protected $_name; - + /** + * + * @param string $name The name of the attribute + * @param mixed $value + */ public function __construct($name, $value = null) { - $this->_name = $name; + $this->name = $name; if ($value) { $this->set($value); } } + /** + * Returns an unescape string version of the attribute + * + * Output escaping is done elsewhere, e.g. in Zend_View_Helper_HtmlElement->_htmlAttribs() + * + * If a subclass needs the view for the right output and the view might not be set + * it must overrule __toString(). + * + * @return string + */ public function __toString() { - // Output escaping is done in Zend_View_Helper_HtmlElement->_htmlAttribs() - // - // If the attribute needs the view to get the right data it must overrule __toString(). return $this->get(); } // public function add($value); // public function get(); + /** + * Returns the attribute name + * + * @return string + */ public function getAttributeName() { - return $this->_name; + return $this->name; } + /** + * + * @return Zend_Controller_Request_Abstract + */ + public function getRequest() + { + if (! $this->request) { + $front = Zend_Controller_Front::getInstance(); + $this->request = $front->getRequest(); + } + + return $this->request; + } + + /** + * + * @return Zend_View_Abstract + */ public function getView() { if (! $this->view) { @@ -88,6 +136,14 @@ return $this->view; } + /** + * Renders the element into a html string + * + * The $view is used to correctly encode and escape the output + * + * @param Zend_View_Abstract $view + * @return string Correctly encoded and escaped html output + */ public function render(Zend_View_Abstract $view) { $this->setView($view); @@ -101,6 +157,21 @@ // public function set($value); + /** + * + * @param Zend_Controller_Request_Abstract $request + * @return MUtil_Html_AttributeAbstract (continuation pattern) + */ + public function setRequest(Zend_Controller_Request_Abstract $request) + { + $this->request = $request; + return $this; + } + + /** + * + * @param Zend_View_Abstract $view + */ public function setView(Zend_View_Abstract $view) { $this->view = $view; Modified: trunk/library/classes/MUtil/Html/AttributeInterface.php =================================================================== --- trunk/library/classes/MUtil/Html/AttributeInterface.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/MUtil/Html/AttributeInterface.php 2012-04-23 14:13:47 UTC (rev 632) @@ -1,62 +1,78 @@ <?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - /** - * - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 - * @package MUtil + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil * @subpackage Html + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ */ /** * Standard interface for attributes in this package. - * + * * The interface ensure the ability to not only get and set the * value, but also the attribute name and the ability to add to * the content in a manner as defined by the attribute itself. - * + * * E.g. adding to a class attribute usually involves seperating * the new addition with a space. - * - * @author Matijs de Jong - * @package MUtil + * + * @package MUtil * @subpackage Html + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 */ interface MUtil_Html_AttributeInterface extends MUtil_Html_HtmlInterface { + /** + * Returns an unescape string version of the attribute + * + * Output escaping is done elsewhere, e.g. in Zend_View_Helper_HtmlElement->_htmlAttribs() + * + * @return string + */ public function __toString(); + public function add($value); public function get(); + + /** + * Returns the attribute name + * + * @return string + */ public function getAttributeName(); + // inherited: public function render(Zend_View_Abstract $view); + public function set($value); } \ No newline at end of file Modified: trunk/library/classes/MUtil/Html/UrlArrayAttribute.php =================================================================== --- trunk/library/classes/MUtil/Html/UrlArrayAttribute.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/MUtil/Html/UrlArrayAttribute.php 2012-04-23 14:13:47 UTC (rev 632) @@ -1,6 +1,5 @@ <?php - /** * Copyright (c) 2011, Erasmus MC * All rights reserved. @@ -26,23 +25,59 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package MUtil + * @subpackage Html + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ */ /** - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 - * @package MUtil + * An array attribute that forms url's using Zend framework routing + * + * @package MUtil * @subpackage Html + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 */ - class MUtil_Html_UrlArrayAttribute extends MUtil_Html_ArrayAttribute { - protected $_request; - protected $_routeReset; + /** + * + * @var boolean + */ + protected $_routeReset = false; + /** + * Seperator used to separate multiple items + * + * @var string + */ protected $_separator = '&'; + protected $_specialTypes = array('setRouter' => 'Zend_Controller_Router_Route'); + + /** + * + * @var Zend_Controller_Router_Route + */ + public $router; + + /** + * Helper function thats fills a parameter from the request if it was not + * already in the options array. + * + * This function ensures that e.g. the current controller is used instead + * of the default 'index' controller. + * + * @param Zend_Controller_Request_Abstract $request + * @param string $name + * @param array $options + */ private static function _rerouteUrlOption(Zend_Controller_Request_Abstract $request, $name, &$options) { if (! array_key_exists($name, $options)) { @@ -85,7 +120,8 @@ // Make sure controllor, action, module are specified $url_parameters = self::rerouteUrl($this->getRequest(), $url_parameters); - return $this->getView()->url($url_parameters, null, $this->getRouteReset(), false); + $router = $this->getRouter(); + return $router->assemble($url_parameters, null, $this->getRouteReset(), false); } return null; @@ -96,25 +132,40 @@ return $key . '=' . $value; } - public function getRequest() + /** + * + * @return Zend_Controller_Router_Route + */ + public function getRouter() { - if (! $this->_request) { + if (! $this->router) { $front = Zend_Controller_Front::getInstance(); - $this->_request = $front->getRequest(); + $this->router = $front->getRouter(); } - return $this->_request; + return $this->router; } + /** + * Whether or not to set route defaults with the paramter values + * + * @return type + */ public function getRouteReset() { return $this->_routeReset; } + /** + * Is this Url an Zend Framework Mvc url or a string with parameters. + * + * @return boolean + */ public function isMvcUrl() { foreach ($this->getArray() as $key => $value) { if (is_numeric($key)) { + // Contains standalone string => not Zend return false; } } @@ -122,31 +173,58 @@ return true; } + /** + * Set the module, controller and action of an url parameter array to the current + * module, controller and action, except when one of these items has already been + * specified in the array. + * + * @param Zend_Controller_Request_Abstract $request + * @param array $options An array of parameters (optionally including e.g. controller name) for the new url + * @param boolean $addRouteReset Deprecated: add the 'RouteReset' parameter that is used by objects of this type to set RouteReset + * @return array Url array with adapted utl's + */ public static function rerouteUrl(Zend_Controller_Request_Abstract $request, $options, $addRouteReset = false) { - self::_rerouteUrlOption($request, 'module', $options); - self::_rerouteUrlOption($request, 'controller', $options); - self::_rerouteUrlOption($request, 'action', $options); + self::_rerouteUrlOption($request, $request->getModuleKey(), $options); + self::_rerouteUrlOption($request, $request->getControllerKey(), $options); + self::_rerouteUrlOption($request, $request->getActionKey(), $options); if ($addRouteReset) { + // Use of this paramter is deprecated $options['RouteReset'] = true; } return $options; } - public function setRequest(Zend_Controller_Request_Abstract $request) + /** + * + * @param Zend_Controller_Router_Route $router + * @return MUtil_Html_UrlArrayAttribute (continuation pattern) + */ + public function setRouter(Zend_Controller_Router_Route $router) { - $this->_request = $request; + $this->router = $router; return $this; } + /** + * Whether or not to set route defaults with the paramter values + * + * @param boolean $routeReset + * @return MUtil_Html_UrlArrayAttribute (continuation pattern) + */ public function setRouteReset($routeReset = true) { $this->_routeReset = $routeReset; return $this; } + /** + * @deprecated + * @param string $label + * @return Zend_Navigation_Page_Mvc + */ public function toPage($label) { if ($this->isMvcUrl()) { @@ -165,4 +243,62 @@ return new Zend_Navigation_Page_Uri($options); } } + + /** + * Returns relative url string using the current module, controller and action when + * none where specified. + * + * This is url is encoded for url usage, but not for use as attribute values, + * i.e. this helper function is used for generating url's for internal use. + * + * @param array $options Array of parameter values + * @param Zend_Controller_Request_Abstract $request + * @param Zend_Controller_Router_Route $router + * @return string + */ + public static function toUrlString(array $options, Zend_Controller_Request_Abstract $request = null, Zend_Controller_Router_Route $router = null) + { + $base = ''; + $encode = true; + $nobase = false; + $reset = false; + + if (array_key_exists('Encode', $options)) { + $encode = $options['Encode']; + unset($options['Encode']); + } + if (array_key_exists('NoBase', $options)) { + $nobase = $options['NoBase']; + unset($options['NoBase']); + } + if (array_key_exists('RouteReset', $options)) { + $reset = $options['RouteReset']; + unset($options['RouteReset']); + } + + if ($nobase || (null === $request) || (null === $router)) { + $front = Zend_Controller_Front::getInstance(); + + if ($nobase) { + $base = rtrim($front->getBaseUrl(), '/'); + } + + if (null === $request) { + $request = $front->getRequest(); + } + if (null === $router) { + $router = $front->getRouter(); + } + } + + $options = self::rerouteUrl($request, $options); + $url = $router->assemble($options, null, $reset, $encode); + + // Remove the base url that was specified + if ($nobase && (0 === strncmp($url, $base . '/', strlen($base) + 1))) { + return substr($url, strlen($base)); + } + + return $url; + } } \ No newline at end of file Modified: trunk/library/classes/MUtil/Html.php =================================================================== --- trunk/library/classes/MUtil/Html.php 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/classes/MUtil/Html.php 2012-04-23 14:13:47 UTC (rev 632) @@ -79,6 +79,12 @@ */ public static $verbose = false; + /** + * @deprecated + * @param Zend_Navigation_Container $menu + * @param string $label + * @param array $arg_array + */ public static function addUrl2Page(Zend_Navigation_Container $menu, $label, $arg_array = null) { $args = array_slice(func_get_args(), 2); @@ -311,6 +317,7 @@ /** * Returns a href attribute * + * @deprecated * @param mixed $arg_array MUtil_Args::ra arguements * @return MUtil_Html_HrefArrayAttribute */ @@ -319,4 +326,21 @@ $args = func_get_args(); return new MUtil_Html_HrefArrayAttribute($args); } + + /** + * Returns relative url string using the current module, controller and action when + * none where specified. + * + * This is url is encoded for url usage, but not for use as attribute values, + * i.e. this helper function is used for generating url's for internal use. + * + * @param array $options Array of parameter values + * @param Zend_Controller_Request_Abstract $request + * @param Zend_Controller_Router_Route $router + * @return string + */ + public static function urlString(array $options, Zend_Controller_Request_Abstract $request = null, Zend_Controller_Router_Route $router = null) + { + return MUtil_Html_UrlArrayAttribute::toUrlString($options, $request, $router); + } } Modified: trunk/library/configs/db/patches.sql =================================================================== --- trunk/library/configs/db/patches.sql 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/configs/db/patches.sql 2012-04-23 14:13:47 UTC (rev 632) @@ -396,4 +396,4 @@ -- GEMS VERSION: 47 -- PATCH: Add return url to tokens --- ALTER TABLE gems__tokens ADD gto_return_url varchar(250) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null default null AFTER gto_reception_code; +ALTER TABLE gems__tokens ADD gto_return_url varchar(250) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null default null AFTER gto_reception_code; Modified: trunk/library/configs/db/tables/gems__tokens.200.sql =================================================================== --- trunk/library/configs/db/tables/gems__tokens.200.sql 2012-04-23 12:08:39 UTC (rev 631) +++ trunk/library/configs/db/tables/gems__tokens.200.sql 2012-04-23 14:13:47 UTC (rev 632) @@ -41,7 +41,7 @@ gto_reception_code varchar(20) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' default 'OK' not null references gems__reception_codes (grc_id_reception_code), - -- gto_return_url varchar(250) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null default null, + gto_return_url varchar(250) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null default null, gto_changed timestamp not null default current_timestamp on update current_timestamp, gto_changed_by bigint unsigned not null, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |