Пример #1
0
 public function importContacts($form, $post)
 {
     $tmpFile = $form->getReceivedFiles();
     $model = new Addressbook();
     $importLogModel = new AddressbookImport();
     $addressbookRecipientModel = new AddressbookRecipient();
     $instance = Zend_Auth::getInstance();
     $identity = $instance->getIdentity();
     $config = Zend_Registry::get('config');
     $maxRow = $config['uploads']['maxpackage'];
     $new_path = $config['uploads']['destination'] . 'addressbook/';
     foreach ($tmpFile as $tmp) {
         if (is_file($tmp) === false) {
             throw new Logic_Exception("Temporary files does not exists!");
         } else {
             $oldInf = pathinfo($tmp);
             $new_name = uniqid();
             if (false === is_dir($new_path) && false === mkdir($new_path, 0777, true)) {
                 throw new Logic_Exception("Cannot create directory for addressbook import file!");
             }
             rename($oldInf['dirname'] . '/' . $oldInf['basename'], $new_path . $new_name);
             if (!file_exists($new_path . $new_name)) {
                 throw new Logic_Exception("Files does not exists!");
             }
             $importInfo = array('addressbook_name' => $post['addressbok_name'], 'private' => $post['is_private'] == 0 ? 'true' : 'false');
             $insertedID = $model->createImport($importInfo);
             $data = array();
             $header = $post['has_headers'] == 1 ? false : true;
             $phoneColumn = isset($post['phone_heders']) ? $post['phone_heders'] : 'phone_number';
             $emailColumn = isset($post['email_heders']) ? $post['email_heders'] : 'email_address';
             if ($fp = fopen($new_path . $new_name, 'r')) {
                 $first = false;
                 $i = 0;
                 while (!feof($fp)) {
                     $delimiter = $post['delimeter'] == 1 ? ',' : ';';
                     $row = fgetcsv($fp, filesize($new_path . $new_name), $delimiter);
                     if ($row !== FALSE) {
                         if (!$first && $header) {
                             $keys = $row;
                             $first = true;
                         } else {
                             if (!$first && !$header) {
                                 $keys = $row;
                                 $first = true;
                             }
                             $tmpArray = array_filter(array_combine($keys, $row));
                             if (!empty($tmpArray)) {
                                 if ($phoneColumn != "none") {
                                     $data[$i]['phone_number'] = $tmpArray[$phoneColumn];
                                 }
                                 if ($emailColumn != "none") {
                                     $data[$i]['email_address'] = $tmpArray[$emailColumn];
                                 }
                                 $data[$i]['ws_adressbook_id'] = $insertedID;
                             }
                             if ($i % $maxRow == 0) {
                                 $addressbookRecipientModel->MultiAdd($data);
                                 $data = array();
                                 $i = 0;
                             }
                             $i++;
                         }
                     }
                 }
                 if ($i < $maxRow) {
                     $addressbookRecipientModel->MultiAdd($data);
                 }
                 fclose($fp);
                 $importLogModel->insert(array('filename' => $new_name, 'original_filename' => $oldInf['basename'], 'created_by' => $identity->id));
             }
         }
     }
 }