OSDN Git Service

YAMLからProfileImport実装
authorCake <cake_67@users.sourceforge.jp>
Tue, 20 Apr 2010 14:18:39 +0000 (23:18 +0900)
committerCake <cake_67@users.sourceforge.jp>
Tue, 20 Apr 2010 14:18:39 +0000 (23:18 +0900)
app/controllers/profiles_controller.php
app/views/profiles/admin_import_profiles.ctp [new file with mode: 0644]
app/views/systems/admin_view.ctp

index 80fd970..b84c986 100644 (file)
@@ -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 (file)
index 0000000..670c096
--- /dev/null
@@ -0,0 +1,36 @@
+<div class="systems form">
+<?php echo $form->create('Profile', array(
+       'url' => array(
+               'controller' => 'profiles',
+               'action' => 'admin_import_profiles',
+               $system['System']['id']
+       ),
+       'type' => 'file',
+));?>
+<fieldset>
+<legend>
+<?php
+       echo sprintf (__('Import Profile Settings of %s', true), $system['System']['name']);?>
+</legend>
+<?php
+       echo $form->input('filename', array(
+               'type' => 'file',
+               'label' => __('Import File', true)
+       ));
+
+       echo $token->create();
+?>
+</fieldset>
+       <?php echo $form->end('Submit');?>
+</div>
+
+<div class="backButton">
+<?php
+       echo $form->create('System', array('url' => array(
+               'controller' => 'systems', 'action' => 'admin_view', $system['System']['id']),
+               'type' => 'GET',
+               'id' => 'CancelButton'
+       ));
+       echo $form->end('Cancel');
+?>
+</div>
index 6c143e8..f36e669 100644 (file)
@@ -157,7 +157,20 @@ $characterSheet['id'],
                        )
                )."]"
        );
-
+?>
+<?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']),
@@ -167,6 +180,7 @@ $characterSheet['id'],
                )."]"
        );
 ?>
+<?php endif; ?>
 </h3></div>
 <div>
 <table>
@@ -283,6 +297,16 @@ foreach ($system['Profile'] as $profile):
 <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']),
@@ -291,9 +315,11 @@ foreach ($system['Profile'] as $profile):
                )
        );
 ?>
+<?php endif; ?>
 </li>
 </ul>
 </div>
+<?php echo $html->div('', ' * '. __('Import is available only No Profiles.', true)); ?>
 </div>
 </div>