Beispiel #1
0
 /**
  * testIsEqual method
  *
  * @access public
  * @return void
  */
 function testIsEqual()
 {
     $a = array(0 => array('name' => 'main'), 1 => array('name' => 'about'));
     $b = array(0 => array('name' => 'main'), 1 => array('name' => 'about'), 2 => array('name' => 'contact'));
     $this->assertTrue(Set::isEqual($a, $a));
     $this->assertTrue(Set::isEqual($b, $b));
     $this->assertFalse(Set::isEqual($a, $b));
     $this->assertFalse(Set::isEqual($b, $a));
 }
Beispiel #2
0
 /**
  * Sets update actions and their conditions which get executed in after save,
  * affects model->data when necessary
  *
  * @param Model $model Model object that method is triggered on
  * @return boolean Always true otherwise model will not save
  */
 public function beforeSave(&$model)
 {
     $this->_update[$model->alias] = array();
     // Sets new order and new groups from model->data
     $this->_setNewOrder($model);
     $this->_setNewGroups($model);
     $orderField = $this->settings[$model->alias]['order_field'];
     $escapedOrderField = $this->settings[$model->alias]['escaped_order_field'];
     // Adding
     if (!$model->id) {
         // Order not specified
         if (!isset($this->_newOrder[$model->alias])) {
             // Insert at end of list
             $model->data[$model->alias][$orderField] = $this->_getHighestOrder($model, $this->_newGroups[$model->alias]) + 1;
             // Order specified
         } else {
             // The updateAll called in afterSave uses old groups values as default
             // conditions to restrict which records are updated, so set old groups
             // to new groups as old isn't set.
             $this->_oldGroups[$model->alias] = $this->_newGroups[$model->alias];
             // Insert and increment order of records it's inserted before
             $this->_update[$model->alias][] = array('action' => array($escapedOrderField => $escapedOrderField . ' + 1'), 'conditions' => array($escapedOrderField . ' >=' => $this->_newOrder[$model->alias]));
         }
         // Editing
     } else {
         // No action if no new order or group specified
         if (!isset($this->_newOrder[$model->alias]) && !isset($this->_newGroups[$model->alias])) {
             return;
         }
         $this->_setOldOrder($model);
         $this->_setOldGroups($model);
         // No action if new and old group and order same
         if ($this->_newOrder[$model->alias] == $this->_oldOrder[$model->alias] && Set::isEqual($this->_newGroups[$model->alias], $this->_oldGroups[$model->alias])) {
             return;
         }
         // If changing group
         if ($this->_newGroups[$model->alias] && !Set::isEqual($this->_newGroups[$model->alias], $this->_oldGroups[$model->alias])) {
             // Decrement records in old group with higher order than moved record old order
             $this->_update[$model->alias][] = array('action' => array($escapedOrderField => $escapedOrderField . ' - 1'), 'conditions' => array($escapedOrderField . ' >=' => $this->_oldOrder[$model->alias]));
             // Order not specified
             if (!isset($this->_newOrder[$model->alias])) {
                 // Insert at end of new group
                 $model->data[$model->alias][$orderField] = $this->_getHighestOrder($model, $this->_newGroups[$model->alias]) + 1;
                 // Order specified
             } else {
                 // Increment records in new group with higher order than moved record new order
                 $this->_update[$model->alias][] = array('action' => array($escapedOrderField => $escapedOrderField . ' + 1'), 'conditions' => array($escapedOrderField . ' >=' => $this->_newOrder[$model->alias]), 'group_values' => $this->_newGroups[$model->alias]);
             }
             // Same group
         } else {
             // If moving up
             if ($this->_newOrder[$model->alias] < $this->_oldOrder[$model->alias]) {
                 // Increment order of those in between
                 $this->_update[$model->alias][] = array('action' => array($escapedOrderField => $escapedOrderField . ' + 1'), 'conditions' => array(array($escapedOrderField . ' >=' => $this->_newOrder[$model->alias]), array($escapedOrderField . ' <' => $this->_oldOrder[$model->alias])));
                 // Moving down
             } else {
                 // Decrement order of those in between
                 $this->_update[$model->alias][] = array('action' => array($escapedOrderField => $escapedOrderField . ' - 1'), 'conditions' => array(array($escapedOrderField . ' >' => $this->_oldOrder[$model->alias]), array($escapedOrderField . ' <=' => $this->_newOrder[$model->alias])));
             }
         }
     }
     return true;
 }
Beispiel #3
0
    function set_utility($method = 'options')
    {
        if ($method == 'merge') {
            $A1 = array('Model' => array('f1' => '1', 'f2' => '1', 'f3' => '1'));
            $A2 = array('Model' => array('f3' => '2', 'f4' => '1', 'f5' => '1'));
            $merged_arrays = array_merge($A1, $A2);
            $added_arrays = $A1 + $A2;
            $merged_recursive = array_merge_recursive($A1, $A2);
            function array_update($arr, $ins)
            {
                if (is_array($arr) && is_array($ins)) {
                    foreach ($ins as $k => $v) {
                        if (isset($arr[$k]) && is_array($v) && is_array($arr[$k])) {
                            $arr[$k] = array_update($arr[$k], $v);
                        } else {
                            $arr[$k] = $v;
                        }
                    }
                } elseif (!is_array($arr) && (strlen($arr) == 0 || $arr == 0)) {
                    $arr = $ins;
                }
                return $arr;
            }
            $array_update = array_update($A1, $A2);
            $set_merge = Set::merge($A1, $A2);
            $REPORT = array('A1' => $A1, 'A2' => $A2, 'merged' => $merged_arrays, 'added' => $added_arrays, 'merged_recursive' => $merged_recursive, 'array_update' => $array_update, 'Set::merge' => $set_merge, 'Set::isEqual($array_update, $set_merge)' => Set::isEqual($array_update, $set_merge));
        } elseif ($method == 'diff') {
            $set = 'a.b.c';
            $DiffList = array('a' => 0, 'a.b' => 0, 'a.b.c' => 1, 'a.b.c.x' => 1, 'a.b.x' => 0, 'a.x' => 0, 'x' => 0, 'b' => 0, 'b.c' => 0, '*' => 1, '*.b.c' => 1, '*.x' => 0);
            $REPORT['results'] = '';
            foreach ($DiffList as $diff_set => $expect) {
                $SetArray = explode('.', $set);
                $DiffArray = explode('.', $diff_set);
                $depth_delta = count($SetArray) - count($DiffArray);
                if ($depth_delta > 0) {
                    foreach (range(1, $depth_delta) as $n) {
                        $DiffArray[] = $DiffArray[count($DiffArray) - 1] == '*' ? '*' : '!';
                    }
                }
                $SetDiff = Set::diff($SetArray, $DiffArray);
                $mismatch = 0;
                if ($SetDiff) {
                    foreach ($SetArray as $n => $x) {
                        if (isset($SetDiff[$n]) && $DiffArray[$n] != '*') {
                            $mismatch = "{$n} => {$x}";
                        }
                    }
                }
                $has_privilege = (int) (!(bool) $mismatch);
                $REPORT[$diff_set] = array('pass' => (int) ($has_privilege == $expect), 'expect' => $expect, 'result' => $has_privilege, 'mismatch' => $mismatch, "{$set} diff {$diff_set}" => $SetDiff);
            }
            $REPORT['results'] = Set::extract('{s}.pass', $REPORT);
            unset($REPORT['results'][0]);
        } else {
            $content = <<<EOMENU
<h3>methods</h3>
<a href="/demo/set_utility/merge">Set::merge</a><br />
<a href="/demo/set_utility/diff">Set::diff</a><br />
EOMENU;
            $this->set('header', 'Gatekeeper Test');
            $this->set('content', $content);
            return $this->render('index');
        }
        $this->set('header', 'Set Examples');
        $this->set('data', $REPORT);
        $this->render('report');
    }
Beispiel #4
0
 /**
  * Sets update actions and their conditions which get executed in after save
  *
  * @param AppModel $model
  */
 function setSaveUpdateData(&$model)
 {
     // Sets new order and new groups from model->data
     $this->setNewOrder($model);
     $this->setNewGroups($model);
     // Adding
     if (!$model->id) {
         // Order not specified
         if (!$this->newOrder) {
             // Insert at end of list
             $model->data[$model->alias][$this->orderField] = $this->getHighestOrder($model, $this->newGroups) + 1;
             // Order specified
         } else {
             // The updateAll called in afterSave uses old groups values as default
             // conditions to restrict which records are updated, so set old groups
             // to new groups as old isn't set.
             $this->oldGroups = $this->newGroups;
             // Insert and increment order of records it's inserted before
             $this->update = array('action' => array($this->escapedOrderField => $this->escapedOrderField . ' + 1'), 'conditions' => array($this->escapedOrderField . ' >=' => $this->newOrder));
         }
         // Editing
     } else {
         // No action if no new order or group specified
         if (is_null($this->newOrder) && is_null($this->newGroups)) {
             return;
         }
         $this->setOldOrder($model);
         $this->setOldGroups($model);
         // No action if new and old group and order same
         if ($this->newOrder == $this->oldOrder && Set::isEqual($this->newGroups, $this->oldGroups)) {
             return;
         }
         // If changing group
         if ($this->newGroups && !Set::isEqual($this->newGroups, $this->oldGroups)) {
             // Decrement records in old group with higher order than moved record old order
             $this->update = array('action' => array($this->escapedOrderField => $this->escapedOrderField . ' - 1'), 'conditions' => array($this->escapedOrderField . ' >=' => $this->oldOrder));
             // Insert at end of new group
             $model->data[$model->alias][$this->orderField] = $this->getHighestOrder($model, $this->newGroups) + 1;
             // Same group
         } else {
             // If moving up
             if ($this->newOrder < $this->oldOrder) {
                 // Increment order of those in between
                 $this->update = array('action' => array($this->escapedOrderField => $this->escapedOrderField . ' + 1'), 'conditions' => array(array($this->escapedOrderField . ' >=' => $this->newOrder), array($this->escapedOrderField . ' <' => $this->oldOrder)));
                 // Moving down
             } else {
                 // Decrement order of those in between
                 $this->update = array('action' => array($this->escapedOrderField => $this->escapedOrderField . ' - 1'), 'conditions' => array(array($this->escapedOrderField . ' >' => $this->oldOrder), array($this->escapedOrderField . ' <=' => $this->newOrder)));
             }
         }
     }
 }
 function validateConditions(&$Model, $primaryKey)
 {
     if (!empty($this->settings[$Model->alias]['saveOn']['conditions'])) {
         $data = $this->getFieldsForConditions(&$Model, $primaryKey);
         if (isset($data[$Model->alias])) {
             $data = $data[$Model->alias];
         }
         if (Set::isEqual($data, $this->settings[$Model->alias]['saveOn']['conditions'])) {
             return true;
         } else {
             return false;
         }
     }
     return false;
 }