From: Tim Otten Date: Fri, 30 May 2014 03:25:50 +0000 (-0700) Subject: CRM-14765 - FullText - DRY; thinner constructor X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ca4269f364393319a60d8078492cb5fa1ff29f89;p=civicrm-core.git CRM-14765 - FullText - DRY; thinner constructor --- diff --git a/CRM/Contact/Form/Search/Custom/FullText.php b/CRM/Contact/Form/Search/Custom/FullText.php index 59e293b3e5..3d6ed4aaff 100644 --- a/CRM/Contact/Form/Search/Custom/FullText.php +++ b/CRM/Contact/Form/Search/Custom/FullText.php @@ -67,42 +67,10 @@ class CRM_Contact_Form_Search_Custom_FullText implements CRM_Contact_Form_Search * @param $formValues */ function __construct(&$formValues) { - $this->_text = CRM_Utils_Array::value('text', $formValues); - $this->_table = CRM_Utils_Array::value('table', $formValues); - - if (!$this->_text) { - $this->_text = CRM_Utils_Request::retrieve('text', 'String', CRM_Core_DAO::$_nullObject); - if ($this->_text) { - $this->_text = trim($this->_text); - $formValues['text'] = $this->_text; - } - } - - if (!$this->_table) { - $this->_table = CRM_Utils_Request::retrieve('table', 'String', CRM_Core_DAO::$_nullObject); - if ($this->_table) { - $formValues['table'] = $this->_table; - } - } - - // fix text to include wild card characters at beginning and end - if ($this->_text) { - if (is_numeric($this->_text)) { - $this->_textID = $this->_text; - } - - $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; - $this->_text = $strtolower(CRM_Core_DAO::escapeString($this->_text)); - if (strpos($this->_text, '%') === FALSE) { - $this->_text = "'%{$this->_text}%'"; - } - else { - $this->_text = "'{$this->_text}'"; - } - } - else { - $this->_text = "'%'"; - } + $formValues['table'] = $this->getFieldValue($formValues, 'table', 'String'); + $this->_table = $formValues['table']; + $formValues['text'] = trim($this->getFieldValue($formValues, 'text', 'String', '')); + $this->_text = $this->convertToSqlWildcard($formValues['text']); if (!$this->_table) { $this->_limitClause = " LIMIT {$this->_limitNumberPlus1}"; @@ -124,6 +92,52 @@ class CRM_Contact_Form_Search_Custom_FullText implements CRM_Contact_Form_Search $this->_formValues = $formValues; } + /** + * Get a value from $formValues. If missing, get it from the request. + * + * @param $formValues + * @param $field + * @param $type + * @param null $default + * @return mixed|null + */ + public function getFieldValue($formValues, $field, $type, $default = NULL) { + $value = CRM_Utils_Array::value($field, $formValues); + if (!$value) { + return CRM_Utils_Request::retrieve($field, $type, CRM_Core_DAO::$_nullObject, FALSE, $default); + } + return $value; + } + + /** + * Modify text to include wild card characters at beginning and end + * + * @param string $text + * @return string + */ + public function convertToSqlWildcard($text) { + if ($text) { + if (is_numeric($text)) { + $textID = $text; + } + + $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; + $text = $strtolower(CRM_Core_DAO::escapeString($text)); + if (strpos($text, '%') === FALSE) { + $text = "'%{$text}%'"; + return $text; + } + else { + $text = "'{$text}'"; + return $text; + } + } + else { + $text = "'%'"; + return $text; + } + } + function __destruct() { }