Пример #1
0
 function doImportAction()
 {
     $active_worker = CerberusApplication::getActiveWorker();
     if (!$active_worker->hasPriv('crm.opp.actions.import')) {
         return;
     }
     @($pos = DevblocksPlatform::importGPC($_REQUEST['pos'], 'array', array()));
     @($field = DevblocksPlatform::importGPC($_REQUEST['field'], 'array', array()));
     @($sync_dupes = DevblocksPlatform::importGPC($_REQUEST['sync_dupes'], 'array', array()));
     @($include_first = DevblocksPlatform::importGPC($_REQUEST['include_first'], 'integer', 0));
     @($is_blank_unset = DevblocksPlatform::importGPC($_REQUEST['is_blank_unset'], 'integer', 0));
     @($opt_assign = DevblocksPlatform::importGPC($_REQUEST['opt_assign'], 'integer', 0));
     @($opt_assign_worker_id = DevblocksPlatform::importGPC($_REQUEST['opt_assign_worker_id'], 'integer', 0));
     $visit = CerberusApplication::getVisit();
     $db = DevblocksPlatform::getDatabaseService();
     $workers = DAO_Worker::getAllActive();
     $csv_file = $visit->get('crm.import.last.csv', '');
     $fp = fopen($csv_file, "rt");
     if (!$fp) {
         return;
     }
     // [JAS]: Do we need to consume a first row of headings?
     if (!$include_first) {
         @fgetcsv($fp, 8192, ',', '"');
     }
     while (!feof($fp)) {
         $parts = fgetcsv($fp, 8192, ',', '"');
         if (empty($parts) || 1 == count($parts) && is_null($parts[0])) {
             continue;
         }
         $fields = array();
         $custom_fields = array();
         $sync_fields = array();
         foreach ($pos as $idx => $p) {
             $key = $field[$idx];
             $val = $parts[$idx];
             // Special handling
             if (!empty($key)) {
                 switch ($key) {
                     case 'amount':
                         if (0 != strlen($val) && is_numeric($val)) {
                             @($val = floatval($val));
                         } else {
                             unset($key);
                         }
                         break;
                         // Translate e-mail address to ID
                     // Translate e-mail address to ID
                     case 'email':
                         if (null != ($addy = CerberusApplication::hashLookupAddress($val, true))) {
                             $key = 'primary_email_id';
                             $val = $addy->id;
                         } else {
                             unset($key);
                         }
                         break;
                         // Bools
                     // Bools
                     case 'is_won':
                     case 'is_closed':
                         if (0 != strlen($val)) {
                             @($val = !empty($val) ? 1 : 0);
                         } else {
                             unset($key);
                         }
                         break;
                         // Dates
                     // Dates
                     case 'created_date':
                     case 'updated_date':
                     case 'closed_date':
                         if (0 != strlen($val)) {
                             @($val = !is_numeric($val) ? strtotime($val) : $val);
                         } else {
                             unset($key);
                         }
                         break;
                         // Worker by name
                     // Worker by name
                     case 'worker':
                         unset($key);
                         if (is_array($workers)) {
                             foreach ($workers as $worker_id => $worker) {
                                 if (0 == strcasecmp($val, $worker->getName())) {
                                     $key = 'worker_id';
                                     $val = $worker_id;
                                 }
                             }
                         }
                         break;
                 }
                 if (!isset($key)) {
                     continue;
                 }
                 // Custom fields
                 if ('cf_' == substr($key, 0, 3)) {
                     $custom_fields[substr($key, 3)] = $val;
                 } elseif (!empty($key)) {
                     $fields[$key] = $val;
                 }
                 // Find dupe combos
                 if (in_array($idx, $sync_dupes)) {
                     $search_field = '';
                     $search_val = '';
                     switch ($key) {
                         case 'primary_email_id':
                             $search_field = SearchFields_CrmOpportunity::PRIMARY_EMAIL_ID;
                             $search_val = intval($val);
                             break;
                         case 'title':
                             $search_field = SearchFields_CrmOpportunity::NAME;
                             $search_val = $val;
                             break;
                         case 'amount':
                             $search_field = SearchFields_CrmOpportunity::AMOUNT;
                             $search_val = floatval($val);
                             break;
                         case 'is_won':
                             $search_field = SearchFields_CrmOpportunity::IS_WON;
                             $search_val = intval($val);
                             break;
                         case 'is_closed':
                             $search_field = SearchFields_CrmOpportunity::IS_CLOSED;
                             $search_val = intval($val);
                             break;
                         case 'created_date':
                             $search_field = SearchFields_CrmOpportunity::CREATED_DATE;
                             $search_val = intval($val);
                             break;
                         case 'updated_date':
                             $search_field = SearchFields_CrmOpportunity::UPDATED_DATE;
                             $search_val = intval($val);
                             break;
                         case 'closed_date':
                             $search_field = SearchFields_CrmOpportunity::CLOSED_DATE;
                             $search_val = intval($val);
                             break;
                         case 'worker_id':
                             $search_field = SearchFields_CrmOpportunity::WORKER_ID;
                             $search_val = intval($val);
                             break;
                         default:
                             // Custom field dupe
                             if ('cf_' == substr($key, 0, 3)) {
                                 $search_field = $key;
                                 // [TODO] Need to format this for proper custom fields
                                 $search_val = $val;
                             }
                             break;
                     }
                     if (!empty($search_field) && !empty($search_val)) {
                         $sync_fields[$search_field] = new DevblocksSearchCriteria($search_field, '=', $search_val);
                     }
                 }
             }
         }
         // end foreach($pos)
         // Dupe checking
         if (!empty($fields) && !empty($sync_fields)) {
             list($dupes, $null) = DAO_CrmOpportunity::search(array(), $sync_fields, 1, 0, null, false, false);
         }
         if (!empty($fields)) {
             if (isset($fields['primary_email_id'])) {
                 // Make sure a minimum amount of fields are provided
                 if (!isset($fields[DAO_CrmOpportunity::UPDATED_DATE])) {
                     $fields[DAO_CrmOpportunity::UPDATED_DATE] = time();
                 }
                 if ($opt_assign && !isset($fields[DAO_CrmOpportunity::WORKER_ID])) {
                     $fields[DAO_CrmOpportunity::WORKER_ID] = $opt_assign_worker_id;
                 }
                 if (empty($dupes)) {
                     // [TODO] Provide an import prefix for blank names
                     if (!isset($fields[DAO_CrmOpportunity::NAME]) && isset($addy)) {
                         $fields[DAO_CrmOpportunity::NAME] = $addy->email;
                     }
                     if (!isset($fields[DAO_CrmOpportunity::CREATED_DATE])) {
                         $fields[DAO_CrmOpportunity::CREATED_DATE] = time();
                     }
                     $id = DAO_CrmOpportunity::create($fields);
                 } else {
                     $id = key($dupes);
                     DAO_CrmOpportunity::update($id, $fields);
                 }
             }
         }
         if (!empty($custom_fields) && !empty($id)) {
             // Format (typecast) and set the custom field types
             $source_ext_id = CrmCustomFieldSource_Opportunity::ID;
             DAO_CustomFieldValue::formatAndSetFieldValues($source_ext_id, $id, $custom_fields, $is_blank_unset);
         }
     }
     @unlink($csv_file);
     // nuke the imported file
     $visit->set('crm.import.last.csv', null);
     DevblocksPlatform::redirect(new DevblocksHttpResponse(array('activity', 'opps')));
 }
Пример #2
0
 function doImportAction()
 {
     $active_worker = CerberusApplication::getActiveWorker();
     if (!$active_worker->hasPriv('core.addybook.import')) {
         return;
     }
     @($pos = DevblocksPlatform::importGPC($_REQUEST['pos'], 'array', array()));
     @($field = DevblocksPlatform::importGPC($_REQUEST['field'], 'array', array()));
     @($sync_column = DevblocksPlatform::importGPC($_REQUEST['sync_column'], 'string', ''));
     @($include_first = DevblocksPlatform::importGPC($_REQUEST['include_first'], 'integer', 0));
     @($is_blank_unset = DevblocksPlatform::importGPC($_REQUEST['is_blank_unset'], 'integer', 0));
     @($replace_passwords = DevblocksPlatform::importGPC($_REQUEST['replace_passwords'], 'integer', 0));
     $visit = CerberusApplication::getVisit();
     $db = DevblocksPlatform::getDatabaseService();
     $csv_file = $visit->get('import.last.csv', '');
     $type = $visit->get('import.last.type', '');
     $fp = fopen($csv_file, "rt");
     if (!$fp) {
         return;
     }
     // [JAS]: Do we need to consume a first row of headings?
     if (!$include_first) {
         @fgetcsv($fp, 8192, ',', '"');
     }
     while (!feof($fp)) {
         $parts = fgetcsv($fp, 8192, ',', '"');
         if (empty($parts) || 1 == count($parts) && is_null($parts[0])) {
             continue;
         }
         $fields = array();
         $custom_fields = array();
         $sync_field = '';
         $sync_val = '';
         // Overrides
         $contact_password = '';
         if (is_array($pos)) {
             foreach ($pos as $idx => $p) {
                 $key = $field[$idx];
                 $val = $parts[$idx];
                 if (!empty($key)) {
                     // Organizations
                     if ($type == "orgs") {
                         switch ($key) {
                             // Multi-Line
                             case 'street':
                                 @($val = isset($fields[$key]) ? $fields[$key] . ', ' . $val : $val);
                                 break;
                                 // Dates
                             // Dates
                             case 'created':
                                 @($val = !is_numeric($val) ? strtotime($val) : $val);
                                 break;
                         }
                         // Custom fields
                         if ('cf_' == substr($key, 0, 3)) {
                             $custom_fields[substr($key, 3)] = $val;
                         } else {
                             $fields[$key] = $val;
                         }
                         // Addresses
                     } elseif ($type == "addys") {
                         switch ($key) {
                             // Org (from string into id)
                             case 'contact_org_id':
                                 if (null != ($org_id = DAO_ContactOrg::lookup($val, true))) {
                                     $val = $org_id;
                                 } else {
                                     $val = 0;
                                 }
                                 break;
                             case 'pass':
                                 $key = null;
                                 // Detect if we need to MD5 a plaintext password.
                                 if (preg_match("/[a-z0-9]{32}/", $val)) {
                                     $contact_password = $val;
                                 } else {
                                     $contact_password = md5($val);
                                 }
                                 break;
                         }
                         // Custom fields
                         if ('cf_' == substr($key, 0, 3)) {
                             $custom_fields[substr($key, 3)] = $val;
                         } elseif (!empty($key)) {
                             $fields[$key] = $val;
                         }
                     }
                     if (!empty($key)) {
                         // [JAS]: Are we looking for matches in a certain field?
                         if ($sync_column == $key && !empty($val)) {
                             $sync_field = $key;
                             $sync_val = $val;
                         }
                     }
                 }
             }
         }
         if (!empty($fields)) {
             if ($type == "orgs") {
                 @($orgs = DAO_ContactOrg::getWhere(!empty($sync_field) && !empty($sync_val) ? sprintf('%s = %s', $sync_field, $db->qstr($sync_val)) : sprintf('name = %s', $db->qstr($fields['name']))));
                 if (isset($fields['name'])) {
                     if (empty($orgs)) {
                         $id = DAO_ContactOrg::create($fields);
                     } else {
                         $id = key($orgs);
                         DAO_ContactOrg::update($id, $fields);
                     }
                 }
             } elseif ($type == "addys") {
                 if (!empty($sync_field) && !empty($sync_val)) {
                     @($addys = DAO_Address::getWhere(sprintf('%s = %s', $sync_field, $db->qstr($sync_val))));
                 }
                 if (isset($fields['email'])) {
                     // Overrides
                     if (!empty($contact_password)) {
                         if ($replace_passwords) {
                             // always replace
                             $fields[DAO_Address::IS_REGISTERED] = 1;
                             $fields[DAO_Address::PASS] = $contact_password;
                         } else {
                             // only replace if null
                             if (null == ($addy = DAO_Address::lookupAddress($fields['email'], false)) || !$addy->is_registered) {
                                 $fields[DAO_Address::IS_REGISTERED] = 1;
                                 $fields[DAO_Address::PASS] = $contact_password;
                             }
                         }
                     }
                     if (empty($addys)) {
                         $id = DAO_Address::create($fields);
                     } else {
                         $id = key($addys);
                         DAO_Address::update($id, $fields);
                     }
                 }
             }
         }
         if (!empty($custom_fields) && !empty($id)) {
             // Format (typecast) and set the custom field types
             $source_ext_id = $type == "orgs" ? ChCustomFieldSource_Org::ID : ChCustomFieldSource_Address::ID;
             DAO_CustomFieldValue::formatAndSetFieldValues($source_ext_id, $id, $custom_fields, $is_blank_unset);
         }
     }
     @unlink($csv_file);
     // nuke the imported file
     $visit->set('import.last.csv', null);
     $visit->set('import.last.type', null);
     DevblocksPlatform::redirect(new DevblocksHttpResponse(array('contacts', 'import')));
 }