public function initialize(Controller $Controller) { parent::initialize($Controller); if (!self::_isSiftable($Controller)) { return; } if (empty($Controller->request->data['Sifter'])) { if (empty($Controller->request->params['named'])) { return true; } $Controller->request->params['named'] = Hash::expand($Controller->request->params['named']); return self::_sift($Controller); } $Model = $Controller->{$Controller->modelClass}; $field = array_filter((array) $Model->sifterConfig('fields')); if (self::_isAjax($Controller)) { if (!array_key_exists($Controller->request->data['Sifter']['search_field'], $field) || empty($Controller->request->data['Sifter']['search_value'])) { return true; } return self::_setAutoComplete($Controller, array($Controller->request->data['Sifter']['search_field'] => $field[$Controller->request->data['Sifter']['search_field']])); } elseif ($Controller->request->is('post')) { unset($Controller->request->data['Sifter']); $url = array(); foreach (Hash::filter($Controller->request->data) as $model => $fields) { foreach ($fields as $field => $data) { $url[$model . '.' . $field] = $data; } } return $Controller->redirect(Hash::merge($url, array_diff_key($Controller->request->params, array('plugin' => null, 'controller' => null, 'action' => null, 'named' => null, 'pass' => null)))); } }
public function compute($data, $delete = false) { $id = $this->getID(); foreach ($this->autoFields as $autoField) { $values = []; $unsetDepends0 = []; foreach ($autoField['depends0'] as $depend) { if (Hash::check($data, $depend)) { $values[$depend] = Hash::get($data, $depend); } else { if (is_array($this->data) && Hash::check($this->data, $depend)) { $values[$depend] = Hash::get($this->data, $depend); } else { $unsetDepends0[] = $depend; } } } if (!empty($unsetDepends0)) { $data = $this->find('first', ['conditions' => [$this->name . '.id' => $id], 'fields' => $unsetDepends0, 'recursive' => 0]); foreach ($unsetDepends0 as $depend) { $values[$depend] = Hash::get($data, $depend); } } foreach ($autoField['depends1'] as $modelName => $depends) { if (!is_string($modelName) || empty($depends)) { continue; } $model = $this->{$modelName}; $data = $this->{$modelName}->find('all', ['conditions' => [$modelName . '.' . Inflector::underscore($this->name) . '_id' => $id], 'fields' => $depends, 'recursive' => 0]); $values[$modelName] = Hash::extract($data, '{n}.' . $modelName); } $this->set($autoField['name'], call_user_func($autoField['callback'], Hash::expand($values))); } }
/** * WYSIWYGの初期処理 * * @param string $fieldName フィールド名("Modelname.fieldname"形式) * @param array $attributes HTML属性のオプション配列 * @return string WYSIWYGのHTML */ public function wysiwyg($fieldName, $attributes = array()) { $ngModel = Hash::expand(array($fieldName => 0)); $ngModel = NetCommonsAppController::camelizeKeyRecursive($ngModel); $ngModel = Hash::flatten($ngModel); $ngModel = array_flip($ngModel); $defaultAttributes = array('type' => 'textarea', 'ui-tinymce' => 'tinymce.options', 'ng-model' => $ngModel[0], 'rows' => 10); $attributes = Hash::merge($defaultAttributes, $attributes); // wysiwygに関連する js読み込みを Wysiwygプラグインから行う $html = ''; $html .= $this->wysiwygScript(); $html .= $this->NetCommonsForm->input($fieldName, $attributes); return $html; }
/** * 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; }
/** * Reformat `contain` array * * @param array|string $contain The value of `contain` option of the query * @return array */ private function reformatContain($contain) { // @codingStandardsIgnoreLine $result = array('options' => array(), 'contain' => array()); $contain = (array) $contain; foreach ($contain as $key => $val) { if (is_int($key)) { $key = $val; $val = array(); } if (!isset($this->containOptions[$key])) { if (strpos($key, '.') !== false) { $expanded = Hash::expand(array($key => $val)); list($key, $val) = each($expanded); } $ref =& $result['contain'][$key]; $ref = Hash::merge((array) $ref, $this->reformatContain($val)); } else { $result['options'][$key] = $val; } } return $result; }
/** * _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; }
/** * Test Set Validation Errors - Reset * * @author Wes DeMoney <*****@*****.**> * @since 1.0 * @return void */ public function testSetValidationErrorsReset() { $this->ApiResource->withFieldMap(array('username' => 'username')); $this->ApiResource->setValidationIndex(array(0, 'ApiResourceComponentThing')); $this->ApiResource->forModel('ApiResourceComponentThing')->setValidationErrors(array('username' => 'Please enter a username')); $this->ApiResource->withFieldMap(array('id' => 'id', 'fname' => 'firstName', 'lname' => 'lastName')); $this->ApiResource->setValidationIndex(array(0, 'ApiResourceComponentStuff')); $this->ApiResource->forModel('ApiResourceComponentStuff')->setValidationErrors(array('fname' => 'Please enter a first name')); $this->ApiResource->setValidationIndex(array(0, 'ApiResourceComponentThing')); // Reset $this->ApiResource->forModel('ApiResourceComponentThing')->setValidationErrors(); $this->ApiResource->withFieldMap(array('email' => 'email')); $this->ApiResource->forModel('ApiResourceComponentThing')->setValidationErrors(array('email' => 'Please enter a valid email address')); $this->assertEqual(array(array('apiResourceComponentThing' => array('email' => array('Please enter a valid email address')), 'apiResourceComponentStuff' => array('firstName' => array('Please enter a first name')))), Hash::expand($this->ApiResource->_validation_errors)); }
/** * Tests Hash::expand * * @return void */ public function testExpand() { $data = array('My', 'Array', 'To', 'Flatten'); $flat = Hash::flatten($data); $result = Hash::expand($flat); $this->assertEquals($data, $result); $data = array('0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1', '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3', '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3', '1.Author.user' => 'larry', '1.Author.password' => null); $result = Hash::expand($data); $expected = array(array('Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'), 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo')), array('Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'), 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null))); $this->assertEquals($expected, $result); $data = array('0/Post/id' => 1, '0/Post/name' => 'test post'); $result = Hash::expand($data, '/'); $expected = array(array('Post' => array('id' => 1, 'name' => 'test post'))); $this->assertEquals($result, $expected); }
/** * バリデーションエラー * * @param Model $model 呼び出しもとのModel * @param int $line 行数 * @return array */ protected function _getValidationErrors(Model $model, $line) { $flatten = Hash::flatten($model->validationErrors); foreach ($flatten as $key => $message) { $flatten[$key] = sprintf(__d('user_manager', 'Line %s: %s'), $line, $message); } return Hash::expand($flatten); }
/** * Retrieve Data for Special Fields * * @author Paul Smith <*****@*****.**> * @since 1.0 * @param Model $Model * @param array $result * @return array */ public function getSpecialFieldData(Model $Model, $result) { $special_field_data = array(); // Fill out JSON/Serialized fields as appropriate foreach (array('json', 'serialized') as $type) { if (!empty($this->settings[$Model->alias][$type])) { foreach ($this->settings[$Model->alias][$type] as $field => $sub_attributes) { if (array_key_exists($field, $result[$Model->alias])) { if ($type === 'json') { $decoded = json_decode($result[$Model->alias][$field], true); } else { $decoded = unserialize($result[$Model->alias][$field]); } if (empty($decoded)) { $special_field_data[$field] = null; } elseif (array_key_exists('field', $sub_attributes) && array_key_exists('type', $sub_attributes)) { $special_field_data[$field] = $decoded; } else { $decoded = Hash::flatten($decoded); foreach ($decoded as $key => $value) { foreach ($sub_attributes as $sub_attribute) { $strlen = strlen($sub_attribute['field']) + 1; if ($sub_attribute['field'] == $field . '.' . $key || $sub_attribute['field'] . '.' == substr($field . '.' . $key, 0, $strlen)) { $special_field_data[$field][$key] = $value; } } } if (!empty($special_field_data[$field])) { $special_field_data[$field] = Hash::expand($special_field_data[$field]); } } } } } } // Get callback field values if (!empty($this->settings[$Model->alias]['callbackFields'])) { foreach ($this->settings[$Model->alias]['callbackFields'] as $callbackFieldName => $callbackFieldAttrs) { if (!empty($this->_callbackFields) && !in_array($callbackFieldName, $this->_callbackFields)) { continue; } $function = $callbackFieldAttrs['callbackFunction']; if (!empty($callbackFieldAttrs['field'])) { $callbackFieldName = $callbackFieldAttrs['field']; } if (method_exists($this, $function)) { // If function is defined within this behavior, use that $handler = array($this, $function); array_unshift($callbackFieldAttrs['callbackParams'], $Model); } elseif (method_exists($Model, $function)) { // Otherwise use the function within the model itself $handler = array($Model, $function); } $callback_params = array_merge(compact('result'), $callbackFieldAttrs['callbackParams']); if (is_callable($handler)) { $special_field_data[$callbackFieldName] = call_user_func_array($handler, $callback_params); } } } return $special_field_data; }
/** * Test Before Validate - Unflatten Special Fields Data * * @author Everton Yoshitani <*****@*****.**> * @since 1.0 * @return void */ public function testAfterValidate() { $data = array('id' => 1, 'options.type' => 1, 'options.frequency' => '2 mins'); $this->SpecialFieldsModel->data = array($this->SpecialFieldsModel->alias => $data); $this->SpecialFieldsBehavior = $this->getMock('SpecialFieldsBehavior', array('getValidationFieldNames')); $validation_field_names = array('options'); $this->SpecialFieldsBehavior->expects($this->once())->method('getValidationFieldNames')->with($this->SpecialFieldsModel)->will($this->returnValue($validation_field_names)); $this->SpecialFieldsBehavior->afterValidate($this->SpecialFieldsModel); $expected = Hash::expand($data); $this->assertEquals($expected, $this->SpecialFieldsModel->data[$this->SpecialFieldsModel->alias]); }
private function initFiles() { $files = []; foreach (['js', 'css'] as $script) { foreach (['files', 'inline'] as $type) { $key = sprintf('%s.%s', $script, $type); $files[$key] = []; } } $this->files = Hash::expand($files); }
/** * __setupViewParameters method * * @param array $registration 登録フォームデータ * @param string $backUrl BACKボタン押下時の戻るパス * @return void */ private function __setupViewParameters($registration, $backUrl) { //$isPublished = $this->Registration->hasPublished($registration); // エラーメッセージはページ、項目、選択肢要素のそれぞれの場所に割り当てる $this->NetCommons->handleValidationError($this->Registration->validationErrors); $flatError = Hash::flatten($this->Registration->validationErrors); $newFlatError = array(); foreach ($flatError as $key => $val) { if (preg_match('/^(.*)\\.(.*)\\.(.*)$/', $key, $matches)) { $newFlatError[$matches[1] . '.error_messages.' . $matches[2] . '.' . $matches[3]] = $val; } } $registration = Hash::merge($registration, Hash::expand($newFlatError)); $registration = $this->NetCommonsTime->toUserDatetimeArray($registration, array('Registration.answer_start_period', 'Registration.answer_end_period', 'Registration.total_show_start_period')); $this->set('postUrl', array('url' => $this->_getActionUrl($this->action))); if ($this->layout == 'NetCommons.setting') { $this->set('cancelUrl', array('url' => NetCommonsUrl::backToIndexUrl('default_setting_action'))); } else { $this->set('cancelUrl', array('url' => NetCommonsUrl::backToPageUrl())); } $this->set('deleteUrl', array('url' => $this->_getActionUrl('delete'))); $this->set('questionTypeOptions', $this->Registrations->getQuestionTypeOptionsWithLabel()); $this->set('newPageLabel', __d('registrations', 'page')); $this->set('newQuestionLabel', __d('registrations', 'New Question')); $this->set('newChoiceLabel', __d('registrations', 'new choice')); $this->set('newChoiceColumnLabel', __d('registrations', 'new column choice')); $this->set('newChoiceOtherLabel', __d('registrations', 'other choice')); // 都道府県データ $prefectures = $this->_getPrefectures(); $this->set('prefectures', $prefectures); //$this->set('isPublished', $isPublished); $this->set('isPublished', false); $this->request->data = $registration; $this->request->data['Frame'] = Current::read('Frame'); $this->request->data['Block'] = Current::read('Block'); // メール通知設定 $conditions = ['plugin_key' => 'registrations', 'block_key' => Current::read('Block.key')]; $mailSetting = $this->MailSetting->find('first', ['conditions' => $conditions]); $this->set('mailSetting', $mailSetting); }
/** * Convert attributes to fields * * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @return object */ public function convertAttributesToFields() { if (empty($this->Controller->request->data) || !is_array($this->Controller->request->data)) { return $this; } if (empty($this->_model)) { return $this; } $model = $this->_model; $this->forModel(); if (!isset($this->{$model})) { $this->{$model} = ClassRegistry::init($model); } foreach ($this->Controller->request->data as $key => $model_group) { foreach ($model_group as $current_model => $save_all_data) { if (empty($this->{$model}->{$current_model}) || !is_object($this->{$model}->{$current_model})) { $modelObject = $this->{$model}; } else { $modelObject = $this->{$model}->{$current_model}; } $field_map = $modelObject->getFieldMap($modelObject->attributes()); foreach ($save_all_data as $save_all_key => $data) { $new_data = array(); foreach ($field_map as $field => $attribute) { if (array_key_exists($attribute, $data)) { $temp = Hash::expand(array($field => $data[$attribute])); $new_data = Hash::merge($new_data, $temp); } } $this->Controller->request->data[$key][$current_model][$save_all_key] = $new_data; } } } return $this; }
/** * _getRegistrations * * @param string $folderPath path string to import zip file exist * @param array $registrations registration data in import json file * @param string $importKey import key (hash string) * @return array RegistrationData */ protected function _getRegistrations($folderPath, $registrations, $importKey) { $wysiswyg = new WysiwygZip(); foreach ($registrations as &$q) { // WysIsWygのデータを入れなおす $flatRegistration = Hash::flatten($q); foreach ($flatRegistration as $key => &$value) { $model = null; if (strpos($key, 'RegistrationQuestion.') !== false) { $model = $this->RegistrationQuestion; } elseif (strpos($key, 'RegistrationPage.') !== false) { $model = $this->RegistrationPage; } elseif (strpos($key, 'Registration.') !== false) { $model = $this->Registration; } if (!$model) { continue; } $columnName = substr($key, strrpos($key, '.') + 1); if ($model->hasField($columnName)) { if ($model->getColumnType($columnName) == 'text') { // keyと同じ名前のフォルダの下にあるkeyの名前のZIPファイルを渡して // その返ってきた値をこのカラムに設定 $value = $wysiswyg->getFromWysiwygZip($folderPath . DS . $value, $model->alias . '.' . $columnName); } } } $q = Hash::expand($flatRegistration); $q['Registration']['import_key'] = $importKey; } return $registrations; }
private function read_meta() { if (!$this->unzip()) { return false; } $meta_filename = $this->content_dir . DS . 'source' . DS . 'meta.xml'; $meta_xml = file_get_contents($meta_filename); $xml = Xml::build($meta_xml); $this->meta = Xml::toArray($xml); $this->meta_user = array(); if (!empty($this->meta['document-meta']['office:meta']['meta:user-defined']['@meta:name']) == 1) { $this->meta['document-meta']['office:meta']['meta:user-defined'] = array($this->meta['document-meta']['office:meta']['meta:user-defined']); } if (empty($this->meta['document-meta']['office:meta']['meta:user-defined'])) { return false; } foreach ($this->meta['document-meta']['office:meta']['meta:user-defined'] as $m) { $cp1251 = Configure::read('App.encoding') == 'CP1251'; $name = $cp1251 ? iconv('utf-8', 'cp1251', $m['@meta:name']) : $m['@meta:name']; $type = empty($m['@meta:value-type']) ? 'string' : $m['@meta:value-type']; $value = $cp1251 ? iconv('utf-8', 'cp1251', $m['@']) : $m['@']; $this->meta_user[$name] = $value; /* * @meta:name */ } $this->meta = Hash::expand($this->meta_user); }
/** * Filter out all irrelevant datas we don't want to index * from an array * * @precondition $data is not empty * * @param Model $Model Model to index * @param array $data $data passed to save() * @return array array of data to send to elastic search for indexing */ public function filterData($Model, $data) { if ($this->settings[$Model->alias]['mapping'] === false) { return $data[$Model->alias]; } $whitelist = Hash::flatten($this->settings[$Model->alias]['mapping']); $fields = Hash::flatten($data[$Model->alias]); if (empty($whitelist)) { return array(); } $fields = array_intersect_key($fields, $whitelist); if (isset($fields['created']) && $fields['created'] instanceof MongoDate) { $fields['created'] = date('Y-m-d H:i:s', $fields['created']->sec); } if (isset($fields['modified']) && $fields['modified'] instanceof MongoDate) { $fields['modified'] = date('Y-m-d H:i:s', $fields['modified']->sec); } return Hash::expand($fields); }
/** * __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); }
/** * 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); }
/** * _createWysIsWygZIP * * @param Folder $folder * @param ZipArchive $zip * @param array $questionnaire questionnaire data * @return void */ protected function _createWysIsWygZIP($folder, $zip, &$questionnaire) { // アンケートデータの中でもWYSISWYGデータのものについては // フォルダ別に確保(フォルダの中にZIPがある) $flatQuestionnaire = Hash::flatten($questionnaire); 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') { $zip->addEmptyDir($key); $wysiswygZipFile = $this->QuestionnairesWysIsWyg->createWysIsWygZIP($folder, $key, $value); $zip->addFile($wysiswygZipFile, $key . DS . $key . '.zip'); $value = $key; } } } $questionnaire = Hash::expand($flatQuestionnaire); }
/** * Expand/unflattens an string to an array * * For example, unflattens an array that was collapsed with `Set::flatten()` * into a multi-dimensional array. So, `array('0.Foo.Bar' => 'Far')` becomes * `array(array('Foo' => array('Bar' => 'Far')))`. * * @param array $data Flattened array * @param string $separator The delimiter used * @return array */ public static function expand($data, $separator = '.') { return Hash::expand($data, $separator); }
/** * Returns Formatted Result * * @since 1.0 * @return array Formatted result array. */ public function returnsFormattedResult() { $cached = array(); if (empty($this->_result) || empty($this->_field_map)) { return $this->_result; } $primary_field_map = $this->_field_map; $this->withFieldMap(); $model = !empty($this->_model) ? $this->_model : $this->Controller->modelClass; $this->forModel(); $result = $this->_result; $is_assoc = !Hash::numeric(array_keys($result)); if ($is_assoc) { $result = array($result); } $lists = array(); $timezone = $this->Query->getTimezone(); foreach ($result as $key => $data_group) { foreach ($data_group as $data_model => $fields) { $crc32 = $data_model . ':' . is_array($fields) ? serialize($fields) : 'null'; if (array_key_exists($crc32, $cached)) { $fields = $cached[$crc32]; } if (!is_null($fields) && !array_key_exists($crc32, $cached)) { if ($model === $data_model) { $modelObject = $this->Controller->{$model}; } else { $modelObject = $this->Controller->{$model}->{$data_model}; } $attributes = $modelObject->attributes(); if ($model === $data_model) { $field_map = $primary_field_map; } else { $filtered_attributes = $this->Permissions->forModel($modelObject->name)->withAttributes($attributes)->on('read')->allowAttributes(); if (empty($filtered_attributes)) { unset($result[$key][$data_model]); continue; } $field_map = $modelObject->getFieldMap($filtered_attributes); } $is_fields_assoc = !Hash::numeric(array_keys($fields)); if ($is_fields_assoc) { $fields = array($fields); } $field_options = array(); if (!empty($fields[0]) && is_array($fields[0])) { foreach ($fields[0] as $field => $field_value) { $field_options[$field] = $modelObject->getOptions($field); } } $attribute_options = array(); if (!empty($attributes)) { foreach ($attributes as $attribute => $settings) { if (!empty($settings['values']['options'])) { $attribute_options[$attribute] = $modelObject->getOptions($settings['values']['options']); } } } foreach ($fields as $fields_key => $fields_data) { $fields[$fields_key] = $fields_data = $this->forModel($data_model)->withFieldMap($field_map)->withResult($fields_data)->returnsFieldsAsAttributes(); foreach ($fields_data as $attribute => $attribute_value) { $field = array_search($attribute, $field_map); if (!empty($field_options[$field]) && !is_array($attribute_value)) { $fields[$fields_key][$attribute] = $fields_data[$attribute] = $attribute_value = array_key_exists($attribute_value, $field_options[$field]) ? $field_options[$field][$attribute_value] : $attribute_value; } elseif (!empty($attribute_options[$attribute]) && !is_array($attribute_value)) { $fields[$fields_key][$attribute] = $fields_data[$attribute] = $attribute_value = array_key_exists($attribute_value, $attribute_options[$attribute]) ? $attribute_options[$attribute][$attribute_value] : $attribute_value; } if (empty($attributes)) { continue; } if (!array_key_exists($attribute, $attributes)) { continue; } if (empty($attributes[$attribute]['type'])) { continue; } // Convert SQL date/datetime to ISO8601 if (in_array($attributes[$attribute]['type'], array('datetime', 'date'))) { if (empty($attribute_value) || !is_string($attribute_value)) { continue; } if ($attribute_value === '0000-00-00' || $attribute_value === '0000-00-00 00:00:00') { $attribute_value = null; } else { $is_datetime = !preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}\$/", $attribute_value); $datetime = new DateTime($attribute_value); $app_timezone = date_default_timezone_get(); if (!empty($timezone) && $timezone !== $app_timezone) { $datetime->setTimezone(new DateTimeZone($timezone)); } $format = $is_datetime ? DATE_ISO8601 : 'Y-m-d'; $attribute_value = $datetime->format($format); } $fields[$fields_key][$attribute] = $fields_data[$attribute] = $attribute_value; continue; } // Convert Data Types if `DataTypeJuggling` Behaviors is loaded if ($modelObject->Behaviors->loaded('DataTypeJuggling')) { $attribute_value = $modelObject->convertDataType($attribute_value, $attributes[$attribute]['type']); $fields[$fields_key][$attribute] = $fields_data[$attribute] = $attribute_value; } } $fields[$fields_key] = Hash::expand($fields[$fields_key]); } if ($is_fields_assoc) { $fields = $fields[0]; } $cached[$crc32] = $fields; } $result[$key][$data_model] = $fields; if ($model === $data_model) { $result[$key] = array_merge($result[$key][$data_model], $result[$key]); unset($result[$key][$data_model]); } else { $result[$key][lcfirst($data_model)] = $result[$key][$data_model]; unset($result[$key][$data_model]); } } if (!empty($result[$key])) { if (array_key_exists('created', $result[$key])) { $value = $result[$key]['created']; unset($result[$key]['created']); $result[$key]['created'] = $value; } if (array_key_exists('modified', $result[$key])) { $value = $result[$key]['modified']; unset($result[$key]['modified']); $result[$key]['modified'] = $value; } } } if ($is_assoc) { $result = $result[0]; } return $result; }