public static function renderModelInput(CModel $model, $field, $htmlOptions = array()) { if (!$field->asa('CommonFieldsBehavior')) { throw new Exception('$field must have CommonFieldsBehavior'); } if ($field->required) { if (isset($htmlOptions['class'])) { $htmlOptions['class'] .= ' x2-required'; } else { $htmlOptions = array_merge(array('class' => 'x2-required'), $htmlOptions); } } $fieldName = $field->fieldName; if (!isset($field)) { return null; } switch ($field->type) { case 'text': return CHtml::activeTextArea($model, $field->fieldName, array_merge(array('title' => $field->attributeLabel), array_merge(array('encode' => false), $htmlOptions))); case 'date': $oldDateVal = $model->{$fieldName}; $model->{$fieldName} = Formatter::formatDate($model->{$fieldName}, 'medium'); Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker'); $pickerOptions = array('dateFormat' => Formatter::formatDatePicker(), 'changeMonth' => false, 'changeYear' => true); if (Yii::app()->getLanguage() === 'fr') { $pickerOptions['monthNamesShort'] = Formatter::getPlainAbbrMonthNames(); } $input = Yii::app()->controller->widget('CJuiDateTimePicker', array('model' => $model, 'attribute' => $fieldName, 'mode' => 'date', 'options' => $pickerOptions, 'htmlOptions' => array_merge(array('title' => $field->attributeLabel), $htmlOptions), 'language' => Yii::app()->language == 'en' ? '' : Yii::app()->getLanguage()), true); $model->{$fieldName} = $oldDateVal; return $input; case 'dateTime': $oldDateTimeVal = $model->{$fieldName}; $pickerOptions = array('dateFormat' => Formatter::formatDatePicker('medium'), 'timeFormat' => Formatter::formatTimePicker(), 'ampm' => Formatter::formatAMPM(), 'changeMonth' => true, 'changeYear' => true); if (Yii::app()->getLanguage() === 'fr') { $pickerOptions['monthNamesShort'] = Formatter::getPlainAbbrMonthNames(); } $model->{$fieldName} = Formatter::formatDateTime($model->{$fieldName}); Yii::import('application.extensions.CJuiDateTimePicker.CJuiDateTimePicker'); $input = Yii::app()->controller->widget('CJuiDateTimePicker', array('model' => $model, 'attribute' => $fieldName, 'mode' => 'datetime', 'options' => $pickerOptions, 'htmlOptions' => array_merge(array('title' => $field->attributeLabel), $htmlOptions), 'language' => Yii::app()->language == 'en' ? '' : Yii::app()->getLanguage()), true); $model->{$fieldName} = $oldDateTimeVal; return $input; case 'dropdown': // Note: if desired to translate dropdown options, change the seecond argument to // $model->module $om = $field->getDropdownOptions(); $multi = (bool) $om['multi']; $dropdowns = $om['options']; $curVal = $multi ? CJSON::decode($model->{$field->fieldName}) : $model->{$field->fieldName}; $ajaxArray = array(); if ($field instanceof Fields) { $dependencyCount = X2Model::model('Dropdowns')->countByAttributes(array('parent' => $field->linkType)); $fieldDependencyCount = X2Model::model('Fields')->countByAttributes(array('modelName' => $field->modelName, 'type' => 'dependentDropdown', 'linkType' => $field->linkType)); if ($dependencyCount > 0 && $fieldDependencyCount > 0) { $ajaxArray = array('ajax' => array('type' => 'GET', 'url' => Yii::app()->controller->createUrl('/site/dynamicDropdown'), 'data' => 'js:{ "val":$(this).val(), "dropdownId":"' . $field->linkType . '", "field":true, "module":"' . $field->modelName . '" }', 'success' => ' function(data){ if(data){ data=JSON.parse(data); if(data[0] && data[1]){ $("#' . $field->modelName . '_"+data[0]).html(data[1]); } } }')); } } $htmlOptions = array_merge($htmlOptions, $ajaxArray, array('title' => $field->attributeLabel)); if ($multi) { $multiSelectOptions = array(); if (!is_array($curVal)) { $curVal = array(); } foreach ($curVal as $option) { $multiSelectOptions[$option] = array('selected' => 'selected'); } $htmlOptions = array_merge($htmlOptions, array('options' => $multiSelectOptions, 'multiple' => 'multiple')); } elseif ($field->includeEmpty) { $htmlOptions = array_merge($htmlOptions, array('empty' => Yii::t('app', "Select an option"))); } return CHtml::activeDropDownList($model, $field->fieldName, $dropdowns, $htmlOptions); case 'dependentDropdown': return CHtml::activeDropDownList($model, $field->fieldName, array('' => '-'), array_merge(array('title' => $field->attributeLabel), $htmlOptions)); case 'link': $linkSource = null; $linkId = ''; $name = ''; if (class_exists($field->linkType)) { // Create a model for autocompletion: if (!empty($model->{$fieldName})) { list($name, $linkId) = Fields::nameAndId($model->{$fieldName}); $linkModel = X2Model::getLinkedModelMock($field->linkType, $name, $linkId, true); } else { $linkModel = X2Model::model($field->linkType); } if ($linkModel instanceof X2Model && $linkModel->asa('X2LinkableBehavior') instanceof X2LinkableBehavior) { $linkSource = Yii::app()->controller->createUrl($linkModel->autoCompleteSource); $linkId = $linkModel->id; $oldLinkFieldVal = $model->{$fieldName}; $model->{$fieldName} = $name; } } static $linkInputCounter = 0; $hiddenInputId = $field->modelName . '_' . $fieldName . "_id" . $linkInputCounter++; $input = CHtml::hiddenField($field->modelName . '[' . $fieldName . '_id]', $linkId, array('id' => $hiddenInputId)) . Yii::app()->controller->widget('zii.widgets.jui.CJuiAutoComplete', array('model' => $model, 'attribute' => $fieldName, 'source' => $linkSource, 'value' => $name, 'options' => array('minLength' => '1', 'select' => 'js:function( event, ui ) { $("#' . $hiddenInputId . '"). val(ui.item.id); $(this).val(ui.item.value); return false; }', 'create' => $field->linkType == 'Contacts' ? 'js:function(event, ui) { $(this).data( "uiAutocomplete" )._renderItem = function(ul,item) { return $("<li>").data("item.autocomplete",item). append(x2.forms.renderContactLookup(item)). appendTo(ul); }; }' : ($field->linkType == 'BugReports' ? 'js:function(event, ui) { $(this).data( "uiAutocomplete" )._renderItem = function( ul, item ) { var label = "<a style=\\"line-height: 1;\\">" + item.label; label += "<span style=\\"font-size: 0.6em;\\">"; // add email if defined if(item.subject) { label += "<br>"; label += item.subject; } label += "</span>"; label += "</a>"; return $( "<li>" ) .data( "item.autocomplete", item ) .append( label ) .appendTo( ul ); }; }' : '')), 'htmlOptions' => array_merge(array('title' => $field->attributeLabel), $htmlOptions)), true); if (isset($oldLinkFieldVal)) { $model->{$fieldName} = $oldLinkFieldVal; } return $input; case 'rating': return Yii::app()->controller->widget('X2StarRating', array('model' => $model, 'attribute' => $field->fieldName, 'readOnly' => isset($htmlOptions['disabled']) && $htmlOptions['disabled'], 'minRating' => Fields::RATING_MIN, 'maxRating' => Fields::RATING_MAX, 'starCount' => Fields::RATING_MAX - Fields::RATING_MIN + 1, 'cssFile' => Yii::app()->theme->getBaseUrl() . '/css/rating/jquery.rating.css', 'htmlOptions' => $htmlOptions, 'callback' => 'function(value, link){ if (typeof x2 !== "undefined" && typeof x2.InlineEditor !== "undefined" && typeof x2.InlineEditor.ratingFields !== "undefined") { x2.InlineEditor.ratingFields["' . $field->modelName . '[' . $field->fieldName . ']"] = value; } }'), true); case 'boolean': $checkbox = CHtml::openTag('div', X2Html::mergeHtmlOptions($htmlOptions, array('class' => 'checkboxWrapper'))); $checkbox .= CHtml::activeCheckBox($model, $field->fieldName, array_merge(array('unchecked' => 0, 'title' => $field->attributeLabel), $htmlOptions)); $checkbox .= CHtml::closeTag('div'); return $checkbox; case 'assignment': $oldAssignmentVal = $model->{$fieldName}; $model->{$fieldName} = !empty($model->{$fieldName}) ? $field->linkType == 'multiple' && !is_array($model->{$fieldName}) ? explode(', ', $model->{$fieldName}) : $model->{$fieldName} : X2Model::getDefaultAssignment(); $dropdownList = CHtml::activeDropDownList($model, $fieldName, X2Model::getAssignmentOptions(true, true), array_merge(array('title' => $field->attributeLabel, 'id' => $field->modelName . '_' . $fieldName . '_assignedToDropdown', 'multiple' => $field->linkType == 'multiple' ? 'multiple' : null), $htmlOptions)); $model->{$fieldName} = $oldAssignmentVal; return $dropdownList; case 'optionalAssignment': // optional assignment for users (can be left blank) $users = User::getNames(); unset($users['Anyone']); return CHtml::activeDropDownList($model, $fieldName, $users, array_merge(array('title' => $field->attributeLabel, 'empty' => ''), $htmlOptions)); case 'visibility': $permissionsBehavior = Yii::app()->params->modelPermissions; return CHtml::activeDropDownList($model, $field->fieldName, $permissionsBehavior::getVisibilityOptions(), array_merge(array('title' => $field->attributeLabel, 'id' => $field->modelName . "_visibility"), $htmlOptions)); // 'varchar', 'email', 'url', 'int', 'float', 'currency', 'phone' // case 'int': // return CHtml::activeNumberField($model, $field->fieldNamearray_merge(array( // 'title' => $field->attributeLabel, // ), $htmlOptions)); // 'varchar', 'email', 'url', 'int', 'float', 'currency', 'phone' // case 'int': // return CHtml::activeNumberField($model, $field->fieldNamearray_merge(array( // 'title' => $field->attributeLabel, // ), $htmlOptions)); case 'percentage': $htmlOptions['class'] = empty($htmlOptions['class']) ? 'input-percentage' : $htmlOptions['class'] . ' input-percentage'; return CHtml::activeTextField($model, $field->fieldName, array_merge(array('title' => $field->attributeLabel), $htmlOptions)); case 'currency': $fieldName = $field->fieldName; $elementId = isset($htmlOptions['id']) ? '#' . $htmlOptions['id'] : '#' . $field->modelName . '_' . $field->fieldName; Yii::app()->controller->widget('application.extensions.moneymask.MMask', array('element' => $elementId, 'currency' => Yii::app()->params['currency'], 'config' => array('affixStay' => true, 'decimal' => Yii::app()->locale->getNumberSymbol('decimal'), 'thousands' => Yii::app()->locale->getNumberSymbol('group')))); return CHtml::activeTextField($model, $field->fieldName, array_merge(array('title' => $field->attributeLabel, 'class' => 'currency-field'), $htmlOptions)); case 'credentials': $typeAlias = explode(':', $field->linkType); $type = $typeAlias[0]; if (count($typeAlias) > 1) { $uid = Credentials::$sysUseId[$typeAlias[1]]; } else { $uid = Yii::app()->user->id; } return Credentials::selectorField($model, $field->fieldName, $type, $uid); case 'timerSum': // Sorry, no-can-do. This is field derives its value from a sum over timer records. return $model->renderAttribute($field->fieldName); case 'float': case 'int': if (isset($model->{$fieldName})) { $oldNumVal = $model->{$fieldName}; $model->{$fieldName} = Yii::app()->locale->numberFormatter->formatDecimal($model->{$fieldName}); } $input = CHtml::activeTextField($model, $field->fieldName, array_merge(array('title' => $field->attributeLabel), $htmlOptions)); if (isset($oldNumVal)) { $model->{$fieldName} = $oldNumVal; } return $input; default: return CHtml::activeTextField($model, $field->fieldName, array_merge(array('title' => $field->attributeLabel), $htmlOptions)); // array( // 'tabindex'=>isset($item['tabindex'])? $item['tabindex'] : null, // 'disabled'=>$item['readOnly']? 'disabled' : null, // 'title'=>$field->attributeLabel, // 'style'=>$default?'color:#aaa;':null, // )); } }