File indexing completed on 2024-12-22 05:33:27
0001 <?php 0002 /** 0003 * ocs-webserver 0004 * 0005 * Copyright 2016 by pling GmbH. 0006 * 0007 * This file is part of ocs-webserver. 0008 * 0009 * This program is free software: you can redistribute it and/or modify 0010 * it under the terms of the GNU Affero General Public License as 0011 * published by the Free Software Foundation, either version 3 of the 0012 * License, or (at your option) any later version. 0013 * 0014 * This program is distributed in the hope that it will be useful, 0015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 * GNU Affero General Public License for more details. 0018 * 0019 * You should have received a copy of the GNU Affero General Public License 0020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 **/ 0022 0023 $_import_counter = 0; 0024 $_import_file_counter = 0; 0025 $_is_import_done = false; 0026 0027 class Backend_HiveuserController extends Local_Controller_Action_Backend 0028 { 0029 0030 const RESULT_OK = "OK"; 0031 const RESULT_ERROR = "ERROR"; 0032 const DATA_ID_NAME = 'id'; 0033 protected $_errorMsg = null; 0034 0035 protected $import_info; 0036 0037 0038 /** @var Default_Model_DbTable_HiveContent */ 0039 protected $_model; 0040 0041 protected $_modelName = 'Default_Model_DbTable_HiveUser'; 0042 protected $_pageTitle = 'Import Hive01 Users'; 0043 0044 public function init() 0045 { 0046 $this->_model = new $this->_modelName(); 0047 0048 $this->view->pageTitle = $this->_pageTitle; 0049 0050 parent::init(); 0051 } 0052 0053 public function indexAction() 0054 { 0055 $params = $this->getAllParams(); 0056 0057 if (empty($params['step'])) { 0058 $this->view->step = 0; 0059 } else { 0060 $this->view->step = $params['step']; 0061 } 0062 if ($this->view->step == 0) { 0063 $this->step0(); 0064 } else { 0065 if ($this->view->step == 1) { 0066 $this->step1(); 0067 } else { 0068 if ($this->view->step == 2) { 0069 $this->step2(); 0070 } else { 0071 if ($this->view->step == 3) { 0072 $this->step3(); 0073 } else { 0074 if ($this->view->step == 4) { 0075 $this->step4(); 0076 } else { 0077 if ($this->view->step == 10) { 0078 $this->step10(); 0079 } else { 0080 if ($this->view->step == 20) { 0081 $this->step20(); 0082 } else { 0083 if ($this->view->step == 30) { 0084 $this->step30(); 0085 } 0086 } 0087 } 0088 } 0089 } 0090 } 0091 } 0092 } 0093 } 0094 0095 private function step0() 0096 { 0097 $countProjects = null; 0098 $contentTable = new Default_Model_DbTable_HiveUser(); 0099 try { 0100 $count = $contentTable->fetchCountUsers(); 0101 $this->view->info = "Erfolgreich geladen aus DB"; 0102 $countProjects = $count; 0103 } catch (Exception $e) { 0104 Zend_Registry::get('logger')->info(__METHOD__ . ' - ' . "Fehler bei fetchCountProjects"); 0105 Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . print_r($e, true)); 0106 0107 $this->view->info = "Fehler bei laden aus DB:" . $e->getMessage(); 0108 $countProjects = 0; 0109 } 0110 0111 $this->view->coutAll = $countProjects; 0112 } 0113 0114 private function step1() 0115 { 0116 } 0117 0118 private function step2() 0119 { 0120 } 0121 0122 private function step3() 0123 { 0124 } 0125 0126 private function step4() 0127 { 0128 } 0129 0130 private function step10() 0131 { 0132 } 0133 0134 private function step20() 0135 { 0136 } 0137 0138 private function step30() 0139 { 0140 } 0141 0142 public function startImportAllAjaxAction() 0143 { 0144 global $_import_counter; 0145 global $_import_file_counter; 0146 global $_is_import_done; 0147 0148 $_import_counter = 0; 0149 $_import_file_counter = 0; 0150 $_is_import_done = false; 0151 0152 $this->_helper->layout->disableLayout(); 0153 $params = $this->getAllParams(); 0154 0155 $info = null; 0156 0157 $startIndex = null; 0158 $limit = intval($params['limit']); 0159 if (empty($startIndex)) { 0160 $startIndex = 0; 0161 } 0162 if (empty($limit)) { 0163 $limit = 5; 0164 } 0165 $hiveUserTable = new Default_Model_DbTable_HiveUser(); 0166 $memberTable = new Default_Model_Member(); 0167 0168 $count = $hiveUserTable->fetchCountUsers(); 0169 0170 try { 0171 $users = $hiveUserTable->fetchAllUsers($startIndex, $limit); 0172 0173 foreach ($users as $user) { 0174 $info .= " ## User: id = " . $user['id'] . ", name = " . $user['login'] . " "; 0175 $start = microtime(true); 0176 $_import_counter++; 0177 $member = $memberTable->fetchRow('source_id = 1 AND source_pk = ' . $user['id']); 0178 0179 if ($member) { 0180 //$info .= " - Update Member; "; 0181 //$info .= $this->updateMember($user); 0182 } else { 0183 $info .= " - Insert Member; "; 0184 $info .= $this->insertMember($user); 0185 } 0186 $time_elapsed_secs = microtime(true) - $start; 0187 $info .= $time_elapsed_secs . " secs"; 0188 $info .= " - Done... "; 0189 0190 //Mark user as imported 0191 $hiveUserTable->update(array("is_imported" => 1), "id = " . $user['id']); 0192 } 0193 $result['Message'] = $info; 0194 $_is_import_done = true; 0195 } catch (Exception $e) { 0196 $this->view->info = $e->getMessage(); 0197 $_is_import_done = true; 0198 0199 $result['Result'] = self::RESULT_ERROR; 0200 $result['Message'] = "Fehler bei laden aus DB:" . $e->getMessage(); 0201 } 0202 0203 $result['Result'] = self::RESULT_OK; 0204 $result['IsImportDone'] = $_is_import_done; 0205 $result['TotalCounter'] = $count; 0206 $result['ImportCounter'] = $_import_counter; 0207 $result['ImportFileCounter'] = $_import_file_counter; 0208 $result['limit'] = $limit; 0209 $result['offset'] = $startIndex + $limit; 0210 0211 $this->_helper->json($result); 0212 } 0213 0214 private function updateMember($user) 0215 { 0216 $memberTable = new Default_Model_Member(); 0217 $updatearray = $this->makeMemberFromHiveUser($user); 0218 0219 $member = $memberTable->fetchMemberFromHiveUserId($user['id']); 0220 $member_id = null; 0221 if ($member) { 0222 $member_id = $member['member_id']; 0223 $updatearray['member_id'] = $member_id; 0224 $memberTable->update($updatearray, 'member_id = ' . $member_id); 0225 } else { 0226 $member_id = $this->insertMember($user); 0227 } 0228 0229 return $member_id; 0230 } 0231 0232 private function makeMemberFromHiveUser($user) 0233 { 0234 $member = array(); 0235 $member['source_id'] = 1; 0236 $member['source_pk'] = $user['id']; 0237 $member['username'] = $user['login']; 0238 $member['mail'] = $user['email']; 0239 $member['password'] = $user['passwd']; 0240 $member['password_type'] = 1; 0241 $member['roleId'] = 300; 0242 $member['type'] = 0; 0243 $member['is_active'] = 1; 0244 $member['is_deleted'] = 0; 0245 $member['mail_checked'] = 1; 0246 $member['agb'] = 1; 0247 $member['newsletter'] = $user['newsletter']; 0248 $member['login_method'] = 'local'; 0249 $member['firstname'] = $user['firstname']; 0250 $member['lastname'] = $user['name']; 0251 $member['street'] = $user['street']; 0252 $member['zip'] = $user['zip']; 0253 $member['city'] = $user['city']; 0254 $member['country'] = $user['country']; 0255 $member['phone'] = $user['phonenumber']; 0256 $member['last_online'] = $user['last_online']; 0257 $member['biography'] = $user['description']; 0258 $member['paypal_mail'] = $user['paypalaccount']; 0259 //user pic 0260 $pic = $this->getHiveUserPicture($user['login'], $user['userdb']); 0261 if (empty($pic)) { 0262 $pic = 'hive/user-pics/nopic.png'; 0263 } 0264 $member['profile_image_url'] = $pic; 0265 $member['profile_img_src'] = 'local'; 0266 0267 $member['link_facebook'] = $user['link_facebook']; 0268 $member['link_twitter'] = $user['link_twitter']; 0269 $member['validated'] = 0; 0270 $member['created_at'] = $user['created_at']; 0271 $member['changed_at'] = $user['last_online']; 0272 0273 return $member; 0274 } 0275 0276 private function getHiveUserPicture($username, $userdb) 0277 { 0278 $imageModel = new Default_Model_DbTable_Image(); 0279 $path = 'http://cp1.hive01.com/CONTENT/user-bigpics/' . $userdb . '/' . $username . '.'; 0280 $fileFolder = 'user-bigpics'; 0281 $fileUrl = null; 0282 $fileExtention = null; 0283 if ($this->check_img($path . 'png')) { 0284 $fileUrl = ($path . 'png'); 0285 $fileExtention = 'png'; 0286 } else { 0287 if ($this->check_img($path . 'gif')) { 0288 $fileUrl = ($path . 'gif'); 0289 $fileExtention = 'gif'; 0290 } else { 0291 if ($this->check_img($path . 'jpg')) { 0292 $fileUrl = ($path . 'jpg'); 0293 $fileExtention = 'jpg'; 0294 } else { 0295 if ($this->check_img($path . 'jpeg')) { 0296 $fileUrl = ($path . 'jpeg'); 0297 $fileExtention = 'jpeg'; 0298 } 0299 } 0300 } 0301 } 0302 0303 if ($fileUrl == null) { 0304 $path = 'http://cp1.hive01.com/CONTENT/user-pics/' . $userdb . '/' . $username . '.'; 0305 $fileFolder = 'user-pics'; 0306 if ($this->check_img($path . 'png')) { 0307 $fileUrl = ($path . 'png'); 0308 $fileExtention = 'png'; 0309 } else { 0310 if ($this->check_img($path . 'gif')) { 0311 $fileUrl = ($path . 'gif'); 0312 $fileExtention = 'gif'; 0313 } else { 0314 if ($this->check_img($path . 'jpg')) { 0315 $fileUrl = ($path . 'jpg'); 0316 $fileExtention = 'jpg'; 0317 } else { 0318 if ($this->check_img($path . 'jpeg')) { 0319 $fileUrl = ($path . 'jpeg'); 0320 $fileExtention = 'jpeg'; 0321 } 0322 } 0323 } 0324 } 0325 } 0326 0327 $cnFileUrl = null; 0328 if ($fileUrl != null) { 0329 $cnFileUrl = 'hive/' . $fileFolder . '/' . $userdb . '/' . $username . '.' . $fileExtention; 0330 } 0331 0332 //var_dump($info); 0333 return $cnFileUrl; 0334 } 0335 0336 private function check_img($file) 0337 { 0338 $response = false; 0339 $x = getimagesize($file); 0340 0341 switch ($x['mime']) { 0342 case "image/gif": 0343 $response = true; 0344 break; 0345 case "image/jpeg": 0346 $response = true; 0347 break; 0348 case "image/png": 0349 $response = true; 0350 break; 0351 default: 0352 $response = false; 0353 break; 0354 } 0355 0356 return $response; 0357 } 0358 0359 private function insertMember($user) 0360 { 0361 $memberTable = new Default_Model_Member(); 0362 $projectTable = new Default_Model_Project(); 0363 $updatearray = $this->makeMemberFromHiveUser($user); 0364 //save member 0365 $member_id = $memberTable->insert($updatearray); 0366 $member = $memberTable->fetchMemberData($member_id); 0367 //save .me project 0368 $project = $this->makeProjectFromUser($member); 0369 $project_id = $projectTable->insert($project); 0370 0371 $updatearray = array(); 0372 $updatearray['main_project_id'] = $project_id; 0373 $memberTable->update($updatearray, 'member_id = ' . $member_id); 0374 0375 return $member_id; 0376 } 0377 0378 private function makeProjectFromUser($user) 0379 { 0380 $project = array(); 0381 $project['source_id'] = 1; 0382 $project['source_pk'] = $user['member_id']; 0383 $project['source_type'] = 'user'; 0384 $project['member_id'] = $user['member_id']; 0385 $project['content_type'] = 'text'; 0386 $project['project_category_id'] = 0; 0387 $project['is_active'] = 1; 0388 $project['is_deleted'] = 0; 0389 $project['status'] = 100; 0390 $project['type_id'] = 0; 0391 $project['description'] = $user['biography']; 0392 $project['created_at'] = $user['created_at']; 0393 $project['changed_at'] = new Zend_Db_Expr('Now()'); 0394 0395 return $project; 0396 } 0397 0398 private function deleteMember($user) 0399 { 0400 $memberTable = new Default_Model_Member(); 0401 $member = $memberTable->fetchMemberFromHiveUserName($user['login']); 0402 $member_id = null; 0403 if ($member) { 0404 $member_id = $member['member_id']; 0405 $memberTable->setDeleted($member_id); 0406 } 0407 0408 return true; 0409 } 0410 }