/** * 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='); }
/** * @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)); 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='); }