Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ImportFile.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 * ImportFile model class file of uploaded file .
11  *
12  * @property string $file
13  * @property string $hashname
14  * @property string $fieldNames
15  * @property string $rowsCount
16  * @property string $hasErrors
17  * @property object $csv_file
18  * @property boolean $valid
19 */
20 class ImportFile extends CFormModel {
21  /**
22  * @var string real file name in a user filesystem
23  */
24  public $file;
25  /**
26  * @var string hash name of the file after uploading
27  */
28  public $hashname;
29  /**
30  * @var string names of columns in the uploaded file
31  */
32  protected $fieldNames;
33  /**
34  * @var string count of rows if the uploaded file
35  */
36  protected $rowsCount;
37  /**
38  * @var boolean shows if there is validation errors in the file
39  */
40  protected $hasErrors = false;
41  /**
42  * @var object of csv file
43  */
44  protected $csv_file = null;
45  /**
46  * @var boolean shows if there is validation errors in the file
47  */
48  public $valid = false;
49  /**
50  * Returns the array of rules
51  * @return array rules
52  */
53  public function rules(){
54  return array(
55  array('file', 'required'),
56  array('file', 'file', 'types'=>'csv'),
57  );
58  }
59  /**
60  * Returns the array of the column names
61  * @return array column names
62  */
63  public function getFieldNames(){
64  if(!isset($this->fieldNames)){
65  $csv_users = new CSVFile($this->getPathToFile());
66  $csv_users->rewind();
67  $this->fieldNames = $csv_users->getKeys();
68  }
69  return $this->fieldNames;
70  }
71  /**
72  * Returns the flag if there is validation errors
73  * @return boolean flag if there is errors in the file
74  */
75  public function hasErrors(){
76  return $this->hasErrors;
77  }
78  /**
79  * Set there is an error in the file
80  */
81  public function assignError(){
82  $this->hasErrors = true;
83  }
84  /**
85  * Returns the number of rows in the file
86  * @return integer the number of records
87  */
88  public function getRowsCount(){
89  if(!isset($this->rowsCount) && $this->isExist()){
90  $csv_users = new CSVFile($this->getPathToFile());
91  $csv_users->seek($csv_users->getSize());
92  $linesTotal = $csv_users->key();
93  $this->rowsCount = $linesTotal - 1;
94  }elseif(!$this->isExist()){
95  $this->rowsCount = 0;
96  }
97  return $this->rowsCount;
98  }
99  /**
100  * Returns the path to directory with uploaded files
101  * @return string path to uploaded files
102  */
103  public static function getPathToFiles(){
104  return Yii::getPathOfAlias('user.CSVImportFiles');
105  }
106  /**
107  * Returns the path to the uploaded file
108  * @return string path to uploaded file
109  */
110  public function getPathToFile(){
111  return isset($this->hashname)?(Yii::getPathOfAlias('user.CSVImportFiles').'/'.$this->hashname):false;
112  }
113  /**
114  * Returns the flag if uploaded file exists
115  * @return boolean flag the uploaded file exists
116  */
117  public function isExist(){
118  $pathToFile = $this->getPathToFile();
119  return $pathToFile? file_exists($pathToFile) : false;
120  }
121  /**
122  * Returns the model of necessary class created with values from the passed row
123  * @param object $model active record model instance
124  * @param array $csv_row an array with form fieldName=>values
125  * @return object the model with filled attributes
126  */
127  public function getFromCsvRow($model,$csv_row){
128  $intersect_to_model = array_intersect_key($csv_row, $model->attributes);
129  if(empty($intersect_to_model)){
130  return false;
131  }
132  $model->attributes = $intersect_to_model;
133 
134  return $model;
135  }
136  /**
137  * Returns the object of csv file
138  * @return object the instance of CSVFile class
139  */
140  public function getCsvFile(){
141  if($this->isExist()){
142  if(!isset($this->csv_file)){
143  $this->csv_file = new CSVFile($this->getPathToFile());
144  }
145  return $this->csv_file;
146  }else{
147  return array();
148  }
149  }
150  /**
151  * Method looking for the same records in database and uses them if them are existed
152  * @param object $user a user model instance
153  * @param object $profile a profile model instance
154  */
155  public static function overrideExisted(&$user,&$profile){
156 
157  $tmpUser = User::model()->find('email=:email',array('email'=>$user->email));
158  if($tmpUser){
159  foreach($user->attributes as $k => $attribute){
160  if(isset($attribute) && !empty($attribute)){
161  $tmpUser->setAttribute($k, $attribute);
162  }
163  }
164  $user = clone $tmpUser;
165  if(isset($scenarioNotUniq)){
166  $user->setScenario($scenarioNotUniq);
167  }
168  }
169  if($profile){
170  $tmpProfile = Profile::model()->findByPk($user->id);
171  if($tmpProfile){
172  foreach($profile->attributes as $k => $attribute){
173  if(isset($attribute) && !empty($attribute)){
174  $tmpProfile->setAttribute($k, $attribute);
175  }
176  }
177  $profile = clone $tmpProfile;
178  }
179  }
180  }
181  /**
182  * Returns an array comprising names of profiles fields
183  * @return array the names of profiles fields
184  */
185  public function getProfileFieldNames(){
186  $profile = new Profile;
187  $profilesFieldNames = array_intersect($this->getFieldNames(),array_keys($profile->attributes));
188  unset($profile);
189  return $profilesFieldNames;
190  }
191  /**
192  * Returns an array comprising names of users fields
193  * @return array the names of users fields
194  */
195  public function getUserFieldNames(){
196  $user = new User;
197  $userFieldNames = array_intersect($this->getFieldNames(),array_keys($user->attributes));
198  unset($user);
199  return $userFieldNames;
200  }
201  /**
202  * Returns the model of user
203  * @param array $csv_row an array with form fieldName=>values
204  * @return object the model with filled attributes
205  */
206  public function getUserFromCsvRow($csv_row){
207  return self::getFromCsvRow(new User('import'), $csv_row);
208  }
209  /**
210  * Returns the model of profile
211  * @param array $csv_row an array with form fieldName=>values
212  * @return object the model with filled attributes
213  */
214  public function getProfileFromCsvRow($csv_row){
215  return self::getFromCsvRow(new Profile('import'), $csv_row);
216  }
217 }