public function check($modelName) { $this->loadModel($modelName); $transformedFields = array(); foreach ($this->request->query['data'] as $association => $fields) { $Model = $association === $modelName ? $this->{$modelName} : $this->{$modelName}->{$association}; if (!$Model) { continue; } $transformedFields[$Model->name] = $this->__transformFields($Model, array($Model->alias), $fields); } $ChangeEvent = new CakeEvent('Form.change', $this, array('fields' => $transformedFields)); $this->getEventManager()->dispatch($ChangeEvent); $fields = array(); foreach (Hash::flatten($ChangeEvent->data['fields']) as $key => $value) { $fieldParts = explode('.', $key); $lastPart = array_pop($fieldParts); if (is_numeric($lastPart)) { $lastPart = array_pop($fieldParts); $fields[implode('.', $fieldParts)][$lastPart][] = $value; } else { $fields[implode('.', $fieldParts)][$lastPart] = $value; } } $this->set('fields', $fields); $this->set('_serialize', array('fields')); }
/** * Sets a configuration variable into this action * * If called with no arguments, all configuration values are * returned. * * $key is interpreted with dot notation, like the one used for * Configure::write() * * If $key is string and $value is not passed, it will return the * value associated with such key. * * If $key is an array and $value is empty, then $key will * be interpreted as key => value dictionary of settings and * it will be merged directly with $this->settings * * If $key is a string, the value will be inserted in the specified * slot as indicated using the dot notation * * @param mixed $key * @param mixed $value * @param boolean $merge * @return mixed|CrudAction */ public function config($key = null, $value = null, $merge = true) { if ($key === null && $value === null) { return $this->_settings; } if ($value === null) { if (is_array($key)) { if ($merge) { $this->_settings = Hash::merge($this->_settings, $key); } else { foreach (Hash::flatten($key) as $k => $v) { $this->_settings = Hash::insert($this->_settings, $k, $v); } } return $this; } return Hash::get($this->_settings, $key); } if (is_array($value)) { if ($merge) { $value = array_merge((array) Hash::get($this->_settings, $key), $value); } else { foreach ($value as $k => $v) { $this->_settings = Hash::insert($this->_settings, $k, $v); } } } $this->_settings = Hash::insert($this->_settings, $key, $value); return $this; }
/** * index * * @return void */ public function index() { if (!$this->viewVars['blockId']) { $this->autoRender = false; return; } if (!$this->initLink(['linkFrameSetting'])) { return; } $this->Categories->initCategories(true, '{n}.Category.id'); //条件 $conditions = $this->__setConditions(); //取得 $links = $this->Link->getLinks($conditions); $links = Hash::combine($links, '{n}.Link.id', '{n}', '{n}.Category.id'); //Viewにセット $results = array('links' => $links); $results = $this->camelizeKeyRecursive($results); $this->set($results); //Tokenセット $this->request->data = array('Frame' => array('id' => $this->viewVars['frameId']), 'Link' => array('id' => null, 'key' => null)); $tokenFields = Hash::flatten($this->request->data); $hiddenFields = array('Frame.id'); $this->set('tokenFields', $tokenFields); $this->set('hiddenFields', $hiddenFields); }
/** * add method * * @return void */ public function upload() { //CakeLog::debug('add() $this->params=' . print_r($this->params, true)); //TODO: 権限チェック if ($this->request->isPost()) { CakeLog::debug('upload() $this->data=' . print_r($this->data, true)); $data = $this->data; $data['File'] = $this->FileUpload->upload($this); CakeLog::debug('upload() $data=' . print_r($data, true)); if (!($result = $this->FileModel->saveFile($data))) { //TODO: Error return; } $result[$this->FileModel->alias]['readableSize'] = CakeNumber::toReadableSize($result[$this->FileModel->alias]['size']); $this->NetCommons->renderJson($result); } else { $ret = $this->FileModel->find('first', ['recursive' => -1]); CakeLog::debug('FileController::upload() $ret=' . print_r($ret, true)); $this->layout = isset($this->params->query['layout']) ? $this->params->query['layout'] : 'FileDevs.modal'; $this->set('tabIndex', 0); $postMaxSize = CakeNumber::fromReadableSize(ini_get('post_max_size')); $uploadMaxFilesize = CakeNumber::fromReadableSize(ini_get('upload_max_filesize')); $this->set('maxUploadSize', CakeNumber::toReadableSize($postMaxSize > $uploadMaxFilesize ? $uploadMaxFilesize : $postMaxSize)); $this->set('maxDiskSize', CakeNumber::toReadableSize(1073741824)); $this->set('useDiskSize', CakeNumber::toReadableSize(50000000)); $this->request->data = ['File' => ['role_type' => $this->params->query['roleType'] ? $this->params->query['roleType'] : ''], 'FilesPlugin' => ['plugin_key' => $this->params->query['pluginKey'] ? $this->params->query['pluginKey'] : ''], 'FilesRoom' => ['room_id' => isset($this->params->query['roomId']) ? (int) $this->params->query['roomId'] : 0], 'FilesUser' => ['user_id' => isset($this->params->query['userId']) ? (int) $this->params->query['userId'] : (int) $this->Auth->user('id')]]; $tokenFields = Hash::flatten($this->request->data); $hiddenFields = ['File.role_type', 'FilesPlugin.plugin_key', 'FilesRoom.room_id', 'FilesUser.user_id']; // $this->set('tokenFields', $tokenFields); // $this->set('hiddenFields', $hiddenFields); // $this->set('unlockField', 'File.' . FileModel::INPUT_NAME); $this->set('fileOptions', $this->request->data); $this->set('accept', $this->params->query['accept']); } }
public function filterConditions($query = null) { if ($query === null) { $request = $this->Controller->request; $query = $request->is('post') ? $request->data : $this->Controller->passedArgs; } $ignore = array('page', 'limit', 'sort', 'direction'); $conditions = array(); $query = Hash::flatten($query); foreach ($query as $k => $v) { if (strlen(trim($v)) < 1) { continue; } if (in_array($k, $ignore)) { continue; } // check for multiple searches $searches = explode(';', $v); foreach ($searches as $search) { // check for operator e.g. "like: My search term" $op = explode(':', $search); if (count($op) == 1) { $term = $op[0]; if (is_numeric($term) || is_bool($term)) { $op = 'eq'; } else { $op = 'like'; } } else { list($op, $term) = $op; } $term = trim($term); //TODO sanitize user input switch ($op) { case "like": case "%": $conditions[] = sprintf("%s LIKE '%%%s%%'", $k, $term); break; case "llike": case "l%": $conditions[] = sprintf("%s LIKE '%s%%'", $k, $term); break; case "rlike": case "r%": $conditions[] = sprintf("%s LIKE '%%%s'", $k, $term); break; case "equal": case "eq": case "==": $conditions[$k] = $term; break; } } } return $conditions; }
/** * Constructor * * @param array $error list of validation errors * @param integer $code code to report to client * @return void */ public function __construct($errors, $code = 412) { $this->_validationErrors = array_filter($errors); $flat = Hash::flatten($this->_validationErrors); $errorCount = $this->_validationErrorCount = count($flat); $this->message = __dn('crud', 'A validation error occurred', '%d validation errors occurred', $errorCount, array($errorCount)); if ($errorCount === 1) { $code = $this->_deriveRuleSpecific($this->_validationErrors, $code); } parent::__construct($this->message, $code); }
private static function correction_callback_builder($assigns, $ptn) { return function ($data) use($assigns, $ptn) { $str = implode("\n", array_filter(Hash::flatten($data), function ($value) { return is_string($value); })); preg_match_all($ptn, $str, $matches, PREG_SET_ORDER); $value = array_sum(array_map(function ($match) { return $match[2] == '-' ? -$match[3] : $match[3]; }, $matches)); return $value ? $value : null; }; }
/** * CSVファイルに追加する連想配列データ * * コンストラクタに$options['header']でカラム名が定義されてればそのカラムだけをCSVに追加する * * @param array $data モデルの連想配列データ * @return void */ public function addModelData(array $data) { $header = Hash::get($this->_options, 'header', false); if ($header) { // ヘッダ指定されてれば指定されたカラムだけ出力 $fields = array_keys($header); $line = array(); foreach ($fields as $field) { $line[] = Hash::get($data, $field, ''); } $this->add($line); } else { // ヘッダ未指定なら全カラム出力 $line = Hash::flatten($data); $this->add($line); } }
/** * Update all the settings. Validation rules are also configured here. * * @param array $data Associative array of all settings to save. * * @return boolean */ function update($data = array()) { $flat_data = Hash::flatten($data); $this->set($flat_data); if ($this->validates()) { $list = $this->find('list', array('fields' => array('key', 'id'))); foreach ($flat_data as $key => $value) { if (array_key_exists($key, $list)) { // This setting already exists in the DB, just update it. $this->id = $list[$key]; $this->saveField('value', $value); } else { // This is a new setting, add a new entry. $this->create(); $this->save(array('key' => $key, 'value' => $value)); } } return true; } return false; }
private function _array_to_csv($array) { $csv = array(); // Flatten the given array foreach ($array as $k => $item) { $csv[$k] = is_array($item) ? Set::flatten($item) : $item; } // Create the Column names. // This has to be done seperate because we don't have all of them beforehand. $csvString = array('headers' => array()); foreach ($csv as $h => $lines) { foreach ($lines as $header => $line) { if (array_search($header, $csvString['headers']) === false && !empty($header)) { $csvString['headers'][] = $header; } } } // Create the values this will also check for commas in the values foreach ($csv as $c => $lines) { $csvString['values'][$c] = ''; foreach ($csvString['headers'] as $h) { if (isset($lines[$h])) { if (is_array($lines[$h])) { Hash::flatten($lines[$h], '|'); } $csvString['values'][$c] .= is_int($lines[$h]) ? $lines[$h] . ',' : '"' . $this->_stipIllegalChars($lines[$h]) . '",'; // was a conflict, can probably delete // $csvString['values'][$c] .= is_int($lines[$h]) ? $lines[$h] . ',' : '"' . str_replace(',', '' , $lines[$h]) . '",'; } else { $csvString['values'][$c] .= ','; } } } $output = ''; foreach ($csvString['headers'] as $i => $header) { $csvString['headers'][$i] = '"' . $header . '"'; } $output .= implode(',', str_replace('.', '_', $csvString['headers'])) . "\r\n"; $output .= implode("\r\n", $csvString['values']); return $output; }
/** * Process $_FILES * * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @return void */ protected function __processFiles() { if (!empty($_FILES)) { $dimensions = Hash::dimensions($_FILES); if ($dimensions === 2) { $this->Controller->request->data = Hash::merge($this->Controller->request->data, $_FILES); } else { foreach ($_FILES as $key => $data) { $parsed = array(); foreach (array('name', 'type', 'tmp_name', 'error') as $file_key) { $flattened = Hash::flatten($_FILES[$key][$file_key]); foreach ($flattened as $flat_key => $flat_value) { $reflattened = $key . '.' . $flat_key . '.' . $file_key; $parsed[$reflattened] = $flat_value; } } $parsed = Hash::expand($parsed); $this->Controller->request->data = Hash::merge($this->Controller->request->data, $parsed); } } } }
/** * Returns an array of server specific options */ public function admin_index() { $globalOptions = $this->Option->find('all'); $serverID = Configure::read('server_id'); $serverOptions = $this->ServerOption->getServerOptions($serverID); $changedServerOptions = array(); foreach ($serverOptions as $k => $v) { $changedServerOptions[$v['ServerOption']['name']] = $v['ServerOption']['value']; } $globalOptions = Hash::flatten($globalOptions); //Replace global option values with server option values foreach ($changedServerOptions as $k => $v) { $key = array_search($k, $globalOptions, true); if ($key) { $key = explode('.', $key); $n = $key[0]; foreach ($globalOptions as $i) { $globalOptions[$n . '.Option.value'] = $v; } } } $serverOptions = Hash::expand($globalOptions); //Remove locked options foreach ($serverOptions as $k => $v) { if ($v['Option']['locked'] == 1) { unset($serverOptions[$k]); } } //pr($serverOptions); if ($this->request->is('requested')) { return $serverOptions; } else { $this->set('serverOptions', $serverOptions); } $serverName = $this->getServerName($serverID); $this->set('serverName', $serverName); return null; }
/** * A special fallback method that handles URL arrays that cannot match * any defined routes. * * @param array $url A URL that didn't match any routes * @return string A generated URL for the array * @see Router::url() */ protected static function _handleNoRoute($url) { $named = $args = array(); $skip = array_merge(array('bare', 'action', 'controller', 'plugin', 'prefix'), self::$_prefixes); $keys = array_values(array_diff(array_keys($url), $skip)); $count = count($keys); // Remove this once parsed URL parameters can be inserted into 'pass' for ($i = 0; $i < $count; $i++) { $key = $keys[$i]; if (is_numeric($keys[$i])) { $args[] = $url[$key]; } else { $named[$key] = $url[$key]; } } list($args, $named) = array(Hash::filter($args), Hash::filter($named)); foreach (self::$_prefixes as $prefix) { $prefixed = $prefix . '_'; if (!empty($url[$prefix]) && strpos($url['action'], $prefixed) === 0) { $url['action'] = substr($url['action'], strlen($prefixed) * -1); break; } } if (empty($named) && empty($args) && (!isset($url['action']) || $url['action'] === 'index')) { $url['action'] = null; } $urlOut = array_filter(array($url['controller'], $url['action'])); if (isset($url['plugin'])) { array_unshift($urlOut, $url['plugin']); } foreach (self::$_prefixes as $prefix) { if (isset($url[$prefix])) { array_unshift($urlOut, $prefix); break; } } $output = implode('/', $urlOut); if (!empty($args)) { $output .= '/' . implode('/', array_map('rawurlencode', $args)); } if (!empty($named)) { foreach ($named as $name => $value) { if (is_array($value)) { $flattend = Hash::flatten($value, '%5D%5B'); foreach ($flattend as $namedKey => $namedValue) { $output .= '/' . $name . "%5B{$namedKey}%5D" . self::$_namedConfig['separator'] . rawurlencode($namedValue); } } else { $output .= '/' . $name . self::$_namedConfig['separator'] . rawurlencode($value); } } } return $output; }
/** * Test that flattening a large complex set doesn't loop forever. * * @return void */ public function testFlattenInfiniteLoop() { $data = array('Order.ASI' => '0', 'Order.Accounting' => '0', 'Order.Admin' => '0', 'Order.Art' => '0', 'Order.ArtChecker' => '0', 'Order.Canned' => '0', 'Order.Customer_Tags' => '', 'Order.Embroidery' => '0', 'Order.Item.0.Product.style_number' => 'a11222', 'Order.Item.0.Product.slug' => 'a11222', 'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000', 'Order.Item.0.Product.Color.slug' => 'kelly_green', 'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey', 'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey', 'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green', 'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green', 'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange', 'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange', 'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze', 'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze', 'Order.Item.0.Product.brand' => 'OUTER BANKS', 'Order.Item.0.Product.style' => 'T-shirt', 'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio', 'Order.Item.0.Product.sizes.0.Size.qty' => '', 'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo', 'Order.Item.0.Product.sizes.0.Size.id' => '38', 'Order.Item.0.Product.sizes.1.Size.qty' => '', 'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo', 'Order.Item.0.Product.sizes.1.Size.id' => '39', 'Order.Item.0.Product.sizes.2.Size.qty' => '78', 'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo', 'Order.Item.0.Product.sizes.2.Size.id' => '40', 'Order.Item.0.Product.sizes.3.Size.qty' => '', 'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo', 'Order.Item.0.Product.sizes.3.Size.id' => '41', 'Order.Item.0.Product.sizes.4.Size.qty' => '', 'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo', 'Order.Item.0.Product.sizes.4.Size.id' => '42', 'Order.Item.0.Art.imprint_locations.0.id' => 2, 'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest', 'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7, 'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery', 'Order.Item.0.Art.imprint_locations.0.art' => '', 'Order.Item.0.Art.imprint_locations.0.num_colors' => 3, 'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!', 'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen', 'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo', 'Order.Item.0.Art.imprint_locations.0.height' => 4, 'Order.Item.0.Art.imprint_locations.0.width' => 5, 'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light', 'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true, 'Order.Item.0.Art.imprint_locations.1.id' => 4, 'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back', 'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6, 'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting', 'Order.Item.0.Art.imprint_locations.1.art' => '', 'Order.Item.0.Art.imprint_locations.1.num_colors' => 3, 'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!', 'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen', 'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo', 'Order.Item.0.Art.imprint_locations.2.id' => 26, 'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below', 'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9, 'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names', 'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!', 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1, 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera', 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red', 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3', 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched', 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic', 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0, 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan', 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal', 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4', 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight', 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US', 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1, 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters', 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red', 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3', 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched', 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic', 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0, 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed', 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal', 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4', 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight', 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US', 'Order.Job.0._id' => 'job-1', 'Order.Job.0.type' => 'screenprinting', 'Order.Job.0.postPress' => 'job-2', 'Order.Job.1._id' => 'job-2', 'Order.Job.1.type' => 'embroidery', 'Order.Postpress' => '0', 'Order.PriceAdjustment.0._id' => 'price-adjustment-1', 'Order.PriceAdjustment.0.adjustment' => '-20', 'Order.PriceAdjustment.0.adjustment_type' => 'percent', 'Order.PriceAdjustment.0.type' => 'grand_total', 'Order.PriceAdjustment.1.adjustment' => '20', 'Order.PriceAdjustment.1.adjustment_type' => 'flat', 'Order.PriceAdjustment.1.min-items' => '10', 'Order.PriceAdjustment.1.type' => 'min-items', 'Order.PriceAdjustment.1._id' => 'another-test-adjustment', 'Order.Purchasing' => '0', 'Order.QualityControl' => '0', 'Order.Receiving' => '0', 'Order.ScreenPrinting' => '0', 'Order.Stage.art_approval' => 0, 'Order.Stage.draft' => 1, 'Order.Stage.quote' => 1, 'Order.Stage.order' => 1, 'Order.StoreLiason' => '0', 'Order.Tag_UI_Email' => '', 'Order.Tags' => '', 'Order._id' => 'test-2', 'Order.add_print_location' => '', 'Order.created' => '2011-Dec-29 05:40:18', 'Order.force_admin' => '0', 'Order.modified' => '2012-Jul-25 01:24:49', 'Order.name' => 'towering power', 'Order.order_id' => '135961', 'Order.slug' => 'test-2', 'Order.title' => 'test job 2', 'Order.type' => 'ttt'); $expanded = Hash::expand($data); $flattened = Hash::flatten($expanded); $this->assertEquals($data, $flattened); }
/** * __setupViewParameters method * * @param array $questionnaire アンケートデータ * @param string $backUrl BACKボタン押下時の戻るパス * @return void */ private function __setupViewParameters($questionnaire, $backUrl) { if (isset($questionnaire['Questionnaire']['origin_id'])) { $isPublished = $this->Questionnaire->find('count', array('conditions' => array('is_active' => true, 'origin_id' => $questionnaire['Questionnaire']['origin_id']))); } else { $isPublished = 0; } // エラーメッセージはページ、質問、選択肢要素のそれぞれの場所に割り当てる $flatError = Hash::flatten($this->qValidationErrors); $newFlatError = array(); foreach ($flatError as $key => $val) { if (preg_match('/^(.*)\\.(.*)\\.(.*)$/', $key, $matches)) { $newFlatError[$matches[1] . '.error_messages.' . $matches[2] . '.' . $matches[3]] = $val; } } $questionnaire = Hash::merge($questionnaire, Hash::expand($newFlatError)); $this->set('jsQuestionnaire', $this->camelizeKeyRecursive($this->_changeBooleansToNumbers($this->_sorted($questionnaire)))); $this->set('backUrl', $backUrl . $this->viewVars['frameId']); $this->set('questionTypeOptions', $this->Questionnaires->getQuestionTypeOptionsWithLabel()); $this->set('newPageLabel', __d('questionnaires', 'page')); $this->set('newQuestionLabel', __d('questionnaires', 'New Question')); $this->set('newChoiceLabel', __d('questionnaires', 'new choice')); $this->set('newChoiceColumnLabel', __d('questionnaires', 'new column choice')); $this->set('newChoiceOtherLabel', __d('questionnaires', 'other choice')); $this->set('isPublished', $isPublished); }
/** * Dumps the state of Configure data into an ini formatted string. * * @param string $filename The filename on $this->_path to save into. * Extension ".ini" will be automatically appended if not included in filename. * @param array $data The data to convert to ini file. * @return int Bytes saved. */ public function dump($filename, $data) { $result = array(); foreach ($data as $key => $value) { if ($key[0] != '[') { $result[] = "[{$key}]"; } if (is_array($value)) { $keyValues = Hash::flatten($value, '.'); foreach ($keyValues as $k => $v) { $result[] = "{$k} = " . $this->_value($v); } } } $contents = join("\n", $result); if (substr($filename, -4) !== '.ini') { $filename .= '.ini'; } return file_put_contents($this->_path . $filename, $contents); }
/** * Creates an HTML link, but access the URL using the method you specify (defaults to POST). * Requires javascript to be enabled in browser. * * This method creates a `<form>` element. So do not use this method inside an existing form. * Instead you should add a submit button using FormHelper::submit() * * ### Options: * * - `data` - Array with key/value to pass in input hidden * - `method` - Request method to use. Set to 'delete' to simulate HTTP/1.1 DELETE request. Defaults to 'post'. * - `confirm` - Can be used instead of $confirmMessage. * - `inline` - Whether or not the associated form tag should be output inline. * Set to false to have the form tag appended to the 'postLink' view block. * Defaults to true. * - `block` - Choose a custom block to append the form tag to. Using this option * will override the inline option. * - Other options are the same of HtmlHelper::link() method. * - The option `onclick` will be replaced. * * @param string $title The content to be wrapped by <a> tags. * @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://) * @param array $options Array of HTML attributes. * @param boolean|string $confirmMessage JavaScript confirmation message. * @return string An `<a />` element. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::postLink */ public function postLink($title, $url = null, $options = array(), $confirmMessage = false) { $options = (array) $options + array('inline' => true, 'block' => null); if (!$options['inline'] && empty($options['block'])) { $options['block'] = __FUNCTION__; } unset($options['inline']); $requestMethod = 'POST'; if (!empty($options['method'])) { $requestMethod = strtoupper($options['method']); unset($options['method']); } if (!empty($options['confirm'])) { $confirmMessage = $options['confirm']; unset($options['confirm']); } $formName = str_replace('.', '', uniqid('post_', true)); $formUrl = $this->url($url); $formOptions = array('name' => $formName, 'id' => $formName, 'style' => 'display:none;', 'method' => 'post'); if (isset($options['target'])) { $formOptions['target'] = $options['target']; unset($options['target']); } $this->_lastAction = $formUrl; $out = $this->Html->useTag('form', $formUrl, $formOptions); $out .= $this->Html->useTag('hidden', '_method', array('value' => $requestMethod)); $out .= $this->_csrfField(); $fields = array(); if (isset($options['data']) && is_array($options['data'])) { foreach (Hash::flatten($options['data']) as $key => $value) { $fields[$key] = $value; $out .= $this->hidden($key, array('value' => $value, 'id' => false)); } unset($options['data']); } $out .= $this->secure($fields); $out .= $this->Html->useTag('formend'); if ($options['block']) { $this->_View->append($options['block'], $out); $out = ''; } unset($options['block']); $url = '#'; $onClick = 'document.' . $formName . '.submit();'; if ($confirmMessage) { $options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options); } else { $options['onclick'] = $onClick . ' '; } $options['onclick'] .= 'event.returnValue = false; return false;'; $out .= $this->Html->link($title, $url, $options); return $out; }
/** * Validates multiple individual records for a single model * * #### Options * * - atomic: If true (default), returns boolean. If false returns array. * - fieldList: Equivalent to the $fieldList parameter in Model::save() * - deep: If set to true, all associated data will be validated as well. * * Warning: This method could potentially change the passed argument `$data`, * If you do not want this to happen, make a copy of `$data` before passing it * to this method * * @param array $data Record data to validate. This should be a numerically-indexed array * @param array $options Options to use when validating record data (see above), See also $options of validates(). * @return mixed If atomic: True on success, or false on failure. * Otherwise: array similar to the $data array passed, but values are set to true/false * depending on whether each record validated successfully. */ public function validateMany(&$data, $options = array()) { $model = $this->getModel(); $options = array_merge(array('atomic' => true, 'deep' => false), $options); $model->validationErrors = $validationErrors = $return = array(); foreach ($data as $key => &$record) { if ($options['deep']) { $validates = $model->validateAssociated($record, $options); } else { $model->create(null); $validates = $model->set($record) && $model->validates($options); $data[$key] = $model->data; } if ($validates === false || is_array($validates) && in_array(false, Hash::flatten($validates), true)) { $validationErrors[$key] = $model->validationErrors; $validates = false; } else { $validates = true; } $return[$key] = $validates; } $model->validationErrors = $validationErrors; if (!$options['atomic']) { return $return; } return empty($model->validationErrors); }
/** * Dumps the state of Configure data into an ini formatted string. * * @param string $key The identifier to write to. If the key has a . it will be treated * as a plugin prefix. * @param array $data The data to convert to ini file. * @return integer Bytes saved. */ public function dump($key, $data) { $result = array(); foreach ($data as $k => $value) { $isSection = false; if ($k[0] !== '[') { $result[] = "[{$k}]"; $isSection = true; } if (is_array($value)) { $kValues = Hash::flatten($value, '.'); foreach ($kValues as $k2 => $v) { $result[] = "{$k2} = " . $this->_value($v); } } if ($isSection) { $result[] = ''; } } $contents = trim(implode("\n", $result)); $filename = $this->_getFilePath($key); return file_put_contents($filename, $contents); }
/** * adds a new ARO to the tree * * @param array $aro one or more ARO records * @return void */ public function addRole(array $aro) { foreach ($aro as $role => $inheritedRoles) { if (!isset($this->_tree[$role])) { $this->_tree[$role] = array(); } if (!empty($inheritedRoles)) { if (is_string($inheritedRoles)) { $inheritedRoles = array_map('trim', explode(',', $inheritedRoles)); } foreach ($inheritedRoles as $dependency) { // detect cycles $roles = $this->roles($dependency); if (in_array($role, Hash::flatten($roles))) { $path = ''; foreach ($roles as $roleDependencies) { $path .= implode('|', (array) $roleDependencies) . ' -> '; } trigger_error(__d('cake_dev', 'cycle detected when inheriting %s from %s. Path: %s', $role, $dependency, $path . $role)); continue; } if (!isset($this->_tree[$dependency])) { $this->_tree[$dependency] = array(); } $this->_tree[$dependency][] = $role; } } } }
/** * Collapses a multi-dimensional array into a single dimension, using a delimited array path for * each array element's key, i.e. array(array('Foo' => array('Bar' => 'Far'))) becomes * array('0.Foo.Bar' => 'Far'). * * @param array $data Array to flatten * @param string $separator String used to separate array key elements in a path, defaults to '.' * @return array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::flatten */ public static function flatten($data, $separator = '.') { return Hash::flatten($data, $separator); }
/** * find method - finds the sum of a single field * the first value speciefied in the 'fields' paramter * @param Model $Model * @param string $functionCall * @param string $state * @param array $query * @param int $sum of a field */ public function _findSum(Model $Model, $functionCall, $state, $query, $results = array()) { if ($state == 'before') { if (is_string($query['fields'])) { $query['fields'] = explode(',', $query['fields']); } if (is_array($query['fields']) && !empty($query['fields'])) { $field = array_shift($query['fields']); $field = trim($field); } if (empty($field)) { throw new OutOfBoundsException(__('Summable: findSum: Missing "fields" paramter - should have a single field to sum', true)); return false; } $field = trim(trim(trim($field), '`')); if (strpos($field, '.') !== false && strpos($field, '`') === false) { $field = str_replace('.', '`.`', $field); } $query['fields'] = array("SUM(`{$field}`)"); return $query; } // after query, check results if (!is_array($results) || empty($results)) { return 0; } // get to the value passed back (should only be one, it'll be the first $results = Hash::flatten($results); foreach ($results as $key => $value) { // checking to ensure the right field is returned (contains puts others in) if (strpos($key, 'SUM') !== false && is_numeric($value)) { return $value; } } // not found... just returning first and wishing for the best return array_shift($results); }
/** * Saves a single record, as well as all its directly associated records. * * #### Options * * - `validate`: Set to `false` to disable validation, `true` to validate each record before saving, * 'first' to validate *all* records before any are saved(default), * - `atomic`: If true (default), will attempt to save all records in a single transaction. * Should be set to false if database/table does not support transactions. * - `fieldList`: Equivalent to the $fieldList parameter in Model::save(). * It should be an associate array with model name as key and array of fields as value. Eg. * {{{ * array( * 'SomeModel' => array('field'), * 'AssociatedModel' => array('field', 'otherfield') * ) * }}} * - `deep`: If set to true, not only directly associated data is saved, but deeper nested associated data as well. * - `callbacks`: See Model::save() * - `counterCache`: See Model::save() * * @param array $data Record data to save. This should be an array indexed by association name. * @param array $options Options to use when saving record data, See $options above. * @return mixed If atomic: True on success, or false on failure. * Otherwise: array similar to the $data array passed, but values are set to true/false * depending on whether each record saved successfully. * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array */ public function saveAssociated($data = null, $options = array()) { if (empty($data)) { $data = $this->data; } $options = array_merge(array('validate' => 'first', 'atomic' => true, 'deep' => false), $options); $this->validationErrors = $validationErrors = array(); if (empty($data) && $options['validate'] !== false) { $result = $this->save($data, $options); if (!$options['atomic']) { return array(!empty($result)); } return !empty($result); } if ($options['validate'] === 'first') { $validates = $this->validateAssociated($data, $options); if (!$validates && $options['atomic'] || !$options['atomic'] && in_array(false, Hash::flatten($validates), true)) { return $validates; } $options['validate'] = false; } if ($options['atomic']) { $db = $this->getDataSource(); $transactionBegun = $db->begin(); } $associations = $this->getAssociated(); $return = array(); $validates = true; foreach ($data as $association => $values) { $notEmpty = !empty($values[$association]) || !isset($values[$association]) && !empty($values); if (isset($associations[$association]) && $associations[$association] === 'belongsTo' && $notEmpty) { $validates = $this->{$association}->create(null) !== null; $saved = false; if ($validates) { if ($options['deep']) { $saved = $this->{$association}->saveAssociated($values, array_merge($options, array('atomic' => false))); } else { $saved = $this->{$association}->save($values, array_merge($options, array('atomic' => false))); } $validates = $saved === true || is_array($saved) && !in_array(false, $saved, true); } if ($validates) { $key = $this->belongsTo[$association]['foreignKey']; if (isset($data[$this->alias])) { $data[$this->alias][$key] = $this->{$association}->id; } else { $data = array_merge(array($key => $this->{$association}->id), $data, array($key => $this->{$association}->id)); } $options = $this->_addToWhiteList($key, $options); } else { $validationErrors[$association] = $this->{$association}->validationErrors; } $return[$association] = $validates; } } if ($validates && !($this->create(null) !== null && $this->save($data, $options))) { $validationErrors[$this->alias] = $this->validationErrors; $validates = false; } $return[$this->alias] = $validates; foreach ($data as $association => $values) { if (!$validates) { break; } $notEmpty = !empty($values[$association]) || !isset($values[$association]) && !empty($values); if (isset($associations[$association]) && $notEmpty) { $type = $associations[$association]; $key = $this->{$type}[$association]['foreignKey']; switch ($type) { case 'hasOne': if (isset($values[$association])) { $values[$association][$key] = $this->id; } else { $values = array_merge(array($key => $this->id), $values, array($key => $this->id)); } $validates = $this->{$association}->create(null) !== null; $saved = false; if ($validates) { $options = $this->{$association}->_addToWhiteList($key, $options); if ($options['deep']) { $saved = $this->{$association}->saveAssociated($values, array_merge($options, array('atomic' => false))); } else { $saved = $this->{$association}->save($values, $options); } } $validates = $validates && ($saved === true || is_array($saved) && !in_array(false, $saved, true)); if (!$validates) { $validationErrors[$association] = $this->{$association}->validationErrors; } $return[$association] = $validates; break; case 'hasMany': foreach ($values as $i => $value) { if (isset($values[$i][$association])) { $values[$i][$association][$key] = $this->id; } else { $values[$i] = array_merge(array($key => $this->id), $value, array($key => $this->id)); } } $options = $this->{$association}->_addToWhiteList($key, $options); $_return = $this->{$association}->saveMany($values, array_merge($options, array('atomic' => false))); if (in_array(false, $_return, true)) { $validationErrors[$association] = $this->{$association}->validationErrors; $validates = false; } $return[$association] = $_return; break; } } } $this->validationErrors = $validationErrors; if (isset($validationErrors[$this->alias])) { $this->validationErrors = $validationErrors[$this->alias]; unset($validationErrors[$this->alias]); $this->validationErrors = array_merge($this->validationErrors, $validationErrors); } if (!$options['atomic']) { return $return; } if ($validates) { if ($transactionBegun) { return $db->commit() !== false; } return true; } $db->rollback(); return false; }
/** * Clears groups of cache engines * * @return void */ public function groups() { $output = call_user_func_array(array($this->_Cleaner, 'groups'), $this->args); $cleaned = count(Hash::flatten($output)); $this->out(__('<success>Groups cleaned:</success> %d', $cleaned), 2); foreach ($output as $group => $engines) { $this->out("\t{$group}:", 1, Shell::VERBOSE); foreach ($engines as $engine => $result) { $this->out("\t - {$engine}: " . ($result ? 'cleared' : 'error'), 1, Shell::VERBOSE); } } $this->out(null, 1, Shell::VERBOSE); }
/** * Validate submitted form * * @param Controller $controller Instantiating controller * @return bool true if submitted form is valid */ protected function _validatePost(Controller $controller) { if (empty($controller->request->data)) { return true; } $data = $controller->request->data; if (!isset($data['_Token']) || !isset($data['_Token']['fields']) || !isset($data['_Token']['unlocked'])) { return false; } $locked = ''; $check = $controller->request->data; $token = urldecode($check['_Token']['fields']); $unlocked = urldecode($check['_Token']['unlocked']); if (strpos($token, ':')) { list($token, $locked) = explode(':', $token, 2); } unset($check['_Token']); $locked = explode('|', $locked); $unlocked = explode('|', $unlocked); $lockedFields = array(); $fields = Hash::flatten($check); $fieldList = array_keys($fields); $multi = array(); foreach ($fieldList as $i => $key) { if (preg_match('/(\\.\\d+){1,10}$/', $key)) { $multi[$i] = preg_replace('/(\\.\\d+){1,10}$/', '', $key); unset($fieldList[$i]); } } if (!empty($multi)) { $fieldList += array_unique($multi); } $unlockedFields = array_unique(array_merge((array) $this->disabledFields, (array) $this->unlockedFields, $unlocked)); foreach ($fieldList as $i => $key) { $isLocked = is_array($locked) && in_array($key, $locked); if (!empty($unlockedFields)) { foreach ($unlockedFields as $off) { $off = explode('.', $off); $field = array_values(array_intersect(explode('.', $key), $off)); $isUnlocked = $field === $off; if ($isUnlocked) { break; } } } if ($isUnlocked || $isLocked) { unset($fieldList[$i]); if ($isLocked) { $lockedFields[$key] = $fields[$key]; } } } sort($unlocked, SORT_STRING); sort($fieldList, SORT_STRING); ksort($lockedFields, SORT_STRING); $fieldList += $lockedFields; $unlocked = implode('|', $unlocked); $hashParts = array($this->request->here(), serialize($fieldList), $unlocked, Configure::read('Security.salt')); $check = Security::hash(implode('', $hashParts), 'sha1'); return $token === $check; }
/** * importアクション * * @return void */ public function import() { //タイムアウトはっせいするなら適宜設定 set_time_limit(1800); $this->set('importHelp', $this->User->getCsvHeader(true)); if ($this->request->is('post')) { $file = $this->FileUpload->getTemporaryUploadFile('import_csv'); if (!$this->User->importUsers($file, $this->data['import_type'])) { //バリデーションエラーの場合 $this->set('errorMessages', Hash::flatten($this->User->validationErrors)); $this->NetCommons->handleValidationError($this->User->validationErrors); return; } $this->NetCommons->setFlashNotification(__d('net_commons', 'Successfully saved.'), array('class' => 'success')); $this->redirect('/user_manager/user_manager/index/'); } else { $this->set('errorMessages', null); } }
/** * Details * * @param array $task * @return string */ public function details(array $task) { return implode(', ', Hash::flatten($task['details'])); }
/** * _getQuestionnaires * * @param string $folderPath path string to import zip file exist * @param array $questionnaires questionnaire data in import json file * @param $importKey import key (hash string) * @return array QuestionnaireData */ protected function _getQuestionnaires($folderPath, $questionnaires, $importKey) { foreach ($questionnaires as &$q) { // id, keyはクリアする $this->Questionnaire->clearQuestionnaireId($q); // WysIsWygのデータを入れなおす $flatQuestionnaire = Hash::flatten($q); foreach ($flatQuestionnaire as $key => &$value) { $model = null; if (strpos($key, 'QuestionnaireQuestion.') !== false) { $model = $this->QuestionnaireQuestion; } else { if (strpos($key, 'QuestionnairePage.') !== false) { $model = $this->QuestionnairePage; } else { if (strpos($key, 'Questionnaire.') !== false) { $model = $this->Questionnaire; } } } if (!$model) { continue; } $columnName = substr($key, strrpos($key, '.') + 1); if ($model->hasField($columnName)) { if ($model->getColumnType($columnName) == 'text') { // keyと同じ名前のフォルダの下にあるkeyの名前のZIPファイルを渡して // その返ってきた値をこのカラムに設定 $value = $this->QuestionnairesWysIsWyg->getFromWysIsWygZIP($folderPath . DS . $key . DS . $key . '.zip', $key); } } } $q = Hash::expand($flatQuestionnaire); $q['Questionnaire']['import_key'] = $importKey; } return $questionnaires; }
/** * Converts a matching route array into a url string. Composes the string url using the template * used to create the route. * * @param array $params The params to convert to a string url. * @return string Composed route string. */ protected function _writeUrl($params) { if (isset($params['prefix'])) { $prefixed = $params['prefix'] . '_'; } if (isset($prefixed, $params['action']) && strpos($params['action'], $prefixed) === 0) { $params['action'] = substr($params['action'], strlen($prefixed) * -1); unset($params['prefix']); } if (is_array($params['pass'])) { $params['pass'] = implode('/', array_map('rawurlencode', $params['pass'])); } $namedConfig = Router::namedConfig(); $separator = $namedConfig['separator']; if (!empty($params['named']) && is_array($params['named'])) { $named = array(); foreach ($params['named'] as $key => $value) { if (is_array($value)) { $flat = Hash::flatten($value, ']['); foreach ($flat as $namedKey => $namedValue) { $named[] = $key . "[$namedKey]" . $separator . rawurlencode($namedValue); } } else { $named[] = $key . $separator . rawurlencode($value); } } $params['pass'] = $params['pass'] . '/' . implode('/', $named); } $out = $this->template; $search = $replace = array(); foreach ($this->keys as $key) { $string = null; if (isset($params[$key])) { $string = $params[$key]; } elseif (strpos($out, $key) != strlen($out) - strlen($key)) { $key .= '/'; } $search[] = ':' . $key; $replace[] = $string; } $out = str_replace($search, $replace, $out); if (strpos($this->template, '*')) { $out = str_replace('*', $params['pass'], $out); } $out = str_replace('//', '/', $out); return $out; }
/** * Validates multiple individual records for a single model * * #### Options * * - atomic: If true (default), returns boolean. If false returns array. * - fieldList: Equivalent to the $fieldList parameter in Model::save() * - deep: If set to true, all associated data will be validated as well. * * Warning: This method could potentially change the passed argument `$data`, * If you do not want this to happen, make a copy of `$data` before passing it * to this method * * @param array &$data Record data to validate. This should be a numerically-indexed array * @param array $options Options to use when validating record data (see above), See also $options of validates(). * * @return mixed If atomic: True on success, or false on failure. * Otherwise: array similar to the $data array passed, but values are set to true/false * depending on whether each record validated successfully. */ public function validateMany(&$data, $options = array()) { $model = $this->getModel(); $options += array('atomic' => TRUE, 'deep' => FALSE); $model->validationErrors = $validationErrors = $return = array(); foreach ($data as $key => &$record) { if ($options['deep']) { $validates = $model->validateAssociated($record, $options); } else { $model->create(NULL); $validates = $model->set($record) && $model->validates($options); $data[$key] = $model->data; } if ($validates === FALSE || is_array($validates) && in_array(FALSE, Hash::flatten($validates), TRUE)) { $validationErrors[$key] = $model->validationErrors; $validates = FALSE; } else { $validates = TRUE; } $return[$key] = $validates; } $model->validationErrors = $validationErrors; if (!$options['atomic']) { return $return; } return empty($model->validationErrors); }