// 追加アクション用 crudMap
var $actionMapPlus = array(
'admin_export_profiles' => 'read',
+ 'admin_import_profiles' => 'read',
);
var $disableTokenActions = array(
$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) {
)
)."]"
);
-
+?>
+<?php if (empty($system['Profile'])): ?>
+<?php
+ echo $html->tag('span',
+ "[".$html->link(__('Import', true),
+ array('controller' => 'profiles', 'action' => 'import_profiles', $system['System']['id']),
+ array(
+ 'class' => 'link',
+ )
+ )."]"
+ );
+?>
+<?php else: ?>
+<?php
echo $html->tag('span',
"[".$html->link(__('Export', true),
array('controller' => 'profiles', 'action' => 'export_profiles', $system['System']['id']),
)."]"
);
?>
+<?php endif; ?>
</h3></div>
<div>
<table>
<ul>
<li><?php echo $html->link(__('New Profile', true), array('controller' => 'profiles', 'action' => 'add', $system['System']['id'])); ?></li>
<li>
+<?php if (empty($system['Profile'])): ?>
+<?php
+ echo $html->link(__('Import', true),
+ array('controller' => 'profiles', 'action' => 'import_profiles', $system['System']['id']),
+ array(
+ 'class' => 'link',
+ )
+ );
+?>
+<?php else: ?>
<?php
echo $html->link(__('Export', true),
array('controller' => 'profiles', 'action' => 'export_profiles', $system['System']['id']),
)
);
?>
+<?php endif; ?>
</li>
</ul>
</div>
+<?php echo $html->div('', ' * '. __('Import is available only No Profiles.', true)); ?>
</div>
</div>