/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Value();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Value'])) {
         $model->attributes = $_POST['Value'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        if (isset($_POST['deleteValID'])) {
            Value::model()->deleteAll('value_id=' . $_POST['deleteValID']);
            ValueDescription::model()->deleteAll('value_id=' . $_POST['deleteValID']);
            Attribute2value::model()->deleteAll('value_id=' . $_POST['deleteValID']);
            echo 'success';
            exit;
        }
        $lang_model = new LanguageModel;
        $AttrDescModel = new AttributeDescription;
        $Attribute2value = $this->loadAttrToValModel('attribute_id=' . $id);

        $ValueModel = new Value;
        $ValueDescription = new ValueDescription;

        $aLangNames = array();
        $aValNames = array();
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        $allLangs = $lang_model->findAll('1', array('order' => 'id ASC'));
        $allLangsJS = array();
        $aValuesNames = array();
        foreach ($allLangs as $language) {
          $allLangsJS[$language['id']] = $language['code'];

          foreach ($Attribute2value as $record) {
            $aNames = $ValueDescription->find('language_id=' . $language['id'] . ' AND value_id=' . $record['value_id']);
            $aValuesNames[$record['value_id']][$language['code']]['value_id'] = $record['value_id'];
            $aValuesNames[$record['value_id']][$language['code']]['value_name'] = $aNames['value_name'];
            $aValuesNames[$record['value_id']][$language['code']]['language_id'] = $language['id'];
          }
        }

        $aAttrNames = $AttrDescModel->findAll("attribute_id = '" . $id . "'");
        foreach ($aAttrNames as $lng_attribute) {
            $aLangNames[$lng_attribute['language_id']] = $lng_attribute['attribute_name'];
        }

        if (isset($_POST['Attribute'])) {
            $model->attributes = $_POST['Attribute'];
            $model->updateByPk($id, $model->attributes);
            if (isset($_POST['AttributeDescription']['attribute_name'])) {
                foreach ($_POST['AttributeDescription']['attribute_name'] as $langId => $attrName) {
                    $attr_desc_model = new AttributeDescription;
                    $attr_desc_model->updateByPk(array('attribute_id' => $id, 'language_id' => $langId), array('attribute_name' => $attrName));
                }
            }

            if (isset($_POST['NewAttrValue'])) {
                foreach ($_POST['NewAttrValue'] as $spec_id => $new_val) {
                    $ValueModel = new Value;
                    $ValueModel->attributes = array(
                        'status' => 1,
                    );
                    $ValueModel->save();
                    $v_id = $ValueModel->getPrimaryKey();

                    foreach ($new_val as $lang_id => $value) {
                        if ($value != '') {
                            $ValueDescription = new ValueDescription;
                            $ValueDescription->attributes = array(
                                'value_id' => $v_id,
                                'language_id' => $lang_id,
                                'value_name' => $value,
                            );
                            $ValueDescription->save();
                        }
                    }

                    $Attribute2value = new Attribute2value;
                    $Attribute2value->attributes = array('attribute_id' => $id, 'value_id' => $v_id);
                    $Attribute2value->save();
                }
            }
            if (isset($_POST['ValueDescription']['value_name'])) {
                foreach ($_POST['ValueDescription']['value_name'] as $language_id => $lang_values) {
                    foreach ($lang_values as $value_id => $value_name) {
                        $value_desc_model = new ValueDescription;
                        $value_desc_model->updateByPk(array('value_id' => $value_id, 'language_id' => $language_id), array('value_name' => $value_name));
                    }
                }

            }
            $this->redirect(array('index'));
        }

        $this->render('update', array(
            'model' => $model,
            'allLangs' => $allLangs,
            'AttrDescModel' => $AttrDescModel,
            'aLangNames' => $aLangNames,
            'allLangsJS' => json_encode($allLangsJS),
            'aValuesNames' => $aValuesNames,
            'ValueDescription' => $ValueDescription,
        ));
    }
 public function addGrid($element, $rows, $columns)
 {
     $element->stepId = $this->id;
     $element->position = $this->elementCount + 1;
     $toReturn = $element->save();
     $idElement = $element->getPrimaryKey();
     foreach ($rows as $key => $value) {
         $row = new Value();
         $row->value = $value;
         $row->elementId = $idElement;
         $row->colonne = false;
         $row->save();
     }
     foreach ($columns as $key => $value) {
         $column = new Value();
         $column->value = $value;
         $column->elementId = $idElement;
         $column->colonne = true;
         $column->save();
     }
     return $toReturn;
 }