From b5b1c52c1c815f9a9d7be0f7acf5a8f2f3340ee8 Mon Sep 17 00:00:00 2001 From: Cake Date: Tue, 20 Apr 2010 23:18:39 +0900 Subject: [PATCH] =?utf8?q?YAML=E3=81=8B=E3=82=89ProfileImport=E5=AE=9F?= =?utf8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/controllers/profiles_controller.php | 125 +++++++++++++++++++++++++++ app/views/profiles/admin_import_profiles.ctp | 36 ++++++++ app/views/systems/admin_view.ctp | 28 +++++- 3 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 app/views/profiles/admin_import_profiles.ctp diff --git a/app/controllers/profiles_controller.php b/app/controllers/profiles_controller.php index 80fd970..b84c986 100644 --- a/app/controllers/profiles_controller.php +++ b/app/controllers/profiles_controller.php @@ -11,6 +11,7 @@ class ProfilesController extends AppController { // 追加アクション用 crudMap var $actionMapPlus = array( 'admin_export_profiles' => 'read', + 'admin_import_profiles' => 'read', ); var $disableTokenActions = array( @@ -234,6 +235,130 @@ class ProfilesController extends AppController { $this->set('system', $system); } + // Profiles Dataのインポート(YAML) + function admin_import_profiles($system_id = null) + { + if (!$system_id && empty($this->data)) { + $this->Session->setFlash(__('Invalid Profile', true)); + $this->redirect(array('controller' => 'systems', 'action'=>'admin_index')); + } + + $system = $this->Profile->System->find('first', array( + 'conditions' => array( + 'System.id' => $system_id, + ), + 'fields' => array( + 'System.id', + 'System.name', + ), + 'contain' => array( + 'Profile', + ), + 'recursive' => 1, + )); + + if (empty($system)) { + $this->Session->setFlash(__('No System', true)); + $this->redirect(array('controller' => 'systems', 'action'=>'admin_index')); + } + if (!empty($system['Profile'])) { + $this->Session->setFlash(__('Import is available only No Profiles.', true)); + $this->redirect(array('controller' => 'systems', 'action'=>'admin_view', $system_id)); + } + + if (!empty($this->data)) { + if (empty($this->data['Profile']['filename']['tmp_name']) || !empty($this->data['Profile']['filename']['error'])) { + $this->Session->setFlash(__('Failed to upload ImportFile.', true)); + } elseif (empty($this->data['Profile']['filename']['size']) || $this->data['Profile']['filename']['type'] != 'text/yaml') { + $this->Session->setFlash(__('Upload File is invalid. ImportFile must be valid YAML.', true)); + } else { + $yaml = file_get_contents($this->data['Profile']['filename']['tmp_name']); + // YAML読み込み + App::import('Vendor', 'Spyc'); + $profiles = Spyc::YAMLLoad($yaml); + + // Profiles Import + if (!empty($profiles)) { + foreach ($profiles as $k => $v) { + $data = array(); + // save Profile + $data['Profile'] = $v; + $data['Profile']['system_id'] = $system_id; + $data['Profile']['name'] = $this->{$this->modelClass}->restore_html($data['Profile']['name'], true); + $data['Profile']['name'] = preg_replace('/^\'(.*)\'$/', '$1', $data['Profile']['name']); + + $this->Profile->validate['key_name']['isUniqueKeyname4system'] = array( + 'rule' => array('isUniqueKeyname4system', $system_id, null), + ); + $this->Profile->set($data); + if ($this->Profile->validates()) { + $this->Profile->create(); + if (!$this->Profile->save($data, array('fieldList' => $this->Profile->fields['add']))) { + $this->Session->setFlash(sprintf(__('The Profile %s has not been saved.', true), $v['name'])); + break; + } + $profile_id = $this->Profile->id; + + // save Select + if (!empty($v['ProfileSelect'])) { + foreach($v['ProfileSelect'] as $select_key => $select) { + $data_select['ProfileSelect'] = $select; + $data_select['ProfileSelect']['profile_id'] = $profile_id; + $data_select['ProfileSelect']['value'] = $this->{$this->modelClass}->restore_html($select['value'], true); + $data_select['ProfileSelect']['value'] = preg_replace('/^\'(.*)\'$/', '$1', $data_select['ProfileSelect']['value']); + $this->Profile->ProfileSelect->create(); + if (!$this->Profile->ProfileSelect->save($data_select, array('fieldList' => $this->Profile->ProfileSelect->fields['add']))) { + $this->Session->setFlash(sprintf(__('The ProfileSelect %s has not been saved.', true), $select['value'])); + break; + } + } + } + + // save Table/StaticTable + if (!empty($v['ProfileTable'])) { + foreach($v['ProfileTable'] as $table_key => $table) { + $data_table['ProfileTable'] = $table; + $data_table['ProfileTable']['profile_id'] = $profile_id; + $data_table['ProfileTable']['title'] = $this->{$this->modelClass}->restore_html($table['title'], true); + $data_table['ProfileTable']['title'] = preg_replace('/^\'(.*)\'$/', '$1', $data_table['ProfileTable']['title']); + $this->Profile->ProfileTable->create(); + if (!$this->Profile->ProfileTable->save($data_table, array('fieldList' => $this->Profile->ProfileTable->fields['add']))) { + $this->Session->setFlash(sprintf(__('The ProfileTable %s has not been saved.', true), $table['title'])); + break; + } + + // save StaticTable + if (!empty($table['ProfileTableStatic'])) { + foreach($table['ProfileTableStatic'] as $static_key => $static) { + $data_static['ProfileTableStatic'] = $static; + $data_static['ProfileTableStatic']['profile_table_id'] = $this->Profile->ProfileTable->id; + $data_static['ProfileTableStatic']['title'] = $this->{$this->modelClass}->restore_html($static['title'], true); + $data_static['ProfileTableStatic']['title'] = preg_replace('/^\'(.*)\'$/', '$1', $data_static['ProfileTableStatic']['title']); + $this->Profile->ProfileTable->ProfileTableStatic->create(); + if (!$this->Profile->ProfileTable->ProfileTableStatic->save($data_static, array('fieldList' => $this->Profile->ProfileTable->ProfileTableStatic->fields['edit']))) { + $this->Session->setFlash(sprintf(__('The ProfileTableStatgic %s has not been saved.', true), $static['title'])); + break; + } + } + } + } + } + + } else { + $this->Session->setFlash(sprintf(__('Validate Error on The Profile %s.', true), $v['name'])); + break; + } + } + + $this->redirect(array('controller' => 'systems', 'action'=>'view', 'id' => $system_id)); + } else { + $this->Session->setFlash(__('ImportFile is not available.', true)); + } + } + } + + $this->set('system', $system); + } /* 共通関数 */ function _restore_html_profile($data, $nl2br = false) { diff --git a/app/views/profiles/admin_import_profiles.ctp b/app/views/profiles/admin_import_profiles.ctp new file mode 100644 index 0000000..670c096 --- /dev/null +++ b/app/views/profiles/admin_import_profiles.ctp @@ -0,0 +1,36 @@ +
+create('Profile', array( + 'url' => array( + 'controller' => 'profiles', + 'action' => 'admin_import_profiles', + $system['System']['id'] + ), + 'type' => 'file', +));?> +
+ + + +input('filename', array( + 'type' => 'file', + 'label' => __('Import File', true) + )); + + echo $token->create(); +?> +
+ end('Submit');?> +
+ +
+create('System', array('url' => array( + 'controller' => 'systems', 'action' => 'admin_view', $system['System']['id']), + 'type' => 'GET', + 'id' => 'CancelButton' + )); + echo $form->end('Cancel'); +?> +
diff --git a/app/views/systems/admin_view.ctp b/app/views/systems/admin_view.ctp index 6c143e8..f36e669 100644 --- a/app/views/systems/admin_view.ctp +++ b/app/views/systems/admin_view.ctp @@ -157,7 +157,20 @@ $characterSheet['id'], ) )."]" ); - +?> + +tag('span', + "[".$html->link(__('Import', true), + array('controller' => 'profiles', 'action' => 'import_profiles', $system['System']['id']), + array( + 'class' => 'link', + ) + )."]" + ); +?> + +tag('span', "[".$html->link(__('Export', true), array('controller' => 'profiles', 'action' => 'export_profiles', $system['System']['id']), @@ -167,6 +180,7 @@ $characterSheet['id'], )."]" ); ?> +
@@ -283,6 +297,16 @@ foreach ($system['Profile'] as $profile): +div('', ' * '. __('Import is available only No Profiles.', true)); ?> -- 2.11.0