Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ImportController.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * ImportController controller class file.
11  */
13 {
14  /**
15  * Sets filter for CRUD operations in this case
16  * @return array action filters
17  */
18  public function filters()
19  {
20  return CMap::mergeArray(
21  parent::filters(),
22  array(
23  'accessControl', // perform access control for CRUD operations
24  )
25  );
26  }
27 
28  /**
29  * Specifies the access control rules.
30  * Allows access for admins only
31  * @return array access control rules
32  */
33  public function accessRules()
34  {
35  return array(
36  array('deny', // deny all users
37  'actions' => array('delete'),
38  'users' => array('*'),
39  ),
40  array('allow', // allow authenticated users to access all actions
41  'roles'=>array('Admin'),
42  ),
43  array('deny'),
44  );
45  }
46  /**
47  * Method renders a row for the table comprising the import date
48  * @return html markup with date
49  */
50  public function renderImportTableRow(&$importFile, $index, $csv_row){
51  if(!$csv_row){
52  return;
53  }
54  $user = $importFile->getUserFromCsvRow($csv_row);
55  $profile = $importFile->getProfileFromCsvRow($csv_row);
56 
57  if(isset($_POST['submit_validate'])||isset($_POST['submit_import'])){
58  importFile::overrideExisted($user,$profile);
59  }
60 
61  if(isset($_POST['submit_validate'])){
62 
63  if(!isset($importFile->valid)){
64  $importFile->valid = true;
65  }
66 
67  if(isset($_POST['enable_override'])){
68  $user->setScenario('nameIsNotUniq');
69  }
70 
71  if(!($user->getIsNewRecord() && !isset($_POST['enable_override']))){
72  $importFile->valid = $user->validate($importFile->getFieldNames()) && $importFile->valid;
73 
74  if($profile){
75  $importFile->valid = $profile->validate($importFile->getFieldNames()) && $importFile->valid;
76  }
77  }
78 
79  if(!$importFile->valid){
80  $importFile->assignError();
81  }
82  }//end submit_import || submit_validate
83 
84  if(isset($_POST['submit_import'])){
85  $importFile->valid = (isset($_POST['valid'])&&$_POST['valid']==1)? true: false;
86 
87  if($importFile->valid){
88  if($user->getIsNewRecord() || (!$user->getIsNewRecord() && isset($_POST['enable_override']))){
89 
90  $user->save(false);
91  $user->justSaved = true;
92 
93  if($profile){
94  $profile->user_id = $user->id;
95  $profile->save(false);
96  }
97  }
98  }
99  }
100 
101  return $this->renderPartial('user.views.import._import_table_row',array(
102  'importFile' => $importFile,
103  'user' => $user,
104  'profile' => $profile,
105  'index' => $index
106  ), true);
107 
108  unset($user);
109  unset($profile);
110  }
111 
112  public function actionIndex(){
113 
114  $importFile = new ImportFile;
115  if(isset($_POST['csvImportFile_hashname'])){
116  $importFile->file = $_POST['csvImportFile_realname'];
117  $importFile->hashname = $_POST['csvImportFile_hashname'];
118  }
119  /*
120  * File uploading block
121  */
122  if(isset($_POST['ImportFile'])){
123 
124  $importFile->attributes=$_POST['ImportFile'];
125  $importFile->file = CUploadedFile::getInstance($importFile,'file');
126 
127  if($importFile->validate() && !empty($importFile->file)){
128  $importFile->hashname = uniqid('import_').".csv";
129 
130  $destpath = $importFile->getPathToFile();
131  $importFile->file->saveAs($destpath);
132 
133  $importFile->file = $importFile->file->getName();
134  }
135  }
136 
137  $this->render('index', array(
138  'importFile'=>$importFile,
139  ));
140  }
141 }