Fix: Courses Import
[tine20] / tine20 / Admin / Import / Csv.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Admin
6  * @subpackage  Import
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @author      Philipp Schuele <p.schuele@metaways.de>
9  * @copyright   Copyright (c) 2009-2010 Metaways Infosystems GmbH (http://www.metaways.de)
10  *
11  */
12
13 /**
14  * Admin csv import class
15  * 
16  * @package     Admin
17  * @subpackage  Import
18  * 
19  */
20 class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
21 {
22     /**
23      * additional config options
24      * 
25      * @var array
26      */
27     protected $_additionalOptions = array(
28         'group_id'                      => '',
29         'password'                      => '',
30         'accountLoginNamePrefix'        => '',
31         'accountHomeDirectoryPrefix'    => '',
32         'accountEmailDomain'            => '',
33         'samba'                         => '',
34         'accountLoginShell'             => '',
35         'userNameSchema'                => 1,
36     );
37     
38     /**
39      * set controller
40      */
41     protected function _setController()
42     {
43         switch($this->_options['model']) {
44             case 'Tinebase_Model_FullUser':
45                 $this->_controller = Admin_Controller_User::getInstance();
46                 break;
47             default:
48                 throw new Tinebase_Exception_InvalidArgument(get_class($this) . ' needs correct model in config.');
49         }
50     }
51     
52     /**
53      * creates a new importer from an importexport definition
54      * 
55      * @param  Tinebase_Model_ImportExportDefinition $_definition
56      * @param  array                                 $_options
57      * @return Calendar_Import_Ical
58      * 
59      * @todo move this to abstract when we no longer need to be php 5.2 compatible
60      */
61     public static function createFromDefinition(Tinebase_Model_ImportExportDefinition $_definition, array $_options = array())
62     {
63         return new Admin_Import_Csv(self::getOptionsArrayFromDefinition($_definition, $_options));
64     }
65     
66     /**
67      * import single record (create password if in data)
68      *
69      * @param Tinebase_Record_Abstract $_record
70      * @param string $_resolveStrategy
71      * @param array $_recordData
72      * @return Tinebase_Record_Interface
73      * @throws Tinebase_Exception_Record_Validation
74      */
75     protected function _importRecord($_record, $_resolveStrategy = NULL, $_recordData = array())
76     {
77         if ($_record instanceof Tinebase_Model_FullUser && $this->_controller instanceof Admin_Controller_User) {
78             
79             $record = $_record;
80             
81             if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ 
82                 . ' record Data' . print_r($_recordData, true));
83             if (isset($_recordData['smtpUser'])) {
84                 $record->smtpUser = new Tinebase_Model_EmailUser($_recordData['smtpUser']);
85             }
86             if (isset($_recordData['imapUser'])) {
87                 $record->imapUser = new Tinebase_Model_EmailUser($_recordData['imapUser']);
88             }
89             if (isset($_recordData['samba']) && (!isset($this->_options['samba']) || empty($this->_options['samba']))) {
90                 $this->_options['samba'] = $_recordData['samba'];
91             }
92             if (isset($_recordData['accountHomeDirectoryPrefix'])) {
93                 $this->_options['accountHomeDirectoryPrefix'] = $_recordData['accountHomeDirectoryPrefix'];
94             }
95             
96             $password = $record->applyOptionsAndGeneratePassword($this->_options, (isset($_recordData['password'])) ? $_recordData['password'] : NULL);
97             Tinebase_Event::fireEvent(new Admin_Event_BeforeImportUser($record, $this->_options));
98             
99             // try to create record with password
100             if ($record->isValid()) {
101                 if (!$this->_options['dryrun']) {
102                     $record = $this->_controller->create($record, $password, $password);
103                 } else {
104                     $this->_importResult['results']->addRecord($record);
105                 }
106                 $this->_importResult['totalcount']++;
107             } else {
108                 Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Record invalid: ' . print_r($record->getValidationErrors(), TRUE));
109                 throw new Tinebase_Exception_Record_Validation('Imported record is invalid.');
110             }
111         } else {
112             $record = parent::_importRecord($_record, $_resolveStrategy, $_recordData);
113         }
114         return $record;
115     }
116     
117     protected function _doMapping($_data)
118     {
119         $result = parent::_doMapping($_data);
120             $result['smtpUser'] = array(
121                 'emailForwardOnly' => isset($_recordData['emailForwardOnly']) ? $_recordData['emailForwardOnly'] : true,
122                 'emailForwards'    => isset($result['emailForwards']) && !empty($result['emailForwards']) ? explode(' ', trim($result['emailForwards'])) : array(),
123                 'emailAliases'     => isset($result['emailAliases']) ? explode(' ', trim($result['emailAliases'])) : array()
124                             );
125             $result['groups'] = !empty($result['groups']) ? array_map('trim',explode(",",$result['groups'])) : array();
126             
127             $result['samba'] = array(
128                 'homePath'      => (isset($result['homePath'])) ? stripslashes($result['homePath']) : '',
129                 'homeDrive'     => (isset($result['homeDrive'])) ? stripslashes($result['homeDrive']) : '',
130                 'logonScript'   => (isset($result['logonScript'])) ? $result['logonScript'] : '',
131                 'profilePath'   => (isset($result['profilePath'])) ? stripslashes($result['profilePath']) : '',
132                 'pwdCanChange'  => isset($result['pwdCanChange'])  ? new Tinebase_DateTime($result['pwdCanChange']) : '',
133                 'pwdMustChange' => isset($result['pwdMustChange']) ? new Tinebase_DateTime($result['pwdMustChange']) : ''
134                             );
135         return $result;
136     }
137 }