/** * @param Array $condition * @param Array $params * @return bool true for success, false otherwise */ public static function checkCondition($condition, &$params) { if ($condition['type'] === 'workflow_status') { return self::checkWorkflowStatusCondition($condition, $params); } $model = isset($params['model']) ? $params['model'] : null; $operator = isset($condition['operator']) ? $condition['operator'] : '='; // $type = isset($condition['type'])? $condition['type'] : null; $value = isset($condition['value']) ? $condition['value'] : null; // default to a doing basic value comparison if (isset($condition['name']) && $condition['type'] === '') { if (!isset($params[$condition['name']])) { return false; } return self::evalComparison($params[$condition['name']], $operator, $value); } switch ($condition['type']) { case 'attribute': if (!isset($condition['name'], $model)) { return false; } $attr =& $condition['name']; if (null === ($field = $model->getField($attr))) { return false; } if ($operator === 'changed') { return $model->attributeChanged($attr); } if ($field->type === 'link') { list($attrVal, $id) = Fields::nameAndId($model->getAttribute($attr)); } else { $attrVal = $model->getAttribute($attr); } return self::evalComparison($attrVal, $operator, X2Flow::parseValue($value, $field->type, $params), $field); case 'current_user': return self::evalComparison(Yii::app()->user->getName(), $operator, X2Flow::parseValue($value, 'assignment', $params)); case 'month': return self::evalComparison((int) date('n'), $operator, $value); // jan = 1, dec = 12 // jan = 1, dec = 12 case 'day_of_month': return self::evalComparison((int) date('j'), $operator, $value); // 1 through 31 // 1 through 31 case 'day_of_week': return self::evalComparison((int) date('N'), $operator, $value); // monday = 1, sunday = 7 // monday = 1, sunday = 7 case 'time_of_day': // - mktime(0,0,0) return self::evalComparison(time(), $operator, X2Flow::parseValue($value, 'time', $params)); // seconds since midnight // case 'current_local_time': // seconds since midnight // case 'current_local_time': case 'current_time': return self::evalComparison(time(), $operator, X2Flow::parseValue($value, 'dateTime', $params)); case 'user_active': return CActiveRecord::model('Session')->exists('user=:user AND status=1', array(':user' => X2Flow::parseValue($value, 'assignment', $params))); case 'on_list': if (!isset($model, $value)) { return false; } $value = X2Flow::parseValue($value, 'link'); // look up specified list if (is_numeric($value)) { $list = CActiveRecord::model('X2List')->findByPk($value); } else { $list = CActiveRecord::model('X2List')->findByAttributes(array('name' => $value)); } return $list !== null && $list->hasRecord($model); case 'has_tags': if (!isset($model, $value)) { return false; } $tags = X2Flow::parseValue($value, 'tags'); return $model->hasTags($tags, 'AND'); case 'workflow_status': if (!isset($model, $condition['workflowId'], $condition['stageNumber'])) { return false; } switch ($operator) { case 'started_workflow': return CActiveRecord::model('Actions')->exists('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'])); case 'started_stage': return CActiveRecord::model('Actions')->exists('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow AND stageNumber=:stage AND (completeDate IS NULL OR completeDate=0)', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'], ':stageNumber' => $condition['stageNumber'])); case 'completed_stage': return CActiveRecord::model('Actions')->exists('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow AND stageNumber=:stage AND completeDate > 0', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'], ':stageNumber' => $condition['stageNumber'])); case 'completed_workflow': $stageCount = CActiveRecord::model('WorkflowStage')->count('workflowId=:id', array(':id' => $condition['workflowId'])); $actionCount = CActiveRecord::model('Actions')->count('associationType=:type AND associationId=:modelId AND type="workflow" AND workflowId=:workflow', array(':type' => get_class($model), ':modelId' => $model->id, ':workflow' => $condition['workflowId'])); return $actionCount >= $stageCount; } return false; case 'email_open': if (isset($params['sentEmails'], $params['sentEmails'][$value])) { $trackEmail = TrackEmail::model()->findByAttributes(array('uniqueId' => $params['sentEmails'][$value])); return $trackEmail && !is_null($trackEmail->opened); } return false; } return false; // foreach($condition as $key = >$value) { // Record attribute (=, <, >, <>, in list, not in list, empty, not empty, contains) // Linked record attribute (eg. a contact's account has > 30 employees) // Current user // Current time (day of week, hours, etc) // Current time in record's timezone // Is user X logged in // Workflow status (in workflow X, started stage Y, completed Y, completed all) // } }
/** * Called when a Contact opens an email sent from Inline Email Form. Inline Email Form * appends an image to the email with src pointing to this function. This function * creates an action associated with the Contact indicating that the email was opened. * * @param integer $uid The unique id of the recipient * @param string $type 'open', 'click', or 'unsub' * */ public function actionEmailOpened($uid, $type) { // If the request is coming from within the web application, ignore it. $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; $baseUrl = Yii::app()->request->getBaseUrl(true); $fromApp = strpos($referrer, $baseUrl) === 0; if ($type == 'open' && !$fromApp) { $track = TrackEmail::model()->findByAttributes(array('uniqueId' => $uid)); $track->recordEmailOpen(); } //return a one pixel transparent png header('Content-Type: image/png'); echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAAXNSR0IArs4c6QAAAAJiS0dEAP+Hj8y/AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAC0lEQVQI12NgYAAAAAMAASDVlMcAAAAASUVORK5CYII='); }
/** * Called when a Contact opens an email sent from Inline Email Form. Inline Email Form * appends an image to the email with src pointing to this function. This function * creates an action associated with the Contact indicating that the email was opened. * * @param integer $uid The unique id of the recipient * @param string $type 'open', 'click', or 'unsub' * */ public function actionEmailOpened($uid, $type) { // If the request is coming from within the web application, ignore it. $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; $baseUrl = Yii::app()->request->getBaseUrl(true); $fromApp = strpos($referrer, $baseUrl) === 0; if ($type == 'open' && !$fromApp) { $track = TrackEmail::model()->findByAttributes(array('uniqueId' => $uid)); if ($track && $track->opened == null) { $action = $track->action; if ($action) { $note = new Actions(); switch ($action->type) { case 'email_quote': case 'email_invoice': $subType = str_replace('email_', '', $action->type); $note->type = "emailOpened_{$subType}"; $quote = Quote::model()->findByPk($action->associationId); if ($quote instanceof Quote) { $contact = $quote->associatedContactsModel; if ($contact instanceof Contacts) { $note->associationType = 'contacts'; $note->associationId = $contact->id; } } break; default: $note->type = 'emailOpened'; $note->associationType = $action->associationType; $note->associationId = $action->associationId; } $now = time(); $note->createDate = $now; $note->lastUpdated = $now; $note->completeDate = $now; $note->complete = 'Yes'; $note->updatedBy = 'admin'; $note->associationName = $action->associationName; $note->visibility = $action->visibility; $note->assignedTo = $action->assignedTo; $note->actionDescription = Yii::t('marketing', 'Contact has opened the email sent on '); $note->actionDescription .= Formatter::formatLongDateTime($action->createDate) . "<br>"; $note->actionDescription .= $action->actionDescription; if ($note->save()) { $event = new Events(); $event->type = 'email_opened'; switch ($action->type) { case 'email_quote': $event->subtype = 'quote'; break; case 'email_invoice': $event->subtype = 'invoice'; break; default: $event->subtype = 'email'; } $contact = isset($quote) && $quote instanceof Quote ? $quote->associatedContactsModel : X2Model::model('Contacts')->findByPk($action->associationId); if (isset($contact)) { $event->user = $contact->assignedTo; } $event->associationType = 'Contacts'; $event->associationId = isset($contact) ? $contact->id : $note->associationId; if ($action->associationType == 'services') { $case = X2Model::model('Services')->findByPk($action->associationId); if (isset($case) && is_numeric($case->contactId)) { $event->associationId = $case->contactId; } elseif (isset($case)) { $event->associationType = 'Services'; $event->associationId = $case->id; } } $event->save(); $track->opened = $now; $track->update(); } } } } //return a one pixel transparent png header('Content-Type: image/png'); echo base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAAXNSR0IArs4c6QAAAAJiS0dEAP+Hj8y/AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAC0lEQVQI12NgYAAAAAMAASDVlMcAAAAASUVORK5CYII='); }
/** * Save the tracking record for this email, but only if an image was inserted. * * @param integer $actionId ID of the email-type action corresponding to the record. */ public function trackEmail($actionId) { if (isset($this->_uniqueId)) { $track = new TrackEmail(); $track->actionId = $actionId; $track->uniqueId = $this->uniqueId; $track->save(); } }
private function handleServiceFormSubmission($model, $extractedParams) { if (isset($_POST['Services'])) { // web form submitted if (isset($_POST['Services']['firstName'])) { $firstName = $_POST['Services']['firstName']; $fullName = $firstName; } if (isset($_POST['Services']['lastName'])) { $lastName = $_POST['Services']['lastName']; if (isset($fullName)) { $fullName .= ' ' . $lastName; } else { $fullName = $lastName; } } if (isset($_POST['Services']['email'])) { $email = $_POST['Services']['email']; } if (isset($_POST['Services']['phone'])) { $phone = $_POST['Services']['phone']; } if (isset($_POST['Services']['desription'])) { $description = $_POST['Services']['description']; } // Extra sanitizing $p = Fields::getPurifier(); foreach ($model->attributes as $name => $value) { if ($name != $model->primaryKey() && !empty($value)) { $model->{$name} = $p->purify($value); } } if (isset($email) && $email) { $contact = Contacts::model()->findByAttributes(array('email' => $email)); } else { $contact = false; } if ($contact) { $model->contactId = $contact->nameId; } else { $model->contactId = "Unregistered"; } if (isset($fullName) || isset($email)) { $model->subject = Yii::t('services', 'Web Form Case entered by {name}', array('{name}' => isset($fullName) ? $fullName : $email)); } else { $model->subject = Yii::t('services', 'Web Form Case'); } $model->origin = 'Web'; if (!isset($model->impact) || $model->impact == '') { $model->impact = Yii::t('services', '3 - Moderate'); } if (!isset($model->status) || $model->status == '') { $model->status = Yii::t('services', 'New'); } if (!isset($model->mainIssue) || $model->mainIssue == '') { $model->mainIssue = Yii::t('services', 'General Request'); } if (!isset($model->subIssue) || $model->subIssue == '') { $model->subIssue = Yii::t('services', 'Other'); } $model->assignedTo = $this->controller->getNextAssignee(); if (isset($email)) { $model->email = CHtml::encode($email); } $now = time(); $model->createDate = $now; $model->lastUpdated = $now; $model->updatedBy = 'admin'; if (isset($description)) { $model->description = CHtml::encode($description); } if (!$model->hasErrors()) { if ($model->save()) { $model->name = $model->id; $model->update(array('name')); self::addTags($model); //use the submitted info to create an action $action = new Actions(); $action->actionDescription = Yii::t('contacts', 'Web Form') . "\n\n" . (isset($fullName) ? Yii::t('contacts', 'Name') . ': ' . $fullName . "\n" : '') . (isset($email) ? Yii::t('contacts', 'Email') . ": " . $email . "\n" : '') . (isset($phone) ? Yii::t('contacts', 'Phone') . ": " . $phone . "\n" : '') . (isset($description) ? Yii::t('services', 'Description') . ": " . $description : ''); // create action $action->type = 'note'; $action->assignedTo = $model->assignedTo; $action->visibility = '1'; $action->associationType = 'services'; $action->associationId = $model->id; $action->associationName = $model->name; $action->createDate = $now; $action->lastUpdated = $now; $action->completeDate = $now; $action->complete = 'Yes'; $action->updatedBy = 'admin'; $action->save(); if (isset($email)) { //send email $emailBody = Yii::t('services', 'Hello') . ' ' . $fullName . ",<br><br>"; $emailBody .= Yii::t('services', 'Thank you for contacting our Technical Support ' . 'team. This is to verify we have received your request for Case# ' . '{casenumber}. One of our Technical Analysts will contact you shortly.', array('{casenumber}' => $model->id)); $emailBody = Yii::app()->settings->serviceCaseEmailMessage; if (isset($firstName)) { $emailBody = preg_replace('/{first}/u', $firstName, $emailBody); } if (isset($lastName)) { $emailBody = preg_replace('/{last}/u', $lastName, $emailBody); } if (isset($phone)) { $emailBody = preg_replace('/{phone}/u', $phone, $emailBody); } if (isset($email)) { $emailBody = preg_replace('/{email}/u', $email, $emailBody); } if (isset($description)) { $emailBody = preg_replace('/{description}/u', $description, $emailBody); } $emailBody = preg_replace('/{case}/u', $model->id, $emailBody); $emailBody = preg_replace('/\\n|\\r\\n/', "<br>", $emailBody); $uniqueId = md5(uniqid(rand(), true)); $emailBody .= '<img src="' . $this->controller->createAbsoluteUrl('/actions/actions/emailOpened', array('uid' => $uniqueId, 'type' => 'open')) . '"/>'; $emailSubject = Yii::app()->settings->serviceCaseEmailSubject; if (isset($firstName)) { $emailSubject = preg_replace('/{first}/u', $firstName, $emailSubject); } if (isset($lastName)) { $emailSubject = preg_replace('/{last}/u', $lastName, $emailSubject); } if (isset($phone)) { $emailSubject = preg_replace('/{phone}/u', $phone, $emailSubject); } if (isset($email)) { $emailSubject = preg_replace('/{email}/u', $email, $emailSubject); } if (isset($description)) { $emailSubject = preg_replace('/{description}/u', $description, $emailSubject); } $emailSubject = preg_replace('/{case}/u', $model->id, $emailSubject); if (Yii::app()->settings->serviceCaseEmailAccount != Credentials::LEGACY_ID) { $from = (int) Yii::app()->settings->serviceCaseEmailAccount; } else { $from = array('name' => Yii::app()->settings->serviceCaseFromEmailName, 'address' => Yii::app()->settings->serviceCaseFromEmailAddress); } $useremail = array('to' => array(array(isset($fullName) ? $fullName : '', $email))); $status = $this->controller->sendUserEmail($useremail, $emailSubject, $emailBody, null, $from); if ($status['code'] == 200) { if ($model->assignedTo != 'Anyone') { $profile = X2Model::model('Profile')->findByAttributes(array('username' => $model->assignedTo)); if (isset($profile)) { $useremail['to'] = array(array($profile->fullName, $profile->emailAddress)); $emailSubject = 'Service Case Created'; $emailBody = "A new service case, #" . $model->id . ", has been created in X2Engine. To view the case, click " . "this link: " . $model->getLink(); $status = $this->controller->sendUserEmail($useremail, $emailSubject, $emailBody, null, $from); } } //email action $action = new Actions(); $action->associationType = 'services'; $action->associationId = $model->id; $action->associationName = $model->name; $action->visibility = 1; $action->complete = 'Yes'; $action->type = 'email'; $action->completedBy = 'admin'; $action->assignedTo = $model->assignedTo; $action->createDate = time(); $action->dueDate = time(); $action->completeDate = time(); $action->actionDescription = '<b>' . $model->subject . "</b>\n\n" . $emailBody; if ($action->save()) { $track = new TrackEmail(); $track->actionId = $action->id; $track->uniqueId = $uniqueId; $track->save(); } } else { $errMsg = 'Error: actionWebForm.php: sendUserEmail failed'; /**/ AuxLib::debugLog($errMsg); Yii::log($errMsg, '', 'application.debug'); } } $this->controller->renderPartial('application.components.views.webFormSubmit', array('type' => 'service', 'caseNumber' => $model->id)); Yii::app()->end(); // success! } } } $sanitizedGetParams = self::sanitizeGetParams(); $this->controller->renderPartial('application.components.views.webForm', array_merge(array('model' => $model, 'type' => 'service'), $sanitizedGetParams)); }