Skip to content

Commit

Permalink
fixed search
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlaefer committed Feb 22, 2012
1 parent 5a5bb8a commit a93647e
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 199 deletions.
62 changes: 34 additions & 28 deletions app/Controller/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class EntriesController extends AppController {
public $components = array(
'CacheTree',
'Flattr',
/* cake2
'Search.Prg',
*/
);
/**
* Setup for Search Plugin
Expand Down Expand Up @@ -456,46 +454,45 @@ public function search() {
}
$this->set('start_year', date('Y', $start_date));

// calculate current month and year
if ( empty($this->passedArgs['month']) ) {
//* calculate current month and year
if ( empty($this->request->data['Entry']['month']) && empty($searchStartMonth)) {
// start in last month
// $start_date = mktime(0,0,0,((int)date('m')-1), 28, (int)date('Y'));
$this->passedArgs['month'] = date('n', $start_date);
$this->passedArgs['year'] = date('Y', $start_date);
// $start_date = mktime(0,0,0,((int)date('m')-1), 28, (int)date('Y'));
$searchStartMonth = date('n', $start_date);
$searchStartYear = date('Y', $start_date);
}


// extract search_term for simple search
$search_term = '';
$searchTerm = '';
if ( isset($this->request->data['Entry']['search_term']) ) {
$search_term = $this->request->data['Entry']['search_term'];
$searchTerm = $this->request->data['Entry']['search_term'];
} elseif ( isset($this->request->params['named']['search_term']) ) {
$search_term = $this->request->params['named']['search_term'];
} elseif ( isset($this->request->params['url']['search_term']) ) {
$searchTerm = $this->request->params['named']['search_term'];
} elseif ( isset($this->request['url']['search_term']) ) {
// search_term is send via get parameter
$search_term = $this->request->params['url']['search_term'];
$searchTerm = $this->request['url']['search_term'];
}
$this->set('search_term', $search_term);
$this->set('search_term', $searchTerm);

if ( isset($this->passedArgs['adv']) ) {
$this->request->params['data']['Entry']['adv'] = 1;
}

if ( !isset($this->request->params['data']['Entry']['adv']) ) {
if ( !isset($this->request->data['Entry']['adv']) && !isset($this->request->params['named']['adv']) ) {
// Simple Search
if ( $search_term ) {
if ( $searchTerm ) {
Router::connectNamed(array( 'search_term' ));

$this->passedArgs['search_term'] = $search_term;
$this->passedArgs['search_term'] = $searchTerm;
/* stupid apache rewrite urlencode bullshit */
// $this->passedArgs['search_term'] = urlencode(urlencode($search_term));

$where = array( );
if ( $search_term ) {
if ( $searchTerm ) {

$this->paginate = array(
'fields' => "*, (MATCH (Entry.subject) AGAINST ('$search_term' IN BOOLEAN MODE)*100) + (MATCH (Entry.text) AGAINST ('$search_term' IN BOOLEAN MODE)*10) + MATCH (Entry.name) AGAINST ('$search_term' IN BOOLEAN MODE) AS rating",
'conditions' => "MATCH (Entry.subject, Entry.text, Entry.name) AGAINST ('$search_term' IN BOOLEAN MODE)",
'fields' => "*, (MATCH (Entry.subject) AGAINST ('$searchTerm' IN BOOLEAN MODE)*100) + (MATCH (Entry.text) AGAINST ('$searchTerm' IN BOOLEAN MODE)*10) + MATCH (Entry.name) AGAINST ('$searchTerm' IN BOOLEAN MODE) AS rating",
'conditions' => "MATCH (Entry.subject, Entry.text, Entry.name) AGAINST ('$searchTerm' IN BOOLEAN MODE)",
'order' => 'rating DESC, `Entry`.`time` DESC',
/*
'conditions' => array(
Expand All @@ -509,21 +506,30 @@ public function search() {
$found_entries = $this->paginate('Entry');

$this->set('FoundEntries', $found_entries);
$this->request->data['Entry']['search']['term'] = $search_term;
$this->request->data['Entry']['search']['term'] = $searchTerm;
}
}
} else {
// Advanced Search
$this->Prg->commonProcess();
$this->paginate['conditions'] = $this->Entry->parseCriteria($this->passedArgs);
$this->paginate['conditions']['time >'] = date('Y-m-d H:i:s',
mktime(0, 0, 0, (int) $this->passedArgs['month'], 1,
(int) $this->passedArgs['year']));
if (isset($this->request->params['named']['month'])):
$searchStartMonth = (int)$this->request->params['named']['month'];
$searchStartYear = (int)$this->request->params['named']['year'];
endif;

$this->paginate['order'] = array('`Entry`.`time`' => 'DESC');
$this->paginate['limit'] = 25;
$this->Prg->commonProcess();
$paginateSettings = array();
$paginateSettings['conditions'] = $this->Entry->parseCriteria(
$this->request->params['named']);
$paginateSettings['conditions']['time >'] = date(
'Y-m-d H:i:s', mktime( 0, 0, 0, $searchStartMonth, 1, $searchStartYear ));
$paginateSettings['order'] = array('`Entry`.`time`' => 'DESC');
$paginateSettings['limit'] = 25;
$this->paginate = $paginateSettings;
$this->set('FoundEntries', $this->paginate());
}

$this->request->data['Entry']['month'] = $searchStartMonth;
$this->request->data['Entry']['year'] = $searchStartYear;
}

public function preview() {
Expand Down
2 changes: 0 additions & 2 deletions app/Model/entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ class Entry extends AppModel {
var $actsAs = array(
'Containable',
'CacheTree',
/* cake2
'Search.Searchable',
*/
);

// fields for search plugin
Expand Down
62 changes: 43 additions & 19 deletions ...ins/search/controllers/components/prg.php → ...rch/Controller/Component/PrgComponent.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* @package plugins.search
* @subpackage plugins.search.controllers.components
*/
class PrgComponent extends Object {
App::uses('Component', 'Controller');

class PrgComponent extends Component {

/**
* Actions used to fetch the post data
Expand All @@ -39,12 +41,22 @@ class PrgComponent extends Object {
public $encode = false;

/**
* Intialize Callback
* Default options
*
* @var array
*/
protected $_defaults = array(
'commonProcess' => array()
);

/**
* Constructor
*
* @param object Controller object
*/
public function initialize(&$controller) {
$this->controller = $controller;
public function __construct(ComponentCollection $collection, $settings) {
$this->controller = $collection->getController();
$this->_defaults = Set::merge($this->_defaults, $settings);
}

/**
Expand All @@ -69,11 +81,13 @@ public function initialize(&$controller) {
public function presetForm($model) {
$data = array($model => array());
$args = $this->controller->passedArgs;

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!
$this->controller->passedArgs[$field['field']] = $args[$field['field']] = pack('H*', $args[$field['field']]);
if (isset($args[$field['field']])) {
$val = $args[$field['field']];
$this->controller->passedArgs[$field['field']] = $args[$field['field']] = base64_decode(str_pad(strtr($val, '-_', '+/'), strlen($val) % 4, '=', STR_PAD_RIGHT));
}
}

if ($field['type'] == 'lookup') {
Expand Down Expand Up @@ -113,16 +127,16 @@ public function presetForm($model) {
public function serializeParams(&$data) {
foreach ($this->controller->presetVars as $field) {
if ($field['type'] == 'checkbox') {
if (is_array($data[$field['field']])) {
$values = join('|', $data[$field['field']]);
if (array_key_exists($field['field'], $data)) {
$values = join('|', (array)$data[$field['field']]);
} else {
$values = '';
}
$data[$field['field']] = $values;
}

if ($this->encode == true || isset($field['encode']) && $field['encode'] == true) {
$data[$field['field']] = bin2hex($data[$field['field']]);
if (($this->encode == true || isset($field['encode']) && $field['encode'] == true) && isset($data[$field['field']])) {
$data[$field['field']] = rtrim(strtr(base64_encode($data[$field['field']]), '+/', '-_'), '=');
}
}
return $data;
Expand Down Expand Up @@ -155,7 +169,7 @@ public function connectNamed($data = null, $exclude = array()) {
* 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
* @return array
Expand All @@ -180,19 +194,22 @@ public function exclude($array, $exclude) {
* - Issuing redirect(), and connecting named parameters before redirect
* - Setting named parameter form data to view
*
* @param string $modelName Name of the model class being used for the prg form
* @param string $modelName - Name of the model class being used for the prg form
* @param array $options Optional parameters:
* - string form 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
* - 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
* - array allowedParams - An array of additional top level route params that should be included in the params processed
* @return void
*/
public function commonProcess($modelName = null, $options = array()) {
$defaults = array(
'form' => null,
'formName' => null,
'keepPassed' => true,
'action' => null,
'modelMethod' => 'validateSearch');
'modelMethod' => 'validateSearch',
'allowedParams' => array());
$defaults = Set::merge($defaults, $this->_defaults['commonProcess']);
extract(Set::merge($defaults, $options));

if (empty($modelName)) {
Expand All @@ -215,7 +232,7 @@ public function commonProcess($modelName = null, $options = array()) {
}

if ($valid) {
$passed = $this->controller->params['pass'];
$passed = $this->controller->request->params['pass'];
$params = $this->controller->data[$modelName];
$params = $this->exclude($params, array());

Expand All @@ -226,7 +243,14 @@ public function commonProcess($modelName = null, $options = array()) {
$this->serializeParams($params);
$this->connectNamed($params, array());
$params['action'] = $action;
$params = array_merge($this->controller->params['named'], $params);
$params = array_merge($this->controller->request->params['named'], $params);

foreach ($allowedParams as $key) {
if (isset($this->controller->request->params[$key])) {
$params[$key] = $this->controller->request->params[$key];
}
}

$this->controller->redirect($params);
} else {
$this->controller->Session->setFlash(__d('search', 'Please correct the errors below.'));
Expand Down
29 changes: 29 additions & 0 deletions app/Plugin/Search/Locale/deu/LC_MESSAGES/search.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# LANGUAGE translation of the CakePHP Categories plugin
#
# Copyright 2010, Cake Development Corporation (http://cakedc.com)
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
#
# @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
#
msgid ""
msgstr ""
"Project-Id-Version: CakePHP Search Plugin\n"
"POT-Creation-Date: 2010-09-15 15:33+0200\n"
"PO-Revision-Date: 2010-09-23 21:53+0100\n"
"Last-Translator: Florian Krämer <florian.kraemer@cakedc.com>\n"
"Language-Team: Cake Development Corporation <info@cakedc.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n!=1);\n"
"X-Poedit-Language: GERMAN\n"
"X-Poedit-Country: GERMANY\n"
"X-Poedit-SourceCharset: utf-8\n"

#: /controllers/components/prg.php:245
msgid "Please correct the errors below."
msgstr "Bitte korrigieren Sie die unten stehenden Fehler."

28 changes: 28 additions & 0 deletions app/Plugin/Search/Locale/por/LC_MESSAGES/search.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# LANGUAGE translation of the CakePHP Categories plugin
#
# Copyright 2010, Cake Development Corporation (http://cakedc.com)
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
#
# @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
#
msgid ""
msgstr ""
"Project-Id-Version: CakePHP Search Plugin\n"
"POT-Creation-Date: 2010-09-15 15:33+0200\n"
"PO-Revision-Date: 2010-09-23 18:59-0300\n"
"Last-Translator: Renan Gonçalves <renan.saddam@gmail.com>\n"
"Language-Team: CakeDC <renan.saddam@gmail.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2;plural=n>1;\n"
"X-Poedit-Language: Portuguese\n"
"X-Poedit-Country: BRAZIL\n"

#: /controllers/components/prg.php:245
msgid "Please correct the errors below."
msgstr "Por favor, corrija os erros abaixo."

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
# @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
#
#, fuzzy
msgid ""
msgstr ""
#
"Project-Id-Version: PROJECT VERSION\n"
"POT-Creation-Date: 2010-09-15 15:33+0200\n"
"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
Expand Down
27 changes: 27 additions & 0 deletions app/Plugin/Search/Locale/spa/LC_MESSAGES/search.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
msgid ""
msgstr ""
"Project-Id-Version: Search CakePHP plugin\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: José Lorenzo Rodríguez <jose.lorenzo@cakedc.com>\n"
"Language-Team: CakeDC <contact@cakedc.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-Language: Spanish\n"

# LANGUAGE translation of the CakePHP Categories plugin
#
# Copyright 2010, Cake Development Corporation (http://cakedc.com)
#
# Licensed under The MIT License
# Redistributions of files must retain the above copyright notice.
#
# @copyright Copyright 2010, Cake Development Corporation (http://cakedc.com)
# @license MIT License (http://www.opensource.org/licenses/mit-license.php)
#
#
#: /controllers/components/prg.php:245
msgid "Please correct the errors below."
msgstr "Por favor corrija los errores que se indican"

Loading

0 comments on commit a93647e

Please sign in to comment.