"Fossies" - the Fresh Open Source Software Archive

Member "4.6.1/db/migrations/02_step_102_datenfeldtypen.php" (8 Apr 2021, 5175 Bytes) of package /linux/www/studip-4.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 /*
    4  * 02_step_102_datenfeldtypen.php - migration for StEP00102
    5  *
    6  * Copyright (C) 2007 - Marcus Lunzenauer <mlunzena@uos.de>
    7  *
    8  * This program is free software; you can redistribute it and/or
    9  * modify it under the terms of the GNU General Public License as
   10  * published by the Free Software Foundation; either version 2 of
   11  * the License, or (at your option) any later version.
   12  */
   13 
   14 
   15 class Step102Datenfeldtypen extends Migration {
   16 
   17   function description() {
   18     return 'modify db schema for StEP00102 to provide typed datafields';
   19   }
   20 
   21   function up() {
   22     DBManager::get()->exec("ALTER TABLE `datafields` CHANGE `object_type` `object_type` enum('sem','inst','user','userinstrole','usersemdata','roleinstdata') default NULL;");
   23     DBManager::get()->exec("ALTER TABLE `datafields` CHANGE `view_perms` `view_perms` enum('all','user','autor','tutor','dozent','admin','root') default NULL;");
   24     DBManager::get()->exec("ALTER TABLE `datafields` ADD `type` enum('bool','textline','textarea','selectbox','date','time','email','url','phone', 'radio', 'combo') NOT NULL default 'textline';");
   25     DBManager::get()->exec("ALTER TABLE `datafields` ADD `typeparam` text NOT NULL;");
   26     DBManager::get()->exec("ALTER TABLE `datafields_entries` ADD `sec_range_id` varchar(32) NOT NULL default '';");
   27     DBManager::get()->exec("ALTER TABLE `datafields_entries` DROP PRIMARY KEY , ADD PRIMARY KEY ( `datafield_id` , `range_id` , `sec_range_id` );");
   28     DBManager::get()->exec("ALTER TABLE `datafields_entries` ADD INDEX `range_id` ( `range_id` , `datafield_id` );");
   29     DBManager::get()->exec("ALTER TABLE `datafields_entries` ADD INDEX `datafield_id_2` (`datafield_id`,`sec_range_id`);");
   30     DBManager::get()->exec("ALTER TABLE `statusgruppe_user` ADD `visible` tinyint(4) NOT NULL default '1';");
   31     DBManager::get()->exec("ALTER TABLE `statusgruppe_user` ADD `inherit` tinyint(4) NOT NULL default '1';");
   32     DBManager::get()->exec("CREATE TABLE `aux_lock_rules` (`lock_id` varchar( 32 ) NOT NULL default '', `name` varchar( 255 ) NOT NULL default '', `description` text NOT NULL , `attributes` text NOT NULL , `sorting` text NOT NULL , PRIMARY KEY ( `lock_id` )) ENGINE=MyISAM;");
   33     DBManager::get()->exec("ALTER TABLE `seminare` ADD `aux_lock_rule` varchar(32) default NULL;");
   34 
   35     $this->migrate_datafields();
   36   }
   37 
   38   function down() {
   39     DBManager::get()->exec("ALTER TABLE `seminare` DROP `aux_lock_rule`;");
   40     DBManager::get()->exec("DROP TABLE `aux_lock_rules`;");
   41     DBManager::get()->exec("ALTER TABLE `statusgruppe_user` DROP `inherit`;");
   42     DBManager::get()->exec("ALTER TABLE `statusgruppe_user` DROP `visible`;");
   43     DBManager::get()->exec("ALTER TABLE `datafields_entries` DROP INDEX `datafield_id_2`;");
   44     DBManager::get()->exec("ALTER TABLE `datafields_entries` DROP INDEX `range_id`;");
   45     DBManager::get()->exec("ALTER TABLE `datafields_entries` DROP PRIMARY KEY , ADD PRIMARY KEY ( `datafield_id` , `range_id` );");
   46     DBManager::get()->exec("ALTER TABLE `datafields_entries` DROP `sec_range_id`;");
   47     DBManager::get()->exec("ALTER TABLE `datafields` DROP `type`;");
   48     DBManager::get()->exec("ALTER TABLE `datafields` DROP `typeparam`;");
   49     DBManager::get()->exec("ALTER TABLE `datafields` CHANGE `view_perms` `view_perms` enum('all','user','autor','tutor','dozent','admin','root') NOT NULL default 'all';");
   50     DBManager::get()->exec("ALTER TABLE `datafields` CHANGE `object_type` `object_type` ENUM('sem','inst','user') default NULL;");
   51   }
   52 
   53   function migrate_datafields() {
   54 
   55     # only require, if exists
   56     if (!file_exists($GLOBALS['STUDIP_BASE_PATH']
   57                      . '/config/config_datafields.inc.php')) {
   58       return;
   59     }
   60     require_once 'config/config_datafields.inc.php';
   61 
   62     if (!isset($DATAFIELDS)) {
   63       return;
   64     }
   65 
   66     require_once 'lib/classes/DataFieldStructure.class.php';
   67 
   68     $ids = array_keys(DataFieldStructure::getDataFieldStructures());
   69 
   70     foreach ($DATAFIELDS as $id => $field) {
   71 
   72       if (!in_array($id, $ids)) {
   73         $this->write('Not existent: ' . $id);
   74         continue;
   75       }
   76 
   77       $struct = new DataFieldStructure(['datafield_id' => $id]);
   78 
   79       $mapping = ['text'     => 'textline',
   80                         'textarea' => 'textarea',
   81                         'checkbox' => 'bool',
   82                         'select'   => 'selectbox',
   83                         'combo'    => 'combo',
   84                         'radio'    => 'radio',
   85                         'date'     => 'date'];
   86 
   87       if (!isset($mapping[$field['type']])) {
   88         # TODO (mlunzena) what to do?
   89       }
   90 
   91       $type = $mapping[$field['type']];
   92       $type_param = '';
   93 
   94       if (in_array($type, ['selectbox', 'combo', 'radio'])) {
   95         $type_param = $this->get_type_param($field['options']);
   96       }
   97 
   98       $struct->setType($type);
   99       $struct->setTypeParam($type_param);
  100       $struct->store();
  101     }
  102   }
  103 
  104   function get_type_param($options) {
  105     $new_options = [];
  106     foreach ((array)$options as $key => $value) {
  107       if (is_string($value)) {
  108         $new_options[] = $value;
  109       }
  110       else {
  111         $new_options[] = $value['name'];
  112       }
  113     }
  114     return join("\n", $new_options);
  115   }
  116 }