/** * @param string $evalId * @param bool $newRecord * @return bool */ public function save($evalId, $newRecord = true) { // fetch the form data $evaElements = []; $model = new EvaluationDetails(); $transaction = Yii::app()->db->beginTransaction(); try { if (!$newRecord) { $model->deleteAll('evalId=:evaId', [':evaId' => $evalId]); } foreach ($this->_properties as $attrNameAndId => $attrVal) { $model->unsetAttributes(); $attrParams = explode("_", $attrNameAndId); $evaElements['evalId'] = $evalId; $evaElements['evalElementsId'] = $attrParams[1]; $evaElements['value'] = is_array($attrVal) ? json_encode($attrVal) : $attrVal; $model->attributes = $evaElements; //print_r($model->attributes); die; $model->setIsNewRecord(true); $model->save(); } //die; $transaction->commit(); } catch (Exception $e) { Yii::log($e->getMessage(), 'error', 'models.EvalForm'); $transaction->rollBack(); EvaluationHeader::model()->deleteByPk($evalId); return false; } //var_dump($evaElements, $model); die; return true; }
/** * getElementsAndDynamicAttributes * @param array $componentData * @access public * @return array */ public function getElementsAndDynamicAttributes($componentData = []) { $elements = []; $attributeArray = []; $dynamicDataAttributes = []; //$getFormCondition = 't.formId=:formId'; //$getFormParams = array(':formId' => 1); $getForm = EvaluationElements::model()->findAll(); //$elements['title'] = "Components Form"; $elements['showErrorSummary'] = true; $elements['showErrors'] = true; $elements['errorSummaryHeader'] = Yii::app()->params['headerErrorSummary']; $elements['errorSummaryFooter'] = Yii::app()->params['footerErrorSummary']; $elements['activeForm']['id'] = "EvalForm"; $elements['activeForm']['enableClientValidation'] = true; $elements['activeForm']['clientOptions'] = ['validateOnSubmit' => true]; //$elements['activeForm']['enableAjaxValidation'] = false; $elements['activeForm']['class'] = 'CActiveForm'; //print_r($getForm); die(); $evalElements = $getForm; $dataArray['getForm'] = $elements; $inputType = 'text'; //$dynamicDataAttributes['frameworkId'] = 'frameworkId'; //$rules[] = ['frameworkId', 'required']; $designData = FrameworkContext::model()->findAll(['condition' => 'userId=:userId', 'params' => [':userId' => Yii::app()->user->id]]); $designItems = []; // process the dropdown data into an array foreach ($designData as $params) { $designItems[$params->frameworkId] = $params->name; } $elements['elements'] = EvaluationHeader::getElements(); // add the dropdown items to the element //$elements['elements']['frameworkId']['items'] = $designItems; $elements['elements']['evaContext']['type'] = 'form'; //$elements['elements']['evaluationName']['layout'] = '{label} {input} {hint} {error}'; $rules = []; $labels = []; foreach ($evalElements as $element) { //set the model attribute array $attributeId = $element->inputName . "_" . $element->evalElementsId; $dynamicDataAttributes[$attributeId] = ''; $validation = $element->required ? 'required' : 'safe'; $rules[] = [$attributeId, $validation]; if ($element->inputName == 'componentNo') { $rules[] = [$attributeId, 'numerical']; } $highlightClass = ""; if (isset($attributeArray[$element->evalElementsId])) { $highlightClass = "attributeHighlight"; } $labels[$attributeId] = $element->label; // add the elements to the CForm array $elements['elements']['evaContext']['elements'][$attributeId] = ['label' => $element->label, 'required' => $element->required, 'type' => $element->inputType, 'class' => $highlightClass, 'title' => UtilModel::urlToLink($element->elementMetaData), 'data-field' => $element->evalElementsId]; // Add an image icon that will be displayed on the ui to show more info $button = CHtml::image('', '', ['id' => 'moreInfoButton' . $element->evalElementsId, 'style' => 'cursor:pointer', 'class' => 'ui-icon ui-icon-info', 'title' => 'More Information', 'onClick' => '$("#moreInfoDialog").html($("#popupData' . $element->evalElementsId . '").html());$("#moreInfoDialog").dialog("open")']); // Add the image icon and information to the layout/ui // if (!empty($element->moreInfo) && !empty($element->url) && !empty($element->description)) { // $elements['elements']['evaContext']['elements'][$attributeId]['layout'] = '{label}<div class="componentImagePopup">' . $button . // '</div>{hint} {input}' . '<div id="popupData' . $element->evalElementsId . '" style="display:none">' . $element->moreInfo . '</div>' . // '<div class="componentDataPopup">' . $element->description . // ' <br/> <a href=' . $element->url . ' target=_blank>' . $element->url . '</a></div> {error}'; // } // add the values to the form if (!empty($componentData[$attributeId])) { $elements['elements']['evaContext']['elements'][$attributeId]['value'] = $componentData[$attributeId]['value']; } // add the component name element value if (!empty($componentData['evaluationName'])) { $elements['elements']['evaContext']['elements']['evaluationName']['value'] = $componentData['evaluationName']; } // add the frameworkId element value if (!empty($componentData['frameworkId'])) { $elements['elements']['evaContext']['elements']['frameworkId']['value'] = $componentData['frameworkId']; } //add the dropdown parameters if ($element->inputType == 'dropdownlist') { $items = CHtml::listData(Options::model()->findAll(['condition' => 'elementId=:elementId', 'params' => [':elementId' => $element->evalElementsId]]), 'optionId', 'label'); // add the dropdown items to the element $elements['elements']['evaContext']['elements'][$attributeId]['items'] = $items; $elements['elements']['evaContext']['elements'][$attributeId]['prompt'] = 'Choose one'; } if ($element->inputName == 'currentCost' || $element->inputName == 'budgetLimit') { $elements['elements']['evaContext']['elements'][$attributeId]['class'] = 'update-able'; } } $elements['buttons'] = ['newEvaluation' => ['type' => 'submit', 'label' => 'Create evaluation context']]; $returnArray = ['elements' => $elements, 'dynamicDataAttributes' => $dynamicDataAttributes, 'labels' => $labels, 'rules' => $rules]; return $returnArray; }