public function testAddValuesToCustomFieldData() { $authenticationData = $this->login(); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $typeFieldData = CustomFieldData::getByName('AccountTypes'); $compareData = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($typeFieldData, 'en'); $data = array('values' => array('Unknown', 'None', array('Not Alowed', '22'), 'None')); $response = $this->createApiCallWithRelativeUrl('addValues/' . $typeFieldData->name, 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $compareData['Unknown'] = 'Unknown'; $compareData['None'] = 'None'; $this->assertEquals($compareData, $response['data']); // Test get custom field CustomFieldData::forgetAll(); $response = $this->createApiCallWithRelativeUrl('read/AccountTypes', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals($compareData, $response['data']); // Test with user that not belong to super administrator group $notAllowedUser = UserTestHelper::createBasicUser('Steven'); $notAllowedUser->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API); $this->assertTrue($notAllowedUser->save()); // Test with unprivileged user that do not belong to super administrators group $authenticationData = $this->login('steven', 'steven'); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); $data = array('values' => array('Unknown2', 'None2')); $response = $this->createApiCallWithRelativeUrl('addValues/AccountTypes', 'PUT', $headers, array('data' => $data)); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']); $this->assertEquals('You do not have rights to perform this action.', $response['message']); }
/** * Gets array for stages * @return array */ protected function getStageDropDownArray() { $customFieldData = CustomFieldData::getByName('ProductStages'); $customFieldIndexedData = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, Yii::app()->language); $data = array_merge(array(ProductsConfigurationForm::FILTERED_BY_ALL_STAGES => Zurmo::t('Core', 'All')), $customFieldIndexedData); return $data; }
protected function getDropDownArray() { $contact = new Contact(); $attribute = $this->model->getAttributeName(); $dropDownModel = $contact->{$attribute}; $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($dropDownModel->data, Yii::app()->language); return $dataAndLabels; }
/** * @return array */ public function getCustomFieldDataAndLabels() { $modelClassName = $this->modelClassName; $model = new $modelClassName(false); //cause we can call the metadata statically and get the customField name to get the CustomFieldData by name. $dropDownModel = $model->{$this->modelAttributeName}; $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($dropDownModel->data, Yii::app()->language); return $dataAndLabels; }
protected function resolveIdAndNameByModel(RedBeanModel $customFieldValue) { if (!isset($this->dataAndLabels)) { $multipleValuesCustomField = $this->model->{$this->attribute}; $this->dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($multipleValuesCustomField->data, Yii::app()->language); } if ($customFieldValue->value != null) { return array('id' => $customFieldValue->value, 'name' => $this->dataAndLabels[$customFieldValue->value]); } }
public function getChartData() { $customFieldData = CustomFieldDataModelUtil::getDataByModelClassNameAndAttributeName('Opportunity', 'stage'); $labels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, Yii::app()->language); $sql = static::makeChartSqlQuery(); $rows = R::getAll($sql); $chartData = array(); foreach ($rows as $row) { $chartData[] = array('value' => $this->resolveCurrencyValueConversionRateForCurrentUserForDisplay($row['amount']), 'displayLabel' => static::resolveLabelByValueAndLabels($row['stage'], $labels)); } return $chartData; }
protected function renderControlEditable() { $multipleValuesCustomField = $this->model->{$this->attribute}; assert('$multipleValuesCustomField instanceof MultipleValuesCustomField'); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($multipleValuesCustomField->data, Yii::app()->language); $dataToValuesString = static::getDataToValuesString($multipleValuesCustomField->values); $dataLabels = static::getJsonEncodedLabelsByDataAndLabels($multipleValuesCustomField->values, $dataAndLabels); $idForInput = $this->getIdForSelectInput(); $autoCompleteUrl = Yii::app()->createUrl('zurmo/default/autoCompleteCustomFieldData/', array('name' => $this->model->{$this->attribute}->data->name)); $cClipWidget = new CClipWidget(); $cClipWidget->beginClip("ModelElement"); $cClipWidget->widget('application.core.widgets.MultiSelectAutoComplete', array('name' => $this->getNameForSelectInput(), 'id' => $this->getIdForSelectInput(), 'jsonEncodedIdsAndLabels' => $dataLabels, 'sourceUrl' => $autoCompleteUrl, 'htmlOptions' => array('disabled' => $this->getDisabledValue()), 'hintText' => Zurmo::t('Core', 'Type to find a tag'))); $cClipWidget->endClip(); $content = $cClipWidget->getController()->clips['ModelElement']; return $content; }
protected function processRead($id) { assert('is_string($id)'); try { $customFieldData = CustomFieldData::getByName($id); } catch (NotFoundException $e) { $message = Zurmo::t('ZurmoModule', 'Specified custom field name was invalid.'); throw new ApiException($message); } $customFieldData = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, Yii::app()->language); try { $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $customFieldData, null, null); } catch (Exception $e) { $message = $e->getMessage(); throw new ApiException($message); } return $result; }
public function testSetAndGetEmptyValueWhichShouldUtilizeFallBack() { $this->assertEquals('en', Yii::app()->language); $values = array('Item 1', 'Item 2', 'Item 3'); $labels = array('fr' => array('Item 1 fr', '', 'Item 3 fr')); $customFieldData = CustomFieldData::getByName('Items2'); $customFieldData->serializedData = serialize($values); $customFieldData->serializedLabels = serialize($labels); $this->assertTrue($customFieldData->save()); $id = $customFieldData->id; $customFieldData->forget(); unset($customFieldData); $customFieldData = CustomFieldData::getById($id); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, 'en'); $compareData = array('Item 1' => 'Item 1', 'Item 2' => 'Item 2', 'Item 3' => 'Item 3'); $this->assertEquals($compareData, $dataAndLabels); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, 'fr'); $compareData = array('Item 1' => 'Item 1 fr', 'Item 2' => 'Item 2', 'Item 3' => 'Item 3 fr'); $this->assertEquals($compareData, $dataAndLabels); }
protected function renderValuesToParentValuesMappingDropDownContent(DropDownDependencyCustomFieldMapping $parentMapping, $position, $value, $valuePosition, $selectedParentValue) { assert('is_int($position)'); assert('is_string($value)'); assert('is_int($valuePosition)'); assert('is_string($selectedParentValue) || $selectedParentValue == null'); $inputName = $this->formName . '[mappingData][' . $position . '][valuesToParentValues][' . $value . ']'; $inputId = $this->formName . '_mappingData_' . $position . '_valuesToParentValues_' . $valuePosition; $htmlOptions = array(); $htmlOptions['id'] = $inputId; $htmlOptions['empty'] = Zurmo::t('Core', '(None)'); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($parentMapping->getCustomFieldData(), Yii::app()->language); $content = ZurmoHtml::dropDownList($inputName, $selectedParentValue, $dataAndLabels, $htmlOptions); return $content; }
protected function getTitleDropDownArray() { $dropDownModel = $this->model->title; $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($dropDownModel->data, Yii::app()->language); return $dataAndLabels; }
/** * Given a name of a customFieldData object and a term to search on return a JSON encoded * array of autocomplete search results. * @param $customFieldDataName * @param $partialName * @return array */ public static function getCustomFieldDataByPartialName($customFieldDataName, $partialName) { assert('is_string($customFieldDataName)'); assert('is_string($partialName)'); $customFieldData = CustomFieldData::getByName($customFieldDataName); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, Yii::app()->language); $autoCompleteResults = array(); foreach ($dataAndLabels as $data => $label) { if (stripos($label, $partialName) === 0) { $autoCompleteResults[] = array('id' => $data, 'name' => $label); } } return $autoCompleteResults; }
/** * @return array * @throws NotSupportedException if the resolved attribute is invalid and not on the resolved model */ public function getCustomFieldDataAndLabels() { $modelClassName = $this->getResolvedAttributeModelClassName(); $attribute = $this->getResolvedAttribute(); if ($modelClassName::isAnAttribute($attribute)) { $model = new $modelClassName(false); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($model->{$attribute}->data, Yii::app()->language); return $dataAndLabels; } else { throw new NotSupportedException(); } }
/** * Given an array of data, create stringified content. Method is extended to provide support for translating * the data into the correct language. * (non-PHPdoc) * @see RedBeanModel::stringifyOneToManyRelatedModelsValues() */ public function stringifyOneToManyRelatedModelsValues($values) { assert('is_array($values)'); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($this->data, Yii::app()->language); foreach ($values as $key => $value) { if (ArrayUtil::getArrayValue($dataAndLabels, $value) != null) { $values[$key] = ArrayUtil::getArrayValue($dataAndLabels, $value); } } return ArrayUtil::stringify($values); }
/** * @depends testApiServerUrl */ public function testGetCustomFieldData() { $authenticationData = $this->login(); $headers = array('Accept: application/json', 'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'], 'ZURMO_TOKEN: ' . $authenticationData['token'], 'ZURMO_API_REQUEST_TYPE: REST'); //Fill some data $values = array('Prospect', 'Customer', 'Vendor'); $typeFieldData = CustomFieldData::getByName('AccountTypes'); $typeFieldData->serializedData = serialize($values); $this->assertTrue($typeFieldData->save()); CustomFieldData::forgetAll(); $customFieldData = CustomFieldData::getByName('AccountTypes'); $compareData = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, 'en'); $response = ApiRestTestHelper::createApiCall($this->serverUrl . '/test.php/zurmo/customField/api/read/AccountTypes', 'GET', $headers); $response = json_decode($response, true); $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']); $this->assertEquals($compareData, $response['data']); }
/** * If role is required, and there is no default value selected, grab the first value available for the role. * @param AccountContactAffiliation $accountContactAffiliation * @return mixed */ protected function resolveRoleValue(AccountContactAffiliation $accountContactAffiliation) { $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($accountContactAffiliation->role->data, Yii::app()->language); return key($dataAndLabels); }
/** * @return array */ protected function resolveGroupByDataAndTranslatedLabels() { $dropDownModel = $this->model->{$this->groupByAttribute}; return CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($dropDownModel->data, Yii::app()->language); }
/** * Raw values such as those used by the header x-axis or y-axis rows/columns need to be translated. An example * is a dropdown where the value is the raw database value and needs to be properly translated for display. * Another example is dynamic __User, where the value is the user id, and needs to be stringified to the User * model. * @param $value * @return string */ public function resolveValueAsLabelForHeaderCell($value, $forExport = false) { $tContent = null; $translatedValue = $value; $resolvedAttribute = $this->getResolvedAttribute(); $displayElementType = $this->getDisplayElementType(); $modelToReportAdapter = $this->makeResolvedAttributeModelRelationsAndAttributesToReportAdapter(); if ($modelToReportAdapter->getModel()->isAttribute($resolvedAttribute) && $modelToReportAdapter->getModel()->isRelation($resolvedAttribute) && !$modelToReportAdapter->getModel()->isOwnedRelation($resolvedAttribute)) { $relationModelClassName = $modelToReportAdapter->getModel()->getRelationModelClassName($resolvedAttribute); $relatedModel = $relationModelClassName::getById((int) $value); if ($relatedModel->isAttribute('serializedLabels')) { $translatedValue = $relatedModel->resolveTranslatedNameByLanguage(Yii::app()->language); } } elseif ($displayElementType == 'User') { $user = User::getById((int) $value); $translatedValue = strval($user); } elseif ($displayElementType == 'DropDown') { $customFieldData = CustomFieldDataModelUtil::getDataByModelClassNameAndAttributeName($this->getResolvedAttributeModelClassName(), $this->getResolvedAttribute()); $dataAndLabels = CustomFieldDataUtil::getDataIndexedByDataAndTranslatedLabelsByLanguage($customFieldData, Yii::app()->language); if (isset($dataAndLabels[$value])) { $translatedValue = $dataAndLabels[$value]; } } elseif ($displayElementType == 'CheckBox') { if ($value) { $translatedValue = Zurmo::t('Core', 'Yes'); } elseif ($value == false && $value != '') { $translatedValue = Zurmo::t('Core', 'No'); } } elseif ($displayElementType == 'GroupByModifierMonth') { $translatedValue = DateTimeUtil::getMonthName($value); } if ($translatedValue === null) { $translatedValue = ''; } if ($this->isALinkableAttribute() && !$forExport) { $modelClassName = get_class($modelToReportAdapter->getModel()); $moduleClassName = $modelToReportAdapter->getModuleClassName(); if (isset($relationModelClassName)) { $modelClassName = $relationModelClassName; } return ReportResultsGridUtil::makeStringForMultipleLinks($value, $modelClassName, $moduleClassName); } return $translatedValue; }