Example #1
0
 /**
  * Enter description here...
  *
  * @param CerberusParserMessage $message
  * @return integer
  */
 public static function parseMessage(CerberusParserMessage $message, $options = array())
 {
     /*
      * options:
      * 'no_autoreply'
      */
     $logger = DevblocksPlatform::getConsoleLog();
     $settings = DevblocksPlatform::getPluginSettingsService();
     $helpdesk_senders = CerberusApplication::getHelpdeskSenders();
     // Pre-parse mail filters
     $pre_filters = Model_PreParseRule::getMatches($message);
     if (is_array($pre_filters) && !empty($pre_filters)) {
         // Load filter action manifests for reuse
         $ext_action_mfts = DevblocksPlatform::getExtensions('cerberusweb.mail_filter.action', false);
         // Loop through all matching filters
         foreach ($pre_filters as $pre_filter) {
             // Do something with matching filter's actions
             foreach ($pre_filter->actions as $action_key => $action) {
                 switch ($action_key) {
                     case 'blackhole':
                         return NULL;
                         break;
                     case 'redirect':
                         @($to = $action['to']);
                         CerberusMail::reflect($message, $to);
                         return NULL;
                         break;
                     case 'bounce':
                         @($msg = $action['message']);
                         @($subject = 'Delivery failed: ' . self::fixQuotePrintableString($message->headers['subject']));
                         // [TODO] Follow the RFC spec on a true bounce
                         if (null != ($fromAddressInst = CerberusParser::getAddressFromHeaders($message->headers))) {
                             CerberusMail::quickSend($fromAddressInst->email, $subject, $msg);
                         }
                         return NULL;
                         break;
                     default:
                         // Plugin pre-parser filter actions
                         if (isset($ext_action_mfts[$action_key])) {
                             if (null != @($ext_action = $ext_action_mfts[$action_key]->createInstance())) {
                                 try {
                                     /* @var $ext_action Extension_MailFilterAction */
                                     $ext_action->run($pre_filter, $message);
                                 } catch (Exception $e) {
                                 }
                             }
                         }
                         break;
                 }
             }
         }
     }
     $headers =& $message->headers;
     // From
     if (null == ($fromAddressInst = CerberusParser::getAddressFromHeaders($headers))) {
         $logger->err("[Parser] 'From' address could not be created.");
         return NULL;
     }
     // To/Cc/Bcc
     $to = array();
     $sTo = @$headers['to'];
     $bIsNew = true;
     if (!empty($sTo)) {
         // [TODO] Do we still need this RFC address parser?
         $to = CerberusParser::parseRfcAddress($sTo);
     }
     // Subject
     // Fix quote printable subject (quoted blocks can appear anywhere in subject)
     $sSubject = "";
     if (isset($headers['subject']) && !empty($headers['subject'])) {
         $sSubject = $headers['subject'];
         if (is_array($sSubject)) {
             $sSubject = array_shift($sSubject);
         }
     }
     // The subject can still end up empty after QP decode
     if (empty($sSubject)) {
         $sSubject = "(no subject)";
     }
     // Date
     $iDate = @strtotime($headers['date']);
     // If blank, or in the future, set to the current date
     if (empty($iDate) || $iDate > time()) {
         $iDate = time();
     }
     // Is banned?
     if (1 == $fromAddressInst->is_banned) {
         $logger->info("[Parser] Ignoring ticket from banned address: " . $fromAddressInst->email);
         return NULL;
     }
     // Overloadable
     $enumSpamTraining = '';
     // Message Id / References / In-Reply-To
     @($sMessageId = $headers['message-id']);
     $body_append_text = array();
     $body_append_html = array();
     // [mdf]Check attached files before creating the ticket because we may need to overwrite the message-id
     // also store any contents of rfc822 files so we can include them after the body
     foreach ($message->files as $filename => $file) {
         /* @var $file ParserFile */
         switch ($file->mime_type) {
             case 'message/rfc822':
                 $full_filename = $file->tmpname;
                 $mail = mailparse_msg_parse_file($full_filename);
                 $struct = mailparse_msg_get_structure($mail);
                 $msginfo = mailparse_msg_get_part_data($mail);
                 $inline_headers = $msginfo['headers'];
                 if (isset($headers['from']) && (strtolower(substr($headers['from'], 0, 11)) == 'postmaster@' || strtolower(substr($headers['from'], 0, 14)) == 'mailer-daemon@')) {
                     $headers['in-reply-to'] = $inline_headers['message-id'];
                 }
                 break;
         }
     }
     // [JAS] [TODO] References header may contain multiple message-ids to find
     if (null != ($ids = self::findParentMessage($headers))) {
         $bIsNew = false;
         $id = $ids['ticket_id'];
         $msgid = $ids['message_id'];
         // Is it a worker reply from an external client?  If so, proxy
         if (null != ($worker_address = DAO_AddressToWorker::getByAddress($fromAddressInst->email))) {
             $logger->info("[Parser] Handling an external worker response from " . $fromAddressInst->email);
             if (!DAO_Ticket::isTicketRequester($worker_address->address, $id)) {
                 // Watcher Commands [TODO] Document on wiki/etc
                 if (0 != ($matches = preg_match_all("/\\[(.*?)\\]/i", $message->headers['subject'], $commands))) {
                     @($command = strtolower(array_pop($commands[1])));
                     $logger->info("[Parser] Worker command: " . $command);
                     switch ($command) {
                         case 'close':
                             DAO_Ticket::updateTicket($id, array(DAO_Ticket::IS_CLOSED => CerberusTicketStatus::CLOSED));
                             break;
                         case 'take':
                             DAO_Ticket::updateTicket($id, array(DAO_Ticket::NEXT_WORKER_ID => $worker_address->worker_id));
                             break;
                         case 'comment':
                             $comment_id = DAO_TicketComment::create(array(DAO_TicketComment::ADDRESS_ID => $fromAddressInst->id, DAO_TicketComment::CREATED => time(), DAO_TicketComment::TICKET_ID => $id, DAO_TicketComment::COMMENT => $message->body));
                             return $id;
                             break;
                         default:
                             // Typo?
                             break;
                     }
                 }
                 $attachment_files = array();
                 $attachment_files['name'] = array();
                 $attachment_files['type'] = array();
                 $attachment_files['tmp_name'] = array();
                 $attachment_files['size'] = array();
                 $i = 0;
                 foreach ($message->files as $filename => $file) {
                     $attachment_files['name'][$i] = $filename;
                     $attachment_files['type'][$i] = $file->mime_type;
                     $attachment_files['tmp_name'][$i] = $file->tmpname;
                     $attachment_files['size'][$i] = $file->file_size;
                     $i++;
                 }
                 CerberusMail::sendTicketMessage(array('message_id' => $msgid, 'content' => $message->body, 'files' => $attachment_files, 'agent_id' => $worker_address->worker_id));
                 return $id;
             } else {
                 // ... worker is a requester, treat as normal
                 $logger->info("[Parser] The external worker was a ticket requester, so we're not treating them as a watcher.");
             }
         } else {
             // Reply: Not sent by a worker
             /*
              * [TODO] check that this sender is a requester on the matched ticket
              * Otherwise blank out the $id
              */
         }
     }
     $group_id = 0;
     if (empty($id)) {
         // New Ticket
         $sMask = CerberusApplication::generateTicketMask();
         $groups = DAO_Group::getAll();
         // Routing new tickets
         if (null != ($routing_rules = Model_MailToGroupRule::getMatches($fromAddressInst, $message))) {
             if (is_array($routing_rules)) {
                 foreach ($routing_rules as $rule) {
                     // Only end up with the last 'move' action (ignore the previous)
                     if (isset($rule->actions['move'])) {
                         $group_id = intval($rule->actions['move']['group_id']);
                         // We don't need to move again when running rule actions
                         unset($rule->actions['move']);
                     }
                 }
             }
         }
         // Make sure the group exists
         if (!isset($groups[$group_id])) {
             $group_id = null;
         }
         // Last ditch effort to check for a default group to deliver to
         if (empty($group_id)) {
             if (null != ($default_team = DAO_Group::getDefaultGroup())) {
                 $group_id = $default_team->id;
             } else {
                 // Bounce
                 return null;
             }
         }
         // [JAS] It's important to not set the group_id on the ticket until the messages exist
         // or inbox filters will just abort.
         $fields = array(DAO_Ticket::MASK => $sMask, DAO_Ticket::SUBJECT => $sSubject, DAO_Ticket::IS_CLOSED => 0, DAO_Ticket::FIRST_WROTE_ID => intval($fromAddressInst->id), DAO_Ticket::LAST_WROTE_ID => intval($fromAddressInst->id), DAO_Ticket::CREATED_DATE => $iDate, DAO_Ticket::UPDATED_DATE => $iDate, DAO_Ticket::LAST_ACTION_CODE => CerberusTicketActionCode::TICKET_OPENED);
         $id = DAO_Ticket::createTicket($fields);
         // Apply routing actions to our new ticket ID
         if (isset($routing_rules) && is_array($routing_rules)) {
             foreach ($routing_rules as $rule) {
                 $rule->run($id);
             }
         }
     }
     // [JAS]: Add requesters to the ticket
     if (!empty($fromAddressInst->id) && !empty($id)) {
         // Don't add a requester if the sender is a helpdesk address
         if (isset($helpdesk_senders[$fromAddressInst->email])) {
             $logger->info("[Parser] Not adding ourselves as a requester: " . $fromAddressInst->email);
         } else {
             DAO_Ticket::createRequester($fromAddressInst->id, $id);
         }
     }
     // Add the other TO/CC addresses to the ticket
     // [TODO] This should be cleaned up and optimized
     if ($settings->get('cerberusweb.core', CerberusSettings::PARSER_AUTO_REQ, 0)) {
         @($autoreq_exclude_list = $settings->get('cerberusweb.core', CerberusSettings::PARSER_AUTO_REQ_EXCLUDE, ''));
         $destinations = self::getDestinations($headers);
         if (is_array($destinations) && !empty($destinations)) {
             // Filter out any excluded requesters
             if (!empty($autoreq_exclude_list)) {
                 @($autoreq_exclude = DevblocksPlatform::parseCrlfString($autoreq_exclude_list));
                 if (is_array($autoreq_exclude) && !empty($autoreq_exclude)) {
                     foreach ($autoreq_exclude as $excl_pattern) {
                         $excl_regexp = DevblocksPlatform::parseStringAsRegExp($excl_pattern);
                         // Check all destinations for this pattern
                         foreach ($destinations as $idx => $dest) {
                             if (@preg_match($excl_regexp, $dest)) {
                                 unset($destinations[$idx]);
                             }
                         }
                     }
                 }
             }
             foreach ($destinations as $dest) {
                 if (null != ($destInst = CerberusApplication::hashLookupAddress($dest, true))) {
                     // Skip if the destination is one of our senders or the matching TO
                     if (isset($helpdesk_senders[$destInst->email])) {
                         continue;
                     }
                     DAO_Ticket::createRequester($destInst->id, $id);
                 }
             }
         }
     }
     $attachment_path = APP_STORAGE_PATH . '/attachments/';
     // [TODO] This should allow external attachments (S3)
     $fields = array(DAO_Message::TICKET_ID => $id, DAO_Message::CREATED_DATE => $iDate, DAO_Message::ADDRESS_ID => $fromAddressInst->id);
     $email_id = DAO_Message::create($fields);
     // Content
     DAO_MessageContent::create($email_id, $message->body);
     // Headers
     foreach ($headers as $hk => $hv) {
         DAO_MessageHeader::create($email_id, $hk, $hv);
     }
     // [mdf] Loop through files to insert attachment records in the db, and move temporary files
     if (!empty($email_id)) {
         foreach ($message->files as $filename => $file) {
             /* @var $file ParserFile */
             //[mdf] skip rfc822 messages since we extracted their content above
             if ($file->mime_type == 'message/rfc822') {
                 continue;
             }
             $fields = array(DAO_Attachment::MESSAGE_ID => $email_id, DAO_Attachment::DISPLAY_NAME => $filename, DAO_Attachment::MIME_TYPE => $file->mime_type, DAO_Attachment::FILE_SIZE => intval($file->file_size));
             $file_id = DAO_Attachment::create($fields);
             if (empty($file_id)) {
                 @unlink($file->tmpname);
                 // remove our temp file
                 continue;
             }
             // Make file attachments use buckets so we have a max per directory
             $attachment_bucket = sprintf("%03d/", mt_rand(1, 100));
             $attachment_file = $file_id;
             if (!file_exists($attachment_path . $attachment_bucket)) {
                 @mkdir($attachment_path . $attachment_bucket, 0770, true);
                 // [TODO] Needs error checking
             }
             rename($file->getTempFile(), $attachment_path . $attachment_bucket . $attachment_file);
             // [TODO] Split off attachments into its own DAO
             DAO_Attachment::update($file_id, array(DAO_Attachment::FILEPATH => $attachment_bucket . $attachment_file));
         }
     }
     // Pre-load custom fields
     if (isset($message->custom_fields) && !empty($message->custom_fields)) {
         foreach ($message->custom_fields as $cf_id => $cf_val) {
             if (is_array($cf_val) && !empty($cf_val) || !is_array($cf_val) && 0 != strlen($cf_val)) {
                 DAO_CustomFieldValue::setFieldValue('cerberusweb.fields.source.ticket', $id, $cf_id, $cf_val);
             }
         }
     }
     // Finalize our new ticket details (post-message creation)
     if ($bIsNew && !empty($id) && !empty($email_id)) {
         // First thread (needed for anti-spam)
         DAO_Ticket::updateTicket($id, array(DAO_Ticket::FIRST_MESSAGE_ID => $email_id));
         // Prime the change fields (which a few things like anti-spam might change before we commit)
         $change_fields = array(DAO_Ticket::TEAM_ID => $group_id);
         $out = CerberusBayes::calculateTicketSpamProbability($id);
         if (!empty($group_id)) {
             @($spam_threshold = DAO_GroupSettings::get($group_id, DAO_GroupSettings::SETTING_SPAM_THRESHOLD, 80));
             @($spam_action = DAO_GroupSettings::get($group_id, DAO_GroupSettings::SETTING_SPAM_ACTION, ''));
             @($spam_action_param = DAO_GroupSettings::get($group_id, DAO_GroupSettings::SETTING_SPAM_ACTION_PARAM, ''));
             if ($out['probability'] * 100 >= $spam_threshold) {
                 $enumSpamTraining = CerberusTicketSpamTraining::SPAM;
                 switch ($spam_action) {
                     default:
                     case 0:
                         // do nothing
                         break;
                     case 1:
                         // delete
                         $change_fields[DAO_Ticket::IS_CLOSED] = 1;
                         $change_fields[DAO_Ticket::IS_DELETED] = 1;
                         break;
                     case 2:
                         // move
                         $buckets = DAO_Bucket::getAll();
                         // Verify bucket exists
                         if (!empty($spam_action_param) && isset($buckets[$spam_action_param])) {
                             $change_fields[DAO_Ticket::TEAM_ID] = $group_id;
                             $change_fields[DAO_Ticket::CATEGORY_ID] = $spam_action_param;
                         }
                         break;
                 }
             }
         }
         // end spam training
         // Save properties
         if (!empty($change_fields)) {
             DAO_Ticket::updateTicket($id, $change_fields);
         }
     }
     // Reply notifications (new messages are handled by 'move' listener)
     if (!$bIsNew) {
         // Inbound Reply Event
         $eventMgr = DevblocksPlatform::getEventService();
         $eventMgr->trigger(new Model_DevblocksEvent('ticket.reply.inbound', array('ticket_id' => $id)));
     }
     // New ticket processing
     if ($bIsNew) {
         // Auto reply
         @($autoreply_enabled = DAO_GroupSettings::get($group_id, DAO_GroupSettings::SETTING_AUTO_REPLY_ENABLED, 0));
         @($autoreply = DAO_GroupSettings::get($group_id, DAO_GroupSettings::SETTING_AUTO_REPLY, ''));
         /*
          * Send the group's autoreply if one exists, as long as this ticket isn't spam
          */
         if (!isset($options['no_autoreply']) && $autoreply_enabled && !empty($autoreply) && $enumSpamTraining != CerberusTicketSpamTraining::SPAM) {
             CerberusMail::sendTicketMessage(array('ticket_id' => $id, 'message_id' => $email_id, 'content' => str_replace(array('#ticket_id#', '#mask#', '#subject#', '#timestamp#', '#sender#', '#sender_first#', '#orig_body#'), array($id, $sMask, $sSubject, date('r'), $fromAddressInst->email, $fromAddressInst->first_name, ltrim($message->body)), $autoreply), 'is_autoreply' => true, 'dont_keep_copy' => true));
         }
     }
     // end bIsNew
     unset($message);
     // Re-open and update our date on new replies
     if (!$bIsNew) {
         DAO_Ticket::updateTicket($id, array(DAO_Ticket::UPDATED_DATE => time(), DAO_Ticket::IS_WAITING => 0, DAO_Ticket::IS_CLOSED => 0, DAO_Ticket::IS_DELETED => 0, DAO_Ticket::LAST_WROTE_ID => $fromAddressInst->id, DAO_Ticket::LAST_ACTION_CODE => CerberusTicketActionCode::TICKET_CUSTOMER_REPLY));
         // [TODO] The TICKET_CUSTOMER_REPLY should be sure of this message address not being a worker
     }
     @imap_errors();
     // Prevent errors from spilling out into STDOUT
     return $id;
 }
Example #2
0
 function doContactSendAction()
 {
     @($sFrom = DevblocksPlatform::importGPC($_POST['from'], 'string', ''));
     @($sSubject = DevblocksPlatform::importGPC($_POST['subject'], 'string', ''));
     @($sContent = DevblocksPlatform::importGPC($_POST['content'], 'string', ''));
     @($sCaptcha = DevblocksPlatform::importGPC($_POST['captcha'], 'string', ''));
     @($aFieldIds = DevblocksPlatform::importGPC($_POST['field_ids'], 'array', array()));
     @($aFollowUpQ = DevblocksPlatform::importGPC($_POST['followup_q'], 'array', array()));
     // Load the answers to any situational questions
     $aFollowUpA = array();
     if (is_array($aFollowUpQ)) {
         foreach ($aFollowUpQ as $idx => $q) {
             @($answer = DevblocksPlatform::importGPC($_POST['followup_a_' . $idx], 'string', ''));
             $aFollowUpA[$idx] = $answer;
         }
     }
     $umsession = $this->getSession();
     $fingerprint = parent::getFingerprint();
     $settings = CerberusSettings::getInstance();
     $default_from = $settings->get(CerberusSettings::DEFAULT_REPLY_FROM);
     $umsession->setProperty('support.write.last_from', $sFrom);
     $umsession->setProperty('support.write.last_subject', $sSubject);
     $umsession->setProperty('support.write.last_content', $sContent);
     //		$umsession->setProperty('support.write.last_followup_q',$aFollowUpQ);
     $umsession->setProperty('support.write.last_followup_a', $aFollowUpA);
     $sNature = $umsession->getProperty('support.write.last_nature', '');
     $captcha_enabled = DAO_CommunityToolProperty::get($this->getPortal(), UmScApp::PARAM_CAPTCHA_ENABLED, 1);
     if (empty($sFrom) || $captcha_enabled && 0 != strcasecmp($sCaptcha, @$umsession->getProperty(UmScApp::SESSION_CAPTCHA, '***'))) {
         if (empty($sFrom)) {
             $umsession->setProperty('support.write.last_error', 'Invalid e-mail address.');
         } else {
             $umsession->setProperty('support.write.last_error', 'What you typed did not match the image.');
         }
         // [TODO] Need to report the captcha didn't match and redraw the form
         DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('portal', $this->getPortal(), 'contact', 'step2')));
         return;
     }
     // Dispatch
     $to = $default_from;
     $subject = 'Contact me: Other';
     $sDispatch = DAO_CommunityToolProperty::get($this->getPortal(), UmScApp::PARAM_DISPATCH, '');
     $dispatch = !empty($sDispatch) ? unserialize($sDispatch) : array();
     foreach ($dispatch as $k => $v) {
         if (md5($k) == $sNature) {
             $to = $v['to'];
             $subject = 'Contact me: ' . strip_tags($k);
             break;
         }
     }
     if (!empty($sSubject)) {
         $subject = $sSubject;
     }
     $fieldContent = '';
     if (!empty($aFollowUpQ)) {
         $fieldContent = "\r\n\r\n";
         $fieldContent .= "--------------------------------------------\r\n";
         if (!empty($sNature)) {
             $fieldContent .= $subject . "\r\n";
             $fieldContent .= "--------------------------------------------\r\n";
         }
         foreach ($aFollowUpQ as $idx => $q) {
             $answer = isset($aFollowUpA[$idx]) ? $aFollowUpA[$idx] : '';
             $fieldContent .= "Q) " . $q . "\r\n" . "A) " . $answer . "\r\n";
             if ($idx + 1 < count($aFollowUpQ)) {
                 $fieldContent .= "\r\n";
             }
         }
         $fieldContent .= "--------------------------------------------\r\n";
         "\r\n";
     }
     $message = new CerberusParserMessage();
     $message->headers['date'] = date('r');
     $message->headers['to'] = $to;
     $message->headers['subject'] = $subject;
     $message->headers['message-id'] = CerberusApplication::generateMessageId();
     $message->headers['x-cerberus-portal'] = 1;
     // Sender
     $fromList = imap_rfc822_parse_adrlist($sFrom, '');
     if (empty($fromList) || !is_array($fromList)) {
         return;
         // abort with message
     }
     $from = array_shift($fromList);
     $message->headers['from'] = $from->mailbox . '@' . $from->host;
     $message->body = 'IP: ' . $fingerprint['ip'] . "\r\n\r\n" . $sContent . $fieldContent;
     $ticket_id = CerberusParser::parseMessage($message);
     $ticket = DAO_Ticket::getTicket($ticket_id);
     // Auto-save any custom fields
     $fields = DAO_CustomField::getBySource('cerberusweb.fields.source.ticket');
     if (!empty($aFieldIds)) {
         foreach ($aFieldIds as $iIdx => $iFieldId) {
             if (!empty($iFieldId)) {
                 $field =& $fields[$iFieldId];
                 /* @var $field Model_CustomField */
                 $value = "";
                 switch ($field->type) {
                     case Model_CustomField::TYPE_SINGLE_LINE:
                     case Model_CustomField::TYPE_MULTI_LINE:
                         @($value = trim($aFollowUpA[$iIdx]));
                         break;
                     case Model_CustomField::TYPE_NUMBER:
                         @($value = intval($aFollowUpA[$iIdx]));
                         break;
                     case Model_CustomField::TYPE_DATE:
                         if (false !== ($time = strtotime($aFollowUpA[$iIdx]))) {
                             @($value = intval($time));
                         }
                         break;
                     case Model_CustomField::TYPE_DROPDOWN:
                         @($value = $aFollowUpA[$iIdx]);
                         break;
                     case Model_CustomField::TYPE_CHECKBOX:
                         @($value = isset($aFollowUpA[$iIdx]) && !empty($aFollowUpA[$iIdx]) ? 1 : 0);
                         break;
                 }
                 if (!empty($value)) {
                     DAO_CustomFieldValue::setFieldValue('cerberusweb.fields.source.ticket', $ticket_id, $iFieldId, $value);
                 }
             }
         }
     }
     // Clear any errors
     $umsession->setProperty('support.write.last_nature', null);
     $umsession->setProperty('support.write.last_nature_string', null);
     $umsession->setProperty('support.write.last_content', null);
     $umsession->setProperty('support.write.last_error', null);
     $umsession->setProperty('support.write.last_opened', $ticket->mask);
     DevblocksPlatform::setHttpResponse(new DevblocksHttpResponse(array('portal', $this->getPortal(), 'contact', 'confirm')));
 }
Example #3
0
 public static function handleFormPost($source_ext_id, $source_id, $field_ids)
 {
     $fields = DAO_CustomField::getBySource($source_ext_id);
     if (is_array($field_ids)) {
         foreach ($field_ids as $field_id) {
             if (!isset($fields[$field_id])) {
                 continue;
             }
             switch ($fields[$field_id]->type) {
                 case Model_CustomField::TYPE_MULTI_LINE:
                 case Model_CustomField::TYPE_SINGLE_LINE:
                 case Model_CustomField::TYPE_URL:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'string', ''));
                     if (0 != strlen($field_value)) {
                         DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, $field_value);
                     } else {
                         DAO_CustomFieldValue::unsetFieldValue($source_ext_id, $source_id, $field_id);
                     }
                     break;
                 case Model_CustomField::TYPE_DROPDOWN:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'string', ''));
                     if (0 != strlen($field_value)) {
                         DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, $field_value);
                     } else {
                         DAO_CustomFieldValue::unsetFieldValue($source_ext_id, $source_id, $field_id);
                     }
                     break;
                 case Model_CustomField::TYPE_MULTI_PICKLIST:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'array', array()));
                     if (!empty($field_value)) {
                         DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, $field_value);
                     } else {
                         DAO_CustomFieldValue::unsetFieldValue($source_ext_id, $source_id, $field_id);
                     }
                     break;
                 case Model_CustomField::TYPE_CHECKBOX:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'integer', 0));
                     $set = !empty($field_value) ? 1 : 0;
                     DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, $set);
                     break;
                 case Model_CustomField::TYPE_MULTI_CHECKBOX:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'array', array()));
                     if (!empty($field_value)) {
                         DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, $field_value);
                     } else {
                         DAO_CustomFieldValue::unsetFieldValue($source_ext_id, $source_id, $field_id);
                     }
                     break;
                 case Model_CustomField::TYPE_DATE:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'string', ''));
                     @($date = strtotime($field_value));
                     if (!empty($date)) {
                         DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, $date);
                     } else {
                         DAO_CustomFieldValue::unsetFieldValue($source_ext_id, $source_id, $field_id);
                     }
                     break;
                 case Model_CustomField::TYPE_NUMBER:
                 case Model_CustomField::TYPE_WORKER:
                     @($field_value = DevblocksPlatform::importGPC($_POST['field_' . $field_id], 'string', ''));
                     if (0 != strlen($field_value)) {
                         DAO_CustomFieldValue::setFieldValue($source_ext_id, $source_id, $field_id, intval($field_value));
                     } else {
                         DAO_CustomFieldValue::unsetFieldValue($source_ext_id, $source_id, $field_id);
                     }
                     break;
             }
         }
     }
     return true;
 }
Example #4
0
 public static function _doBulkSetCustomFields($source_extension, $custom_fields, $ids)
 {
     $fields = DAO_CustomField::getAll();
     if (!empty($custom_fields)) {
         foreach ($custom_fields as $cf_id => $params) {
             if (!is_array($params) || !isset($params['value'])) {
                 continue;
             }
             $cf_val = $params['value'];
             // Data massaging
             switch ($fields[$cf_id]->type) {
                 case Model_CustomField::TYPE_DATE:
                     $cf_val = intval(@strtotime($cf_val));
                     break;
                 case Model_CustomField::TYPE_CHECKBOX:
                 case Model_CustomField::TYPE_NUMBER:
                     $cf_val = 0 == strlen($cf_val) ? '' : intval($cf_val);
                     break;
             }
             // If multi-selection types, handle delta changes
             if (Model_CustomField::TYPE_MULTI_PICKLIST == $fields[$cf_id]->type || Model_CustomField::TYPE_MULTI_CHECKBOX == $fields[$cf_id]->type) {
                 if (is_array($cf_val)) {
                     foreach ($cf_val as $val) {
                         $op = substr($val, 0, 1);
                         $val = substr($val, 1);
                         if (is_array($ids)) {
                             foreach ($ids as $id) {
                                 if ($op == '+') {
                                     DAO_CustomFieldValue::setFieldValue($source_extension, $id, $cf_id, $val, true);
                                 } elseif ($op == '-') {
                                     DAO_CustomFieldValue::unsetFieldValue($source_extension, $id, $cf_id, $val);
                                 }
                             }
                         }
                     }
                 }
                 // Otherwise, set/unset as a single field
             } else {
                 if (is_array($ids)) {
                     foreach ($ids as $id) {
                         if (0 != strlen($cf_val)) {
                             DAO_CustomFieldValue::setFieldValue($source_extension, $id, $cf_id, $cf_val);
                         } else {
                             DAO_CustomFieldValue::unsetFieldValue($source_extension, $id, $cf_id);
                         }
                     }
                 }
             }
         }
     }
 }