Permalink
Browse files

Merge pull request #39 from ADmad/2.0-querystring

2.0 querystring
  • Loading branch information...
2 parents ef6d50d + eb4bc3f commit f34bfcd8d5d8b98737bafedfede7bc66aaf8a1d9 @lorenzo lorenzo committed Mar 28, 2012
Showing with 87 additions and 43 deletions.
  1. +59 −33 Controller/Component/PrgComponent.php
  2. +28 −10 Test/Case/Controller/Component/PrgComponentTest.php
@@ -16,13 +16,14 @@
* @subpackage plugins.search.controllers.components
*/
App::uses('Component', 'Controller');
+App::uses('Set', 'Utility');
class PrgComponent extends Component {
/**
* Actions used to fetch the post data
*
- * Maps the action that takes the post data and processes it by using this
+ * Maps the action that takes the post data and processes it by using this
* component and maps it to another action that is accessed by a redirect which
* has the post data attached as get data now
*
@@ -46,7 +47,19 @@ class PrgComponent extends Component {
* @var array
*/
protected $_defaults = array(
- 'commonProcess' => array()
+ 'commonProcess' => array(
+ 'formName' => null,
+ 'keepPassed' => true,
+ 'action' => null,
+ 'modelMethod' => 'validateSearch',
+ 'allowedParams' => array(),
+ 'paramType' => 'named',
+ 'filterEmpty' => false
+ ),
+ 'presetForm' => array(
+ 'model' => null,
+ 'paramType' => 'named'
+ )
);
/**
@@ -63,7 +76,7 @@ public function __construct(ComponentCollection $collection, $settings) {
* Poplulates controller->data with allowed values from the named/passed get params
*
* Fields in $controller::$presetVars that have a type of 'lookup' the foreignKey value will be inserted
- *
+ *
* 1) 'lookup'
* Is used for autocomplete selectors
* For autocomplete we have hidden field with value and autocomplete text box
@@ -76,11 +89,20 @@ public function __construct(ComponentCollection $collection, $settings) {
* 1 use field, model, formField, and modelField
* 2, 3 need only field parameter
*
- * @param array
+ * @param array $options
*/
- public function presetForm($model) {
+ public function presetForm($options) {
+ if (!is_array($options)) {
+ $options = array('model' => $options);
+ }
+ extract(Set::merge($this->_defaults['presetForm'], $options));
+
$data = array($model => array());
- $args = $this->controller->passedArgs;
+ if ($paramType == 'named') {
+ $args = $this->controller->passedArgs;
+ } else {
+ $args = $this->controller->request->query;
+ }
foreach ($this->controller->presetVars as $field) {
if ($this->encode == true || isset($field['encode']) && $field['encode'] == true) {
// Its important to set it also back to the controllers passed args!
@@ -100,7 +122,7 @@ public function presetForm($model) {
$data[$model][$field['formField']] = $result[$searchModel][$field['modelField']];
}
}
-
+
if ($field['type'] == 'checkbox') {
if (isset($args[$field['field']])) {
$values = split('\|', $args[$field['field']]);
@@ -121,7 +143,7 @@ public function presetForm($model) {
/**
* Restores form params for checkboxs and other url encoded params
- *
+ *
* @param array
*/
public function serializeParams(&$data) {
@@ -166,9 +188,9 @@ public function connectNamed($data = null, $exclude = array()) {
}
/**
- * Exclude
- *
- * Removes key/values from $array based on $exclude
+ * Exclude
+ *
+ * Removes key/values from $array based on $exclude
*
* @param array Array of data to be filtered
* @param array Array of keys to exclude from other $array
@@ -186,7 +208,7 @@ public function exclude($array, $exclude) {
/**
* Common search method
- *
+ *
* Handles processes common to all PRG forms
*
* - Handles validation of post data
@@ -198,19 +220,13 @@ public function exclude($array, $exclude) {
* @param array $options Optional parameters:
* - string formName - name of the form involved in the prg
* - string action - The action to redirect to. Defaults to the current action
- * - mixed modelMethod - If not false a string that is the model method that will be used to process the data
+ * - mixed modelMethod - If not false a string that is the model method that will be used to process the data
* - array allowedParams - An array of additional top level route params that should be included in the params processed
+ * - string paramType - 'named' if you want to used named params or 'querystring' is you want to use query string
* @return void
*/
public function commonProcess($modelName = null, $options = array()) {
- $defaults = array(
- 'formName' => null,
- 'keepPassed' => true,
- 'action' => null,
- 'modelMethod' => 'validateSearch',
- 'allowedParams' => array());
- $defaults = Set::merge($defaults, $this->_defaults['commonProcess']);
- extract(Set::merge($defaults, $options));
+ extract(Set::merge($this->_defaults['commonProcess'], $options));
if (empty($modelName)) {
$modelName = $this->controller->modelClass;
@@ -232,18 +248,28 @@ public function commonProcess($modelName = null, $options = array()) {
}
if ($valid) {
- $passed = $this->controller->request->params['pass'];
- $params = $this->controller->data[$modelName];
- $params = $this->exclude($params, array());
-
+ $params = $this->controller->request->params['named'];
if ($keepPassed) {
- $params = array_merge($passed, $params);
+ $params = array_merge($this->controller->request->params['pass'], $params);
+ }
+
+ $searchParams = $this->controller->data[$modelName];
+ $searchParams = $this->exclude($searchParams, array());
+ if ($filterEmpty) {
+ $searchParams = Set::filter($searchParams);
+ }
+
+ $this->serializeParams($searchParams);
+
+ if ($paramType == 'named') {
+ $params = array_merge($params, $searchParams);
+ $this->connectNamed($params, array());
+ } else {
+ $this->connectNamed($params, array());
+ $params['?'] = array_merge($this->controller->request->query, $searchParams);
}
- $this->serializeParams($params);
- $this->connectNamed($params, array());
$params['action'] = $action;
- $params = array_merge($this->controller->request->params['named'], $params);
foreach ($allowedParams as $key) {
if (isset($this->controller->request->params[$key])) {
@@ -255,11 +281,11 @@ public function commonProcess($modelName = null, $options = array()) {
} else {
$this->controller->Session->setFlash(__d('search', 'Please correct the errors below.'));
}
- }
-
- if (empty($this->controller->data) && !empty($this->controller->passedArgs)) {
+ } elseif (($paramType == 'named' && !empty($this->controller->passedArgs)) ||
+ ($paramType == 'querystring' && !empty($this->controller->request->query))
+ ) {
$this->connectNamed($this->controller->passedArgs, array());
- $this->presetForm($formName);
+ $this->presetForm(array('model' => $formName, 'paramType' => $paramType));
}
}
}
@@ -81,9 +81,9 @@ public function beforeFilter() {
/**
* Overwrite redirect
*
- * @param string $url
- * @param string $status
- * @param string $exit
+ * @param string $url
+ * @param string $status
+ * @param string $exit
* @return void
*/
public function redirect($url, $status = NULL, $exit = true) {
@@ -134,21 +134,22 @@ class PrgComponentTest extends CakeTestCase {
*
* @return void
*/
- function startTest() {
+ public function startTest() {
$this->Controller = new PostsTestController();
$this->Controller->constructClasses();
$this->Controller->request->params = array(
'named' => array(),
'pass' => array(),
- 'url' => array());
+ 'url' => array());
+ $this->Controller->request->query = array();
}
/**
* endTest
*
* @return void
*/
- function endTest() {
+ public function endTest() {
unset($this->Controller);
ClassRegistry::flush();
}
@@ -178,6 +179,12 @@ public function testOptions() {
'title' => 'test',
'action' => 'search',
'lang' => 'en'));
+
+ $this->Controller->presetVars = array(
+ array('field' => 'title', 'type' => 'value'));
+ $this->Controller->Prg->commonProcess('Post', array('paramType' => 'querystring'));
+ $expected = array('action' => 'search', '?' => array('title' => 'test'));
+ $this->assertEqual($expected, $this->Controller->redirectUrl);
}
/**
@@ -216,6 +223,17 @@ public function testPresetForm() {
'lookup' => 1,
'lookup_input' => 'First Post'));
$this->assertEqual($this->Controller->data, $expected);
+
+ $this->Controller->data = array();
+ $this->Controller->passedArgs = array();
+ $this->Controller->request->query = array(
+ 'title' => 'test',
+ 'checkbox' => 'test|test2|test3',
+ 'lookup' => '1');
+ $this->Controller->beforeFilter();
+
+ $this->Controller->Prg->presetForm(array('model' => 'Post', 'paramType' => 'querystring'));
+ $this->assertEquals($expected, $this->Controller->data);
}
/**
@@ -312,7 +330,7 @@ public function testCommonProcess() {
$this->assertEqual($this->Controller->redirectUrl, array(
'title' => 'test',
'action' => 'search'));
-
+
$this->Controller->Prg->commonProcess(null, array(
'modelMethod' => false));
$this->assertEqual($this->Controller->redirectUrl, array(
@@ -369,7 +387,7 @@ public function testCommonProcessGet() {
$this->Controller->request->params['named'] = array('title' => 'test');
$this->Controller->passedArgs = array_merge($this->Controller->request->params['named'], $this->Controller->request->params['pass']);
$this->Controller->Prg->commonProcess('Post');
- $this->assertEqual($this->Controller->data, array('Post' => array('title' => 'test')));
+ $this->assertEqual($this->Controller->data, array('Post' => array('title' => 'test')));
}
/**
@@ -393,9 +411,9 @@ public function testSerializeParamsWithEncoding() {
$test = array('title' => 'ef?');
$result = $this->Controller->Prg->serializeParams($test);
$this->assertEqual($result['title'] , $this->_urlEncode('ef?'));
-
+
}
-
+
protected function _urlEncode($str) {
return rtrim(strtr(base64_encode($str), '+/', '-_'), '=');
}

0 comments on commit f34bfcd

Please sign in to comment.