/** * Helper method for compareAttributes */ protected function compareAttribute(&$criteria, $field) { $fieldName = $field->fieldName; switch ($field->type) { case 'boolean': $criteria->compare('t.' . $fieldName, $this->compareBoolean($this->{$fieldName}), true); break; case 'assignment': $assignmentCriteria = new CDbCriteria(); $assignmentVal = $this->compareAssignment($this->{$fieldName}); if ($field->linkType === 'multiple' && $this->{$fieldName}) { if (!is_array($assignmentVal)) { $assignmentVal = array(); } $assignmentVal = array_map(function ($val) { return preg_quote($val); }, $assignmentVal); if (strlen($this->{$fieldName}) && strncmp("Anyone", ucfirst($this->{$fieldName}), strlen($this->{$fieldName})) === 0) { $assignmentVal[] = 'Anyone'; } $assignmentRegex = '(^|, )(' . implode('|', $assignmentVal) . ')' . (in_array('Anyone', $assignmentVal) ? '?' : '') . '(, |$)'; $assignmentParamName = CDbCriteria::PARAM_PREFIX . CDbCriteria::$paramCount; $criteria->params[$assignmentParamName] = $assignmentRegex; CDbCriteria::$paramCount++; $criteria->addCondition('t.' . $fieldName . ' REGEXP BINARY ' . $assignmentParamName); } else { $assignmentCriteria->compare('t.' . $fieldName, $assignmentVal, true); if (strlen($this->{$fieldName}) && strncmp("Anyone", ucfirst($this->{$fieldName}), strlen($this->{$fieldName})) === 0) { $assignmentCriteria->compare('t.' . $fieldName, 'Anyone', false, 'OR'); $assignmentCriteria->addCondition('t.' . $fieldName . ' = ""', 'OR'); } } $criteria->mergeWith($assignmentCriteria); break; case 'dropdown': $dropdownVal = $this->compareDropdown($field->linkType, $this->{$fieldName}); if (is_array($dropdownVal)) { foreach ($dropdownVal as $val) { $dropdownRegex = '(^|((\\[|,)"))' . preg_quote($val) . '(("(,|\\]))|$)'; $dropdownParamName = CDbCriteria::PARAM_PREFIX . CDbCriteria::$paramCount; $criteria->params[$dropdownParamName] = $dropdownRegex; CDbCriteria::$paramCount++; $criteria->addCondition('t.' . $fieldName . ' REGEXP BINARY ' . $dropdownParamName); } } else { $criteria->compare('t.' . $fieldName, $dropdownVal, false); } break; case 'date': case 'dateTime': if (!empty($this->{$fieldName})) { // get operator and convert date string to timestamp $retArr = $this->unshiftOperator($this->{$fieldName}); $operator = $retArr[0]; $timestamp = Formatter::parseDate($retArr[1]); if (!$timestamp) { // if date string couldn't be parsed, it's better to display no results // than non-empty incorrect results (which could result in bad mass updates // or deletes) $criteria->addCondition('FALSE'); } else { if ($operator === '=' || $operator === '') { $criteria->addBetweenCondition('t.' . $fieldName, $timestamp, $timestamp + 60 * 60 * 24); } else { $value = $operator . $timestamp; $criteria->compare('t.' . $fieldName, $value); } } } break; case 'phone': // $criteria->join .= ' RIGHT JOIN x2_phone_numbers ON (x2_phone_numbers.itemId=t.id AND x2_tags.type="Contacts" AND ('.$tagConditions.'))'; // $criteria->join .= ' RIGHT JOIN x2_phone_numbers ON (x2_phone_numbers.itemId=t.id AND x2_tags.type="Contacts" AND ('.$tagConditions.'))'; default: $criteria->compare('t.' . $fieldName, $this->{$fieldName}, true); } }
/** * Parses a value for table insertion using X2Fields rules * @param mixed $value * @param bool $filter If true, replace HTML special characters (prevents markup injection) * @return mixed the parsed value */ public function parseValue($value, $filter = false) { if (in_array($this->type, array('int', 'float', 'currency', 'percentage'))) { return self::strToNumeric($value, $this->type); } switch ($this->type) { case 'assignment': return $this->linkType === 'multiple' ? self::parseUsers($value) : $value; case 'date': case 'dateTime': if (is_numeric((string) $value)) { // must already be a timestamp return $value; } $value = $this->type === 'dateTime' ? Formatter::parseDateTime($value) : Formatter::parseDate($value); return $value === false ? null : $value; case 'link': if (empty($value) || empty($this->linkType)) { return $value; } list($name, $id) = self::nameAndId($value); if (ctype_digit((string) $id)) { // Already formatted as a proper reference. Check for existence of the record. $linkedModel = X2Model::model($this->linkType)->findByAttributes(array('nameId' => $value)); // Return the plain text name if the link is broken; otherwise, // given how the record exists, return the value. return empty($linkedModel) ? $name : $value; } else { if (ctype_digit($value)) { // User manually entered the ID, i.e. in an API call $link = Yii::app()->db->createCommand()->select('nameId')->from(X2Model::model($this->linkType)->tableName())->where('id=?', array($value))->queryScalar(); } else { // Look up model's unique nameId by its name: $link = Yii::app()->db->createCommand()->select('nameId')->from(X2Model::model($this->linkType)->tableName())->where('name=?', array($name))->queryScalar(); } } return $link === false ? $name : $link; case 'boolean': return (bool) $value; case 'text': return self::getPurifier()->purify($value); case 'dropdown': return is_array($value) ? CJSON::encode($value) : $value; default: return $filter ? CHtml::encode($value) : $value; } }
public function filterModels(array $gridModels) { $filteredModels = array(); $that = $this; $filters = array_filter($this->attributeNames(), function ($a) use($that) { return $that->{$a} !== '' && $that->{$a} !== null; }); foreach ($gridModels as $model) { $filterOut = false; foreach ($filters as $filter) { $val = $this->{$filter}; switch ($filter) { case 'name': $filterOut = !preg_match('/' . $val . '/i', $model->relatedModel->getAttribute('name')); break; case 'relatedModelName': $filterOut = $val !== get_class($model->relatedModel); break; case 'assignedTo': $filterOut = !preg_match('/' . $val . '/i', $model->relatedModel->getAttribute('assignedTo.fullName')); break; case 'label': $filterOut = !preg_match('/' . $val . '/i', $model->relatedModel->getRelationshipLabel($this->myModel)); break; case 'createDate': $timestampA = Formatter::parseDate($val); $timestampB = $model->relatedModel->getAttribute('createDate'); // compare days since UNIX epoch $filterOut = floor($timestampA / (3600 * 24)) !== floor($timestampB / (3600 * 24)); break; } if ($filterOut) { break; } } if (!$filterOut) { $filteredModels[] = $model; } } return $filteredModels; }
public static function parseValue($value, $type, &$params = null, $renderFlag = true) { if (is_string($value)) { if (strpos($value, '=') === 0) { // It's a formula. Evaluate it. $evald = X2FlowFormatter::parseFormula($value, $params); // Fail silently because there's not yet a good way of reporting // problems that occur in parseFormula -- $value = ''; if ($evald[0]) { $value = $evald[1]; } } else { // Run token replacement: $value = X2FlowFormatter::replaceVariables($value, $params, $type, $renderFlag, true); } } switch ($type) { case 'boolean': return (bool) $value; case 'time': case 'date': case 'dateTime': if (ctype_digit((string) $value)) { // must already be a timestamp return $value; } if ($type === 'date') { $value = Formatter::parseDate($value); } elseif ($type === 'dateTime') { $value = Formatter::parseDateTime($value); } else { $value = strtotime($value); } return $value === false ? null : $value; case 'link': $pieces = explode('_', $value); if (count($pieces) > 1) { return $pieces[0]; } return $value; case 'tags': return Tags::parseTags($value); default: return $value; } }
public function renderFilterCellByType() { $model = $this->grid->filter; $fieldName = $this->name; switch ($this->filterType) { case 'date': $model->{$fieldName} = Formatter::parseDate($model->{$fieldName}); return X2Html::activeDatePicker($model, $this->name); break; case 'dateTime': $model->{$fieldName} = Formatter::parseDate($model->{$fieldName}); return X2Html::activeDatePicker($model, $this->name, array(), 'datetime'); break; } }
/** * Helper method for compareAttributes */ protected function compareAttribute(&$criteria, $field) { $fieldName = $field->fieldName; switch ($field->type) { case 'boolean': $criteria->compare('t.' . $fieldName, $this->compareBoolean($this->{$fieldName}), true); break; case 'assignment': $criteria->compare('t.' . $fieldName, $this->compareAssignment($this->{$fieldName}), true); break; case 'dropdown': $criteria->compare('t.' . $fieldName, $this->compareDropdown($field->linkType, $this->{$fieldName}), false); break; case 'date': case 'dateTime': if (!empty($this->{$fieldName})) { // get operator and convert date string to timestamp $retArr = $this->unshiftOperator($this->{$fieldName}); $operator = $retArr[0]; $timestamp = Formatter::parseDate($retArr[1]); if (!$timestamp) { // if date string couldn't be parsed, it's better to display no results // than non-empty incorrect results (which could result in bad mass updates // or deletes) $criteria->addCondition('FALSE'); } else { if ($operator === '=' || $operator === '') { $criteria->addBetweenCondition('t.' . $fieldName, $timestamp, $timestamp + 60 * 60 * 24); } else { $value = $operator . $timestamp; $criteria->compare('t.' . $fieldName, $value); } } } break; case 'phone': // $criteria->join .= ' RIGHT JOIN x2_phone_numbers ON (x2_phone_numbers.itemId=t.id AND x2_tags.type="Contacts" AND ('.$tagConditions.'))'; // $criteria->join .= ' RIGHT JOIN x2_phone_numbers ON (x2_phone_numbers.itemId=t.id AND x2_tags.type="Contacts" AND ('.$tagConditions.'))'; default: $criteria->compare('t.' . $fieldName, $this->{$fieldName}, true); } }
public function actionUpdateStageDetails($id) { $action = X2Model::model('Actions')->findByPk($id); $previouslyComplete = $action->complete === 'Yes'; $model = X2Model::getModelOfTypeWithId($action->associationType, $action->associationId, true); if (isset($model, $action, $_POST['Actions'])) { $action->setScenario('workflow'); $action->createDate = Formatter::parseDate($_POST['Actions']['createDate']); $action->completeDate = Formatter::parseDate($_POST['Actions']['completeDate']); $action->actionDescription = $_POST['Actions']['actionDescription']; if (isset($_POST['Actions']['completedBy']) && (Yii::app()->params->isAdmin || Yii::app()->settings->workflowBackdateReassignment)) { $action->completedBy = $_POST['Actions']['completedBy']; } // don't save if createDate isn't valid if ($action->createDate === false) { return; } if ($action->completeDate === false) { $action->complete = 'No'; $action->completedBy = null; $model->updateLastActivity(); if ($previouslyComplete) { // we're uncompleting this thing Workflow::updateWorkflowChangelog($action, 'revert', $model); } unset($action->completeDate); // remove invalid value } else { if ($action->completeDate < $action->createDate) { // we can't have the completeDate before the createDate now can we $action->completeDate = $action->createDate; } $action->complete = 'Yes'; if (!$previouslyComplete) { // we're completing it Workflow::updateWorkflowChangelog($action, 'complete', $model); } } if ($action->save()) { if ($action->complete === 'Yes') { echo 'complete'; } else { echo 'success'; } } } }
/** * Legacy Method: Should be replaced with dateRangeToDates * * This function returns a date range to be used for generating a report * based on the dropdown value the user selected. I think it might occur elsewhere * in the code and could probably be refactored. * @return array An array with the date range values */ public static function getDateRange($startKey = 'start', $endKey = 'end', $rangeKey = 'range', $defaultRange = 'custom') { $dateRange = array(); $dateRange['strict'] = false; if (isset($_GET['strict']) && $_GET['strict']) { $dateRange['strict'] = true; } $dateRange['range'] = $defaultRange; if (isset($_GET[$rangeKey])) { $dateRange['range'] = $_GET[$rangeKey]; } switch ($dateRange['range']) { case 'thisWeek': $dateRange['start'] = strtotime('mon this week'); // first of this month $dateRange['end'] = time(); // now break; case 'thisMonth': $dateRange['start'] = mktime(0, 0, 0, date('n'), 1); // first of this month $dateRange['end'] = time(); // now break; case 'lastWeek': $dateRange['start'] = strtotime('mon last week'); // first of last month $dateRange['end'] = strtotime('mon this week') - 1; // first of this month break; case 'lastMonth': $dateRange['start'] = mktime(0, 0, 0, date('n') - 1, 1); // first of last month $dateRange['end'] = mktime(0, 0, 0, date('n'), 1) - 1; // first of this month break; case 'thisYear': $dateRange['start'] = mktime(0, 0, 0, 1, 1); // first of the year $dateRange['end'] = time(); // now break; case 'lastYear': $dateRange['start'] = mktime(0, 0, 0, 1, 1, date('Y') - 1); // first of last year $dateRange['end'] = mktime(0, 0, 0, 1, 1, date('Y')) - 1; // first of this year break; case 'all': $dateRange['start'] = 0; // every record $dateRange['end'] = time(); if (isset($_GET[$endKey])) { $dateRange['end'] = Formatter::parseDate($_GET[$endKey]); if ($dateRange['end'] == false) { $dateRange['end'] = time(); } else { $dateRange['end'] = strtotime('23:59:59', $dateRange['end']); } } break; case 'custom': default: $dateRange['end'] = time(); if (isset($_GET[$endKey])) { $dateRange['end'] = Formatter::parseDate($_GET[$endKey]); if ($dateRange['end'] == false) { $dateRange['end'] = time(); } else { $dateRange['end'] = strtotime('23:59:59', $dateRange['end']); } } $dateRange['start'] = strtotime('1 month ago', $dateRange['end']); if (isset($_GET[$startKey])) { $dateRange['start'] = Formatter::parseDate($_GET[$startKey]); if ($dateRange['start'] == false) { $dateRange['start'] = strtotime('-30 days 0:00', $dateRange['end']); } else { $dateRange['start'] = strtotime('0:00', $dateRange['start']); } } } return $dateRange; }