/** * hasDiffAddress * @param Model $model * @return boolean */ protected function hasDiffAddress(Model $model) { $this->_instance = $model->findById($model->id); if ($this->_instance !== null && isset($model->data['User']['live_country'])) { $prevAddress = array('live_country' => $this->_instance['User']['live_country'], 'live_place' => $this->_instance['User']['live_place'], 'live_address' => $this->_instance['User']['live_address']); $currentAddress = array('live_country' => $model->data['User']['live_country'], 'live_place' => $model->data['User']['live_place'], 'live_address' => $model->data['User']['live_address']); $result = Hash::diff($prevAddress, $currentAddress); return !empty($result) ? true : false; } return false; }
/** * Set plugin name * * @param array $backtrace Output "debug_backtrace" function * * @throws CakeException */ protected static function setPluginName(array $backtrace) { $pluginPath = APP . 'Plugin'; if (!is_readable($pluginPath)) { throw new CakeException(__d('hurad', 'Plugin path is not readable')); } if (strpos($backtrace[0]['file'], $pluginPath) != 0) { throw new CakeException(__d('hurad', 'You should use meta box in plugin')); } $diffPath = Hash::diff(explode(DIRECTORY_SEPARATOR, $backtrace[0]['file']), explode(DIRECTORY_SEPARATOR, $pluginPath)); self::$pluginName = reset($diffPath); }
public function testDifferentFormat001Save() { $dateFormats = array('d-m-Y' => '04-01-1952', 'd/m/Y' => '04/01/1952', 'Y/m/d' => '1952/01/04', 'Y-m-d' => '1952-01-04', 'Y-d-m' => '1952-04-01', 'Y/d/m' => '1952/04/01', 'm-d-Y' => '01-04-1952', 'm/d/Y' => '01/04/1952', 'Ymd' => '19520104', 'Ydm' => '19520401'); //-- this record must be resetted at every cycle $this->loadUnload(); $before = $this->Apple->read(null, 1); foreach ($dateFormats as $dateFormat => $expectedValue) { $this->loadUnload($dateFormat); $before = $this->Apple->read(null, 1); $postData = $before; $postData['Apple']['date'] = $expectedValue; $result = $this->Apple->save($postData); $this->assertInternalType('array', $result); $this->assertEquals($postData, $result); $expectedDiff = array('date' => $expectedValue); $diff = Hash::diff($result['Apple'], $before['Apple']); $this->assertEquals($expectedDiff, $diff); //-- Save record for next cycle $this->Apple->save($before); } }
/** * select method * * @return void */ public function select() { $this->__prepare(); if (Hash::get($this->viewVars['user'], 'User.id') !== Current::read('User.id')) { $this->throwBadRequest(); return; } $roomId = Hash::get($this->viewVars, 'roomId'); if (!$roomId) { $this->throwBadRequest(); return; } if ($this->request->is('post')) { //登録処理 //** ロールルームユーザデータ取得 $rolesRoomsUsers = $this->RolesRoomsUser->getRolesRoomsUsers(array('RolesRoomsUser.user_id' => $this->request->data['UserSelectCount']['user_id'], 'Room.id' => $roomId)); $userIds = Hash::extract($rolesRoomsUsers, '{n}.RolesRoomsUser.user_id'); sort($userIds); sort($this->request->data['UserSelectCount']['user_id']); //** user_idのチェック if (Hash::diff($userIds, $this->request->data['UserSelectCount']['user_id'])) { //diffがあった場合は、不正ありと判断する $this->throwBadRequest(); return; } $data = array_map(function ($userId) { return array('UserSelectCount' => array('user_id' => $userId, 'created_user' => Current::read('User.id'))); }, $this->request->data['UserSelectCount']['user_id']); //** 登録処理 if (!$this->UserSelectCount->saveUserSelectCount($data)) { $this->NetCommons->handleValidationError($this->UserSelectCount->validationErrors); } return; } else { //表示処理 //** レイアウトの設定 $this->viewClass = 'View'; $this->layout = 'NetCommons.modal'; //** 選択したユーザ取得 $users = $this->UserSelectCount->getUsers($roomId); if (!$users) { $users = array(); } $this->set('searchResults', $users); } }
/** * Handles automatic pagination of model records. * * @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel') * @param string|array $scope Additional find conditions to use while paginating * @param array $whitelist List of allowed fields for ordering. This allows you to prevent ordering * on non-indexed, or undesirable columns. * @return array Model query results * @throws MissingModelException * @throws NotFoundException */ public function paginate($object = null, $scope = array(), $whitelist = array()) { if (is_array($object)) { $whitelist = $scope; $scope = $object; $object = null; } $object = $this->_getObject($object); if (!is_object($object)) { throw new MissingModelException($object); } $options = $this->mergeOptions($object->alias); $options = $this->validateSort($object, $options, $whitelist); $options = $this->checkLimit($options); $conditions = $fields = $order = $limit = $page = $recursive = null; if (!isset($options['conditions'])) { $options['conditions'] = array(); } $type = 'all'; if (isset($options[0])) { $type = $options[0]; unset($options[0]); } extract($options); if (is_array($scope) && !empty($scope)) { $conditions = array_merge($conditions, $scope); } elseif (is_string($scope)) { $conditions = array($conditions, $scope); } if ($recursive === null) { $recursive = $object->recursive; } $extra = array_diff_key($options, compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive')); if (!empty($extra['findType'])) { $type = $extra['findType']; unset($extra['findType']); } if ($type !== 'all') { $extra['type'] = $type; } if (intval($page) < 1) { $page = 1; } $page = $options['page'] = (int) $page; if ($object->hasMethod('paginate')) { $results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra); } else { $parameters = compact('conditions', 'fields', 'order', 'limit', 'page'); if ($recursive != $object->recursive) { $parameters['recursive'] = $recursive; } $results = $object->find($type, array_merge($parameters, $extra)); } $defaults = $this->getDefaults($object->alias); unset($defaults[0]); if (!$results) { $count = 0; } elseif ($object->hasMethod('paginateCount')) { $count = $object->paginateCount($conditions, $recursive, $extra); } else { $parameters = compact('conditions'); if ($recursive != $object->recursive) { $parameters['recursive'] = $recursive; } $count = $object->find('count', array_merge($parameters, $extra)); } $pageCount = intval(ceil($count / $limit)); $requestedPage = $page; $page = max(min($page, $pageCount), 1); $paging = array('page' => $page, 'current' => count($results), 'count' => $count, 'prevPage' => $page > 1, 'nextPage' => $count > $page * $limit, 'pageCount' => $pageCount, 'order' => $order, 'limit' => $limit, 'options' => Hash::diff($options, $defaults), 'paramType' => $options['paramType']); if (!isset($this->Controller->request['paging'])) { $this->Controller->request['paging'] = array(); } $this->Controller->request['paging'] = array_merge((array) $this->Controller->request['paging'], array($object->alias => $paging)); if ($requestedPage > $page) { throw new NotFoundException(); } if (!in_array('Paginator', $this->Controller->helpers) && !array_key_exists('Paginator', $this->Controller->helpers)) { $this->Controller->helpers[] = 'Paginator'; } return $results; }
/** * Merge association of merge into data * * @param array &$data The data to merge. * @param array &$merge The data to merge. * @param string $association The association name to merge. * @param string $type The type of association * @param bool $selfJoin Whether or not this is a self join. * @return void */ protected function _mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) { if (isset($merge[0]) && !isset($merge[0][$association])) { $association = Inflector::pluralize($association); } $dataAssociation =& $data[$association]; if ($type === 'belongsTo' || $type === 'hasOne') { if (isset($merge[$association])) { $dataAssociation = $merge[$association][0]; } else { if (!empty($merge[0][$association])) { foreach ($merge[0] as $assoc => $data2) { if ($assoc !== $association) { $merge[0][$association][$assoc] = $data2; } } } if (!isset($dataAssociation)) { $dataAssociation = array(); if ($merge[0][$association]) { $dataAssociation = $merge[0][$association]; } } else { if (is_array($merge[0][$association])) { $mergeAssocTmp = array(); foreach ($dataAssociation as $k => $v) { if (!is_array($v)) { $dataAssocTmp[$k] = $v; } } foreach ($merge[0][$association] as $k => $v) { if (!is_array($v)) { $mergeAssocTmp[$k] = $v; } } $dataKeys = array_keys($data); $mergeKeys = array_keys($merge[0]); if ($mergeKeys[0] === $dataKeys[0] || $mergeKeys === $dataKeys) { $dataAssociation[$association] = $merge[0][$association]; } else { $diff = Hash::diff($dataAssocTmp, $mergeAssocTmp); $dataAssociation = array_merge($merge[0][$association], $diff); } } elseif ($selfJoin && array_key_exists($association, $merge[0])) { $dataAssociation = array_merge($dataAssociation, array($association => array())); } } } } else { if (isset($merge[0][$association]) && $merge[0][$association] === false) { if (!isset($dataAssociation)) { $dataAssociation = array(); } } else { foreach ($merge as $row) { $insert = array(); if (count($row) === 1) { $insert = $row[$association]; } elseif (isset($row[$association])) { $insert = array_merge($row[$association], $row); unset($insert[$association]); } if (empty($dataAssociation) || isset($dataAssociation) && !in_array($insert, $dataAssociation, true)) { $dataAssociation[] = $insert; } } } } }
/** * Test diff(); * * @return void */ public function testDiff() { $a = array(0 => array('name' => 'main'), 1 => array('name' => 'about')); $b = array(0 => array('name' => 'main'), 1 => array('name' => 'about'), 2 => array('name' => 'contact')); $result = Hash::diff($a, array()); $expected = $a; $this->assertEquals($expected, $result); $result = Hash::diff(array(), $b); $expected = $b; $this->assertEquals($expected, $result); $result = Hash::diff($a, $b); $expected = array(2 => array('name' => 'contact')); $this->assertEquals($expected, $result); $b = array(0 => array('name' => 'me'), 1 => array('name' => 'about')); $result = Hash::diff($a, $b); $expected = array(0 => array('name' => 'main')); $this->assertEquals($expected, $result); $a = array(); $b = array('name' => 'bob', 'address' => 'home'); $result = Hash::diff($a, $b); $this->assertEquals($result, $b); $a = array('name' => 'bob', 'address' => 'home'); $b = array(); $result = Hash::diff($a, $b); $this->assertEquals($result, $a); $a = array('key' => true, 'another' => false, 'name' => 'me'); $b = array('key' => 1, 'another' => 0); $expected = array('name' => 'me'); $result = Hash::diff($a, $b); $this->assertEquals($expected, $result); $a = array('key' => 'value', 'another' => null, 'name' => 'me'); $b = array('key' => 'differentValue', 'another' => null); $expected = array('key' => 'value', 'name' => 'me'); $result = Hash::diff($a, $b); $this->assertEquals($expected, $result); $a = array('key' => 'value', 'another' => null, 'name' => 'me'); $b = array('key' => 'differentValue', 'another' => 'value'); $expected = array('key' => 'value', 'another' => null, 'name' => 'me'); $result = Hash::diff($a, $b); $this->assertEquals($expected, $result); $a = array('key' => 'value', 'another' => null, 'name' => 'me'); $b = array('key' => 'differentValue', 'another' => 'value'); $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me'); $result = Hash::diff($b, $a); $this->assertEquals($expected, $result); $a = array('key' => 'value', 'another' => null, 'name' => 'me'); $b = array(0 => 'differentValue', 1 => 'value'); $expected = $a + $b; $result = Hash::diff($a, $b); $this->assertEquals($expected, $result); }
public function paginate($object = null, $scope = array(), $whitelist = array()) { if (is_array($object)) { $whitelist = $scope; $scope = $object; $object = null; } $object = $this->_getObject($object); if (!is_object($object)) { throw new MissingModelException($object); } $options = $this->mergeOptions($object->alias); $options = $this->validateSort($object, $options, $whitelist); $options = $this->checkLimit($options); $conditions = $fields = $order = $limit = $page = $recursive = null; if (!isset($options['conditions'])) { $options['conditions'] = array(); } $type = 'all'; if (isset($options[0])) { $type = $options[0]; unset($options[0]); } extract($options); if (is_array($scope) && !empty($scope)) { $conditions = array_merge($conditions, $scope); } elseif (is_string($scope)) { $conditions = array($conditions, $scope); } if ($recursive === null) { $recursive = $object->recursive; } $extra = array_diff_key($options, compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive')); if (!empty($extra['findType'])) { $type = $extra['findType']; unset($extra['findType']); } if ($type !== 'all') { $extra['type'] = $type; } if ((int) $page < 1) { $page = 1; } $page = $options['page'] = (int) $page; if ($object->hasMethod('paginate')) { $results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra); } else { $parameters = compact('conditions', 'fields', 'order', 'limit', 'page'); if ($recursive != $object->recursive) { $parameters['recursive'] = $recursive; } /***********************************************************************************/ /****************************** CUSTOMIZED BY WEBZASH ******************************/ /***********************************************************************************/ /** * This fix is related to bug reported at * https://groups.google.com/forum/#!topic/webzash-help/A6fpPwOzHfA * MySQL seems to mess up the results when its at last page of pagination. * This is due to the limit clause being eg : 30, 10 if there are total 36 rows. * Changing the limit clause at the last page to 30, 6 to match the number of exact * number of rows remaining fixes the problem. */ $temp_params = $parameters; $temp_parameters = compact('conditions'); if ($recursive != $object->recursive) { $temp_parameters['recursive'] = $recursive; } $temp_count = $object->find('count', array_merge($temp_parameters, $extra)); $temp_pageCount = (int) ceil($temp_count / $limit); $temp_requestedPage = $page; $temp_page = max(min($page, $temp_pageCount), 1); /* If last page, then remove the page parameter and set the limit and offset parameters */ if ($temp_pageCount == $temp_requestedPage) { $temp_params['offset'] = ($temp_requestedPage - 1) * $temp_params['limit']; $temp_params['limit'] = $temp_count - $temp_params['offset']; unset($temp_params['page']); } $results = $object->find($type, array_merge($temp_params, $extra)); } $defaults = $this->getDefaults($object->alias); unset($defaults[0]); if (!$results) { $count = 0; } elseif ($object->hasMethod('paginateCount')) { $count = $object->paginateCount($conditions, $recursive, $extra); } elseif ($page === 1 && count($results) < $limit) { $count = count($results); } else { $parameters = compact('conditions'); if ($recursive != $object->recursive) { $parameters['recursive'] = $recursive; } $count = $object->find('count', array_merge($parameters, $extra)); } $pageCount = (int) ceil($count / $limit); $requestedPage = $page; $page = max(min($page, $pageCount), 1); $paging = array('page' => $page, 'current' => count($results), 'count' => $count, 'prevPage' => $page > 1, 'nextPage' => $count > $page * $limit, 'pageCount' => $pageCount, 'order' => $order, 'limit' => $limit, 'options' => Hash::diff($options, $defaults), 'paramType' => $options['paramType']); if (!isset($this->Controller->request['paging'])) { $this->Controller->request['paging'] = array(); } $this->Controller->request['paging'] = array_merge((array) $this->Controller->request['paging'], array($object->alias => $paging)); if ($requestedPage > $page) { throw new NotFoundException(); } if (!in_array('Paginator', $this->Controller->helpers) && !array_key_exists('Paginator', $this->Controller->helpers)) { $this->Controller->helpers[] = 'Paginator'; } return $results; }
/** * Abstract for Hahs/Set/array_diff() * * @param array $one * @param array $two * @return array $diff */ private function _diff($one, $two) { if (class_exists('Hash')) { return Hash::diff($one, $two); } if (class_exists('Set')) { return Set::diff($one, $two); } return array_diff($one, $two); }