/** * actionSelectComponents */ public function actionSelectComponents() { $this->setPageTitle('Select components'); // check if the context supports component selection $rsEvaluationType = EvaluationDetails::model()->with('options')->find('evalElementsId=:element AND evalId=:evaId', [':element' => 5, ':evaId' => $this->evaContextId]); if (!isset($rsEvaluationType)) { Yii::app()->user->setFlash('notice', 'PLease update the evaluation context'); $this->redirect('listEvaContext'); } //print_r($rsEvaluationType); die; if (isset($rsEvaluationType->options) && $rsEvaluationType->options->label == 'System') { Yii::app()->user->setFlash('notice', 'You are planning to evaluate at system level hence no selection' . ' of components is required'); $this->redirect('selectEvaAttributes'); } $model = new DesignForm(); $elements = ContextController::getDefaultElements(); $rules = []; $componentsCriteria = new CDbCriteria(); $componentsCriteria->select = 'componentId, componentName'; $componentsCriteria->condition = 'frameworkId=:framework'; $componentsCriteria->params = [':framework' => $this->frameworkId]; $elements['title'] = '<h3>Select Components</h3>'; $elements['elements'] = ['<p> Please select the components you would like to include in your evaluation from the components' . ' included in this system using the table below. If the components you want to include are not listed' . ' in this table please go back and enter the information about your components into the ' . CHtml::link('add components', ['design/addMultipleComponents']) . ' screen. </p>', '<table id="componentsDisplay" width="100%" border="0" cellspacing="0" cellpadding="0">', '<thead><tr></tr><th></th><th>Component Name</th><th>Target Species</th><th>Data collection point</th>' . '<th>Study type</th></tr></thead>', '<tbody></tbody>', '</table>']; $rules[] = ['components', 'required']; $evaModel = EvaluationHeader::model()->find('evalId=:evalId', [':evalId' => $this->evaContextId]); $model->setPropertyName('components'); $elements['buttons'] = ['save' => ['label' => 'Save', 'type' => 'submit'], 'next' => ['label' => 'Next', 'type' => 'submit']]; $componentsConditions = EvaQuestionGroups::model()->find('section=:section', [':section' => 'evaComponents'])->questions; $availableComponents = json_decode($componentsConditions, true); $requiredComponents = 1; if (isset(array_flip($availableComponents[2])[$this->evaQuestionId])) { $requiredComponents = 2; } //var_dump($availableComponents, $this->evaQuestionId); die; // Get number of components $componentNo = EvaluationDetails::model()->find('evalId=:evaId AND evalElementsId=:evaElement', [':evaId' => $this->evaContextId, ':evaElement' => 6])->value; $rules[] = ['components', 'type', 'type' => 'array']; $rules[] = ['components', 'ext.validators.ComponentNumber', 'requiredComponents' => $requiredComponents, 'declaredComponents' => $componentNo]; //var_dump($requiredComponents, $componentNo); die; $model->setRules($rules); $tableData = DesignController::getComponentData()['componentListArray']; $componentsList = json_encode($tableData); //print_r($model); die; $form = new CForm($elements, $model); //print_r($_POST); die; if ($form->submitted('save') || $form->submitted('next')) { if ($form->validate()) { $evaModel->components = json_encode($form->model->components); // print_r($evaModel->components); die; if ($evaModel->save()) { Yii::app()->user->setFlash('success', 'Components successfully saved'); if (isset($_POST['next'])) { $this->redirect('selectEvaAttributes'); } } else { Yii::app()->user->setFlash('error', 'An error occurred while save the components,' . ' please try again or contact your administrator if this problem persists'); } } } $selectedComponents = is_null($evaModel->components) ? [] : json_decode($evaModel->components); //Get evaluation summary $evaDetails = $this->getEvaDetails(); $this->docName = 'selectComponents'; if (isset($_POST['pageId'])) { SystemController::savePage('selectComponents'); } $page = SystemController::getPageContent($this->docName); if (!isset($page['content']->docData)) { Yii::app()->user->setFlash('notice', 'This page is missing some information, please contact your administrator'); } $this->render('selectComponents', compact('form', 'componentsList', 'selectedComponents', 'page', 'evaDetails')); return; }
/** * actionEditComponent * @access public * @return void */ public function actionEditComponent() { Yii::log("actionEditComponent DesignController called", "trace", self::LOG_CAT); $component = new ComponentHead(); $componentDetails = new ComponentDetails(); $dataArray = []; $dataArray['formType'] = 'Edit'; $model = new DesignForm(); $attributeArray = []; if (empty($_GET['id'])) { Yii::app()->user->setFlash('error', Yii::t("translation", "Please select a component to edit")); $this->redirect(['design/listComponents']); } if (!empty(Yii::app()->session['surDesign'])) { //fetch the form data $fetchComponentData = Yii::app()->db->createCommand()->select('cd.componentDetailId, cd.componentId, cd.subFormId, cd.value, sd.inputName, sd.inputType,ch.componentName, op.optionId, op.label')->from('componentDetails cd')->join('surFormDetails sd', 'sd.subFormId = cd.subFormId')->join('componentHead ch', 'ch.componentId = cd.componentId')->leftJoin('options op', 'op.componentId = sd.subFormId')->where('cd.componentId =' . $_GET['id'])->queryAll(); //print_r($fetchComponentData); //arrange data in array $componentData = []; $returnArray = self::getElementsAndDynamicAttributes(); $returnArray['elements']['buttons'] = ['updateComponent' => ['type' => 'submit', 'label' => 'Update component']]; $elements = $returnArray['elements']; //$model = new ComponentsForm; $model->setProperties($returnArray['dynamicDataAttributes']); $model->setAttributeLabels($returnArray['dynamicLabels']); $model->setRules($returnArray['dynamicRules']); // update the model with the data values and add id foreach ($fetchComponentData as $dat) { $compKey = $dat['inputName'] . "_" . $dat['subFormId']; $componentData[$compKey] = ['value' => $dat['value'], 'id' => $dat['componentDetailId']]; $model->{$compKey} = $dat['value']; // add the component name to the array as well but just once if (empty($componentData['componentName'])) { $componentData['componentName'] = $dat['componentName']; $model->componentName = $dat['componentName']; } } //var_dump($model, $fetchComponentData); die; // generate the components form $form = new CForm($elements, $model); if (Yii::app()->getRequest()->getIsAjaxRequest()) { echo CActiveForm::validate([$model]); Yii::app()->end(); } //validate and save the component data if ($form->submitted('DesignForm') && $form->validate()) { //print_r($form->getModel()); die(); $component->setIsNewRecord(false); $component->componentName = $form->model->componentName; $component->frameworkId = Yii::app()->session['surDesign']['id']; $component->componentId = $_GET['id']; //save the componentHead values $component->save(); $form->model->unsetProperties(['componentName']); //var_dump($form->model); die; $componentId = $_GET['id']; // fetch the form data foreach ($form->model as $key => $val) { $componentDetails = new ComponentDetails(); // record found edit record if (!empty($componentData[$key])) { $componentDetails->setIsNewRecord(false); $componentDetails->componentDetailId = $componentData[$key]['id']; $componentDetails->componentId = $componentId; $componentDetails->value = $val; $componentDetails->save(false); // record not found add new record } else { $componentDetails->setIsNewRecord(true); $componentDetails->componentDetailId = null; $componentDetails->componentId = $componentId; $params = explode("_", $key); $componentDetails->subFormId = $params[1]; $componentDetails->value = $val; $componentDetails->save(); } } Yii::app()->user->setFlash('success', Yii::t("translation", "Component successfully updated")); $this->redirect(['listComponents']); } $this->render('component', ['model' => $model, 'dataArray' => $dataArray, 'form' => $form]); } }