| 
      
      
      From: <gem...@li...> - 2012-03-14 17:30:06
       | 
| Revision: 551
          http://gemstracker.svn.sourceforge.net/gemstracker/?rev=551&view=rev
Author:   matijsdejong
Date:     2012-03-14 17:29:56 +0000 (Wed, 14 Mar 2012)
Log Message:
-----------
Added setOnTextFilter for DatabaseModelAbstract.php and ConcatenatedRow.php
Modified Paths:
--------------
    trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php
    trunk/library/classes/MUtil/Model/ModelAbstract.php
    trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php
Modified: trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php
===================================================================
--- trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php	2012-03-13 14:06:54 UTC (rev 550)
+++ trunk/library/classes/MUtil/Model/DatabaseModelAbstract.php	2012-03-14 17:29:56 UTC (rev 551)
@@ -55,11 +55,18 @@
      *
      * Zend_Element allows some other extended characters, but those may not work
      * with some browsers.
+     *
+     * If there exists a table containing two fields where one fields maps with this key
+     * to the other, shoot the table designer!!!
      */
     const KEY_COPIER = '__c_1_3_copy__%s__key_k_0_p_1__';
-    // If there exists a table containing two fields that map to these, shoot the table designer!!!
 
     /**
+     * Name for query filter transformers
+     */
+    const TEXTFILTER_TRANSFORMER = 'filter_transformer';
+
+    /**
      * @var array When specified delete() updates the selected rows with these values, instead of physically deleting the rows.
      */
     protected $_deleteValues;
@@ -228,7 +235,7 @@
             if ($this->is($name, 'table', $table_name)) {
                 if (array_key_exists($name, $data)) {
 
-                    if ($data[$name] && ($len = $this->get($name, 'maxlength'))) {
+                    if ($data[$name] && (! is_array($data[$name])) && ($len = $this->get($name, 'maxlength'))) {
                         $tableData[$name] = substr($data[$name], 0, $len);
                     } else {
                         $tableData[$name] = $data[$name];
@@ -429,10 +436,12 @@
             }
 
             // Check for actual values for this table to save.
+            // MUtil_Echo::track($newValues);
             if ($returnValues = $this->_filterDataFor($table_name, $newValues, ! $update)) {
                 if (MUtil_Model::$verbose) {
                     MUtil_Echo::r($returnValues, 'Return');
                 }
+                // MUtil_Echo::track($returnValues);
 
                 if ($update) {
                     // MUtil_Echo::r($filter);
@@ -678,12 +687,59 @@
     }
 
     /**
+     * Creates an SQL filter for this value on this name.
+     *
+     * @param mixed $filter The value to filter for
+     * @param string $name The name of the current field
+     * @param string $sqlField The SQL name of the current field
+     * @return mixed Nothing, a single filter statement or an array of OR filters
+     */
+    public function getOnTextFilter($filter, $name, $sqlField)
+    {
+        if ($call = $this->get($name, self::TEXTFILTER_TRANSFORMER)) {
+
+            if (is_callable($call)) {
+                return call_user_func($call, $filter, $name, $sqlField, $this);
+            } else {
+                return $call;
+            }
+        }
+
+        if ($options = $this->get($name, 'multiOptions')) {
+            $adapter = $this->getAdapter();
+            $wheres = array();
+            foreach ($options as $key => $value) {
+                // MUtil_Echo::track($key, $value, $filter, stripos($value, $filter));
+                if (stripos($value, $filter) !== false) {
+                    if (null === $key) {
+                        $wheres[] = $sqlField . ' IS NULL';
+                    } else {
+                        $wheres[] = $sqlField . ' = ' . $adapter->quote($key);
+                    }
+                }
+            }
+            return $wheres;
+        }
+
+        if (is_numeric($filter) || $this->isString($name)) {
+            // Only for strings or all fields when numeric
+            return $sqlField . ' LIKE \'%' . trim($this->getAdapter()->quote($filter), '\'') . '%\'';
+        }
+    }
+
+    /**
      * The select object where we get the query from.
      *
      * @return Zend_Db_Table_Select
      */
     abstract public function getSelect();
 
+    /**
+     * Creates a filter for this model for the given wildcard search text.
+     *
+     * @param string $searchText
+     * @return array An array of filter statements for wildcard text searching for this model type
+     */
     public function getTextSearchFilter($searchText)
     {
         $filter = array();
@@ -705,21 +761,13 @@
             if ($fields) {
                 foreach ($this->getTextSearches($searchText) as $searchOn) {
                     $wheres = array();
-                    $search = trim($adapter->quote($searchOn), '\'');
                     foreach ($fields as $name => $sqlField) {
-                        if ($options = $this->get($name, 'multiOptions')) {
-                            foreach ($options as $key => $value) {
-                                if (stripos($value, $searchOn) !== false) {
-                                    if (null === $key) {
-                                        $wheres[] = $sqlField . ' IS NULL';
-                                    } else {
-                                        $wheres[] = $sqlField . ' = ' . $adapter->quote($key);
-                                    }
-                                }
+                        if ($where = $this->getOnTextFilter($searchOn, $name, $sqlField)) {
+                            if (is_array($where)) {
+                                $wheres = array_merge($wheres, $where);
+                            } else {
+                                $wheres[] = $where;
                             }
-                        } elseif (is_numeric($searchOn) || $this->isString($name)) {
-                            // Only for strings or all fields when numeric
-                            $wheres[] = $sqlField . ' LIKE \'%' . $search . '%\'';
                         }
                     }
 
@@ -834,6 +882,19 @@
     }
 
     /**
+     * Changes the key copy string that is used to create a new identifier
+     * for keys.
+     *
+     * @param string $value A sting of at least 3 characters containing %s.
+     * @return MUtil_Model_DatabaseModelAbstract (continuation pattern)
+     */
+    public function setKeyCopier($value = self::KEY_COPIER)
+    {
+        $this->keyCopier = $value;
+        return $this;
+    }
+
+    /**
      * When passed an array this method set the keys of this database object
      * to those keys.
      * When passed a string it is assumed to be a table name and the keys of
@@ -855,15 +916,15 @@
     }
 
     /**
-     * Changes the key copy string that is used to create a new identifier
-     * for keys.
+     * Sets a name to a callable function for query filtering.
      *
-     * @param string $value A sting of at least 3 characters containing %s.
-     * @return MUtil_Model_DatabaseModelAbstract (continuation pattern)
+     * @param string $name The fieldname
+     * @param mixed $callableOrConstant A constant or a function of this type: callable($filter, $name, $sqlField, MUtil_Model_DatabaseModelAbstract $model)
+     * @return MUtil_Model_ModelAbstract (continuation pattern)
      */
-    public function setKeyCopier($value = self::KEY_COPIER)
+    public function setOnTextFilter($name, $callableOrConstant)
     {
-        $this->keyCopier = $value;
+        $this->set($name, self::TEXTFILTER_TRANSFORMER, $callableOrConstant);
         return $this;
     }
 }
Modified: trunk/library/classes/MUtil/Model/ModelAbstract.php
===================================================================
--- trunk/library/classes/MUtil/Model/ModelAbstract.php	2012-03-13 14:06:54 UTC (rev 550)
+++ trunk/library/classes/MUtil/Model/ModelAbstract.php	2012-03-14 17:29:56 UTC (rev 551)
@@ -716,12 +716,24 @@
         return $this->getMeta('textFilter', MUtil_Model::TEXT_FILTER);
     }
 
+    /**
+     * Splits a wildcard search text into its constituent parts.
+     *
+     * @param string $searchText
+     * @return array
+     */
     public function getTextSearches($searchText)
     {
         // Replace -/ with space, trim & remove all double spaces
         return explode(' ', str_replace('  ', ' ', trim(strtr($searchText, '-+/\\',  '    '))));
     }
 
+    /**
+     * Creates a filter for this model for the given wildcard search text.
+     *
+     * @param string $searchText
+     * @return array An array of filter statements for wildcard text searching for this model type
+     */
     public function getTextSearchFilter($searchText)
     { }
 
Modified: trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php
===================================================================
--- trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php	2012-03-13 14:06:54 UTC (rev 550)
+++ trunk/library/classes/MUtil/Model/Type/ConcatenatedRow.php	2012-03-14 17:29:56 UTC (rev 551)
@@ -46,17 +46,48 @@
  */
 class MUtil_Model_Type_ConcatenatedRow
 {
+    /**
+     * The character used to separate values when displaying.
+     *
+     * @var string
+     */
     protected $displaySeperator = ' ';
 
+    /**
+     * The character used to separate values when storing.
+     *
+     * @var string
+     */
     protected $seperatorChar = ' ';
 
+    /**
+     * When true the value is padded on both sides with the $seperatorChar.
+     *
+     * Makes it easier to filter.
+     *
+     * @see $seperatorChar
+     *
+     * @var boolean
+     */
     protected $valuePad = true;
 
+    /**
+     * MUtil_Ra::args() parameter passing is allowed.
+     *
+     * @param string $seperatorChar
+     * @param string $displaySeperator
+     * @param boolean $valuePad
+     */
     public function __construct($seperatorChar = ' ', $displaySeperator = ' ', $valuePad = true)
     {
-        $this->seperatorChar = substr($seperatorChar . ' ', 0, 1);
-        $this->displaySeperator = $displaySeperator;
-        $this->valuePad = $valuePad;
+        $args = MUtil_Ra::args(
+                func_get_args(),
+                array('seperatorChar' => 'is_string', 'displaySeperator' => 'is_string', 'valuePad' => 'is_boolean'),
+                array('seperatorChar' => ' ', 'displaySeperator' => ' ', 'valuePad' => true));
+
+        $this->seperatorChar    = substr($args['seperatorChar'] . ' ', 0, 1);
+        $this->displaySeperator = $args['displaySeperator'];
+        $this->valuePad         = $args['valuePad'];
     }
 
     /**
@@ -72,6 +103,10 @@
         $model->setOnLoad($name, array($this, 'loadValue'));
         $model->setOnSave($name, array($this, 'saveValue'));
 
+        if ($model instanceof MUtil_Model_DatabaseModelAbstract) {
+            $model->setOnTextFilter($name, array($this, 'textFilter'));
+        }
+
         return $this;
     }
 
@@ -99,7 +134,7 @@
      */
     public function loadValue($value, $isNew = false, $name = null, array $context = array())
     {
-        // MUtil_Echo::track($value);
+        // MUtil_Echo::track($value, $name, $context);
         if (! is_array($value)) {
             if ($this->valuePad) {
                 $value = trim($value, $this->seperatorChar);
@@ -125,6 +160,7 @@
      */
     public function saveValue($value, $isNew = false, $name = null, array $context = array())
     {
+        // MUtil_Echo::track($value);
         if (is_array($value)) {
             $value = implode($this->seperatorChar, $value);
 
@@ -134,4 +170,39 @@
         }
         return $value;
     }
+
+    /**
+     *
+     * @param string $filter The text to filter for
+     * @param string $name The model field name
+     * @param string $sqlField The SQL field name
+     * @param MUtil_Model_DatabaseModelAbstract $model
+     * @return array Array of OR-filter statements
+     */
+    public function textFilter($filter, $name, $sqlField, MUtil_Model_DatabaseModelAbstract $model)
+    {
+        if ($options = $model->get($name, 'multiOptions')) {
+            $adapter = $model->getAdapter();
+            $wheres = array();
+            foreach ($options as $key => $value) {
+                // MUtil_Echo::track($key, $value, $filter, stripos($value, $filter));
+                if (stripos($value, $filter) !== false) {
+                    if (null === $key) {
+                        $wheres[] = $sqlField . ' IS NULL';
+                    } else {
+                        $quoted   = $adapter->quote($key);
+                        $wheres[] = $sqlField . " LIKE '%" . $this->seperatorChar . $quoted . $this->seperatorChar . "%'";
+
+                        if (! $this->valuePad) {
+                            // Add other options
+                            $wheres[] = $sqlField . " LIKE '" . $quoted . $this->seperatorChar . "%'";
+                            $wheres[] = $sqlField . " LIKE '%" . $this->seperatorChar . $quoted . "'";
+                            $wheres[] = $sqlField . " = " . $quoted;
+                        }
+                    }
+                }
+            }
+            return $wheres;
+        }
+    }
 }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 | 
| 
      
      
      From: <gem...@li...> - 2012-12-13 13:14:01
       | 
| Revision: 1059
          http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1059&view=rev
Author:   matijsdejong
Date:     2012-12-13 13:13:53 +0000 (Thu, 13 Dec 2012)
Log Message:
-----------
processAfterLoad expects a nested array
Modified Paths:
--------------
    trunk/library/classes/MUtil/Model/JoinModel.php
    trunk/library/classes/MUtil/Model/TableModel.php
Modified: trunk/library/classes/MUtil/Model/JoinModel.php
===================================================================
--- trunk/library/classes/MUtil/Model/JoinModel.php	2012-12-13 10:08:08 UTC (rev 1058)
+++ trunk/library/classes/MUtil/Model/JoinModel.php	2012-12-13 13:13:53 UTC (rev 1059)
@@ -373,9 +373,9 @@
         if ($this->getChanged() > $oldChanged) {
             $this->setChanged(++$oldChanged);
         }
-        
+
         // Handle possible onLoad
-        $newValues = $this->processAfterLoad($newValues);
+        $newValues = $this->processAfterLoad(array($newValues));
 
         return $newValues;
     }
Modified: trunk/library/classes/MUtil/Model/TableModel.php
===================================================================
--- trunk/library/classes/MUtil/Model/TableModel.php	2012-12-13 10:08:08 UTC (rev 1058)
+++ trunk/library/classes/MUtil/Model/TableModel.php	2012-12-13 13:13:53 UTC (rev 1059)
@@ -120,9 +120,9 @@
         // $this->_saveTableData returns the new row values, including any automatic changes.
         // add $newValues to throw nothing away.
         $updatedValues = $this->_saveTableData($this->_table, $newValues, $filter, parent::SAVE_MODE_ALL) + $newValues;
-        
+
         // Handle possible onLoad
-        $updatedValues = $this->processAfterLoad($updatedValues);
+        $updatedValues = $this->processAfterLoad(array($updatedValues));
         return  $updatedValues;
     }
 }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 | 
| 
      
      
      From: <gem...@li...> - 2012-12-17 09:59:18
       | 
| Revision: 1065
          http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1065&view=rev
Author:   mennodekker
Date:     2012-12-17 09:59:11 +0000 (Mon, 17 Dec 2012)
Log Message:
-----------
Fixed commit 1059: output of save should not be a nested array
Modified Paths:
--------------
    trunk/library/classes/MUtil/Model/JoinModel.php
    trunk/library/classes/MUtil/Model/TableModel.php
Modified: trunk/library/classes/MUtil/Model/JoinModel.php
===================================================================
--- trunk/library/classes/MUtil/Model/JoinModel.php	2012-12-14 16:26:58 UTC (rev 1064)
+++ trunk/library/classes/MUtil/Model/JoinModel.php	2012-12-17 09:59:11 UTC (rev 1065)
@@ -375,7 +375,7 @@
         }
 
         // Handle possible onLoad
-        $newValues = $this->processAfterLoad(array($newValues));
+        $newValues = reset($this->processAfterLoad(array($newValues)));
 
         return $newValues;
     }
Modified: trunk/library/classes/MUtil/Model/TableModel.php
===================================================================
--- trunk/library/classes/MUtil/Model/TableModel.php	2012-12-14 16:26:58 UTC (rev 1064)
+++ trunk/library/classes/MUtil/Model/TableModel.php	2012-12-17 09:59:11 UTC (rev 1065)
@@ -122,7 +122,7 @@
         $updatedValues = $this->_saveTableData($this->_table, $newValues, $filter, parent::SAVE_MODE_ALL) + $newValues;
 
         // Handle possible onLoad
-        $updatedValues = $this->processAfterLoad(array($updatedValues));
+        $updatedValues = reset($this->processAfterLoad(array($updatedValues)));
         return  $updatedValues;
     }
 }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 | 
| 
      
      
      From: <gem...@li...> - 2012-12-17 10:01:10
       | 
| Revision: 1066
          http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1066&view=rev
Author:   mennodekker
Date:     2012-12-17 10:01:04 +0000 (Mon, 17 Dec 2012)
Log Message:
-----------
strict standards fix
Modified Paths:
--------------
    trunk/library/classes/MUtil/Model/JoinModel.php
    trunk/library/classes/MUtil/Model/TableModel.php
Modified: trunk/library/classes/MUtil/Model/JoinModel.php
===================================================================
--- trunk/library/classes/MUtil/Model/JoinModel.php	2012-12-17 09:59:11 UTC (rev 1065)
+++ trunk/library/classes/MUtil/Model/JoinModel.php	2012-12-17 10:01:04 UTC (rev 1066)
@@ -375,9 +375,9 @@
         }
 
         // Handle possible onLoad
-        $newValues = reset($this->processAfterLoad(array($newValues)));
+        $newValues = $this->processAfterLoad(array($newValues));
 
-        return $newValues;
+        return reset($newValues);
     }
 
     /**
Modified: trunk/library/classes/MUtil/Model/TableModel.php
===================================================================
--- trunk/library/classes/MUtil/Model/TableModel.php	2012-12-17 09:59:11 UTC (rev 1065)
+++ trunk/library/classes/MUtil/Model/TableModel.php	2012-12-17 10:01:04 UTC (rev 1066)
@@ -122,7 +122,7 @@
         $updatedValues = $this->_saveTableData($this->_table, $newValues, $filter, parent::SAVE_MODE_ALL) + $newValues;
 
         // Handle possible onLoad
-        $updatedValues = reset($this->processAfterLoad(array($updatedValues)));
-        return  $updatedValues;
+        $updatedValues = $this->processAfterLoad(array($updatedValues));
+        return reset($updatedValues);
     }
 }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |