/**
  * Modify User POST
  */
 function modcontact()
 {
     $clang = Yii::app()->lang;
     $action = isset($_GET['action']) ? $_GET['action'] : '';
     if ($action == "modcontact") {
         $contact_id = (int) Yii::app()->request->getPost("contact_id");
         $saluation = flattenText($_POST['saluation'], false, true, 'UTF-8', true);
         $contact_fname = flattenText($_POST['contact_fname'], false, true, 'UTF-8', true);
         $contact_mname = flattenText($_POST['contact_mname'], false, true, 'UTF-8', true);
         $contact_lname = flattenText($_POST['contact_lname'], false, true, 'UTF-8', true);
         $company_id = (int) Yii::app()->request->getPost("company_id");
         $contact_group = (int) Yii::app()->request->getPost("contact_group");
         $contact_group = $contact_group == 0 ? '-1' : $contact_group;
         $IsListProvider = flattenText($_POST['IsListProvider'], false, true, 'UTF-8', true);
         $notes = flattenText($_POST['notes'], false, true, 'UTF-8', true);
         $gender = flattenText($_POST['gender'], false, true, 'UTF-8', true);
         $birthdate = date('Y-m-d', strtotime($_POST['birthdate']));
         $add1 = flattenText($_POST['add1'], false, true, 'UTF-8', true);
         $add2 = flattenText($_POST['add2'], false, true, 'UTF-8', true);
         $add3 = flattenText($_POST['add3'], false, true, 'UTF-8', true);
         $country = (int) Yii::app()->request->getPost("country");
         $zonelist = (int) Yii::app()->request->getPost("zonelist");
         $statelist = (int) Yii::app()->request->getPost("statelist");
         $citylist = (int) Yii::app()->request->getPost("citylist");
         $zip = flattenText($_POST['zip'], false, true, 'UTF-8', true);
         $fax = flattenText($_POST['fax'], false, true, 'UTF-8', true);
         $emsilp = flattenText($_POST['emailp'], false, true, 'UTF-8', true);
         $emails = flattenText($_POST['emails'], false, true, 'UTF-8', true);
         $phonep = flattenText($_POST['phonep'], false, true, 'UTF-8', true);
         $phones = flattenText($_POST['phones'], false, true, 'UTF-8', true);
         $company_type = array();
         if (isset($_POST['company_type'])) {
             $company_type = $_POST['company_type'];
         }
         $ctype = Company_type::model()->findAll(array('select' => 'concat(company_type_id,"__", company_type) as company_type_id , company_type_name'));
         $ctypelist = CHtml::listData($ctype, 'company_type_id', 'company_type_name');
         $IsActive = flattenText(Yii::app()->request->getPost("IsActive"));
         $addsummary = '';
         $aViewUrls = array();
         $is_Active = 0;
         if ($IsActive) {
             $is_Active = 1;
         }
         $sresult = Contact::model()->findAllByAttributes(array('contact_id' => $contact_id));
         $sresultcount = count($sresult);
         if (Permission::model()->hasGlobalPermission('superadmin', 'read') || $sresultcount > 0 && Permission::model()->hasGlobalPermission('contacts', 'update')) {
             if ($emsilp == '' || $phonep == '' || $contact_fname == '') {
                 $aViewUrls['mboxwithredirect'][] = $this->_messageBoxWithRedirect($clang->gT("Failed to edit Contact "), $clang->gT("Could not modify Contact."), "warningheader", $clang->gT("Contact Name or Primary Email or Phone not be empty."), $this->getController()->createUrl('admin/contact/sa/modifycontact'), $clang->gT("Back"), array('contact_id' => $contact_id));
             } elseif (Contact::model()->findByAttributes(array('primary_emailid' => $emsilp), 'contact_id != ' . $contact_id . '')) {
                 $aViewUrls['message'] = array('title' => $clang->gT("Failed to edit Contact "), 'message' => $clang->gT("The Contact Email already exists."), 'class' => 'warningheader');
             } else {
                 $oRecord = Contact::model()->findByPk($contact_id);
                 $oRecord->first_name = $contact_fname;
                 $oRecord->middle_name = $contact_mname;
                 $oRecord->last_name = $contact_lname;
                 $oRecord->saluation = $saluation;
                 $oRecord->contact_group_id = $contact_group;
                 $oRecord->is_list_provider = $IsListProvider;
                 $oRecord->notes = $notes;
                 $oRecord->gender = $gender;
                 $oRecord->birth_date = $birthdate;
                 $oRecord->address1 = $add1;
                 $oRecord->address2 = $add2;
                 $oRecord->address3 = $add3;
                 $oRecord->country_id = $country;
                 $oRecord->zone_id = $zonelist;
                 $oRecord->state_id = $statelist;
                 $oRecord->city_id = $citylist;
                 $oRecord->zip = $zip;
                 $oRecord->fax = $fax;
                 $oRecord->primary_emailid = $emsilp;
                 $oRecord->primary_contact_no = $phonep;
                 $oRecord->other_emailid = $emails;
                 $oRecord->other_contact_no = $phones;
                 $oRecord->IsActive = $is_Active;
                 $oRecord->contact_type_id = CONTACT;
                 $EditContact = $oRecord->save();
                 if ($EditContact) {
                     // When saved successfully
                     $dlt = "DELETE FROM {{map_contact_n_titles}} WHERE contact_id = " . $contact_id;
                     $result = Yii::app()->db->createCommand($dlt)->query();
                     foreach ($ctypelist as $key => $value) {
                         foreach ($company_type as $ky => $vle) {
                             $test = explode('__', $vle);
                             if ($vle == $key) {
                                 $title_ID = (int) Yii::app()->request->getPost($value);
                                 $sql = "INSERT INTO {{map_contact_n_titles}} (contact_id,contact_title_id)\n                                            VALUES('{$contact_id}`', '{$title_ID}')";
                                 $result = Yii::app()->db->createCommand($sql)->query();
                             }
                         }
                     }
                     Yii::app()->setFlashMessage($clang->gT("Contact updated successfully"));
                     $this->getController()->redirect(array("admin/contact/sa/modifycontact/contact_id/{$company_id}/action/modifycompany"));
                 } else {
                     //Saving the user failed for some reason, message about email is not helpful here
                     // Username and/or email adress already exists.
                     $aViewUrls['mboxwithredirect'][] = $this->_messageBoxWithRedirect($clang->gT("Editing contact title"), $clang->gT("Could not modify contact title."), 'warningheader');
                 }
             }
         } else {
             Yii::app()->setFlashMessage(Yii::app()->lang->gT("You do not have sufficient rights to access this page."), 'error');
         }
     } elseif ($action == "modcompany") {
         // table contact_id
         $contact_id = (int) Yii::app()->request->getPost("contact_id");
         // company filed value
         $company_name = flattenText($_POST['company_name'], false, true, 'UTF-8', true);
         $parent_contact = (int) Yii::app()->request->getPost("parent_contact");
         $contact_group = (int) Yii::app()->request->getPost("contact_group");
         $IsListProvider = flattenText($_POST['IsListProvider'], false, true, 'UTF-8', true);
         $notes = flattenText($_POST['notes'], false, true, 'UTF-8', true);
         $add1 = flattenText($_POST['add1'], false, true, 'UTF-8', true);
         $add2 = flattenText($_POST['add2'], false, true, 'UTF-8', true);
         $add3 = flattenText($_POST['add3'], false, true, 'UTF-8', true);
         $country = (int) Yii::app()->request->getPost("country");
         $zonelist = (int) Yii::app()->request->getPost("zonelist");
         $statelist = (int) Yii::app()->request->getPost("statelist");
         $citylist = (int) Yii::app()->request->getPost("citylist");
         $zip = flattenText($_POST['zip'], false, true, 'UTF-8', true);
         $fax = flattenText($_POST['fax'], false, true, 'UTF-8', true);
         $emsilp = flattenText($_POST['emailp'], false, true, 'UTF-8', true);
         $emails = flattenText($_POST['emails'], false, true, 'UTF-8', true);
         $phonep = flattenText($_POST['phonep'], false, true, 'UTF-8', true);
         $phones = flattenText($_POST['phones'], false, true, 'UTF-8', true);
         $RIDCheck = flattenText($_POST['RIDCheck'], false, true, 'UTF-8', true);
         $company_type = array();
         if (isset($_POST['company_type'])) {
             $company_type = $_POST['company_type'];
         }
         foreach ($company_type as $key => $value) {
             $test = explode('__', $value);
             $row[] = $test[1];
         }
         if (in_array('V', $row)) {
             $completionlink = flattenText($_POST['completionlink'], false, true, 'UTF-8', true);
             $disqualifylink = flattenText($_POST['disqualifylink'], false, true, 'UTF-8', true);
             $quatafulllink = flattenText($_POST['quatafulllink'], false, true, 'UTF-8', true);
         } else {
             $completionlink = '';
             $disqualifylink = '';
             $quatafulllink = '';
         }
         $ctype = Company_type::model()->findAll(array('select' => 'concat(company_type_id,"__", company_type) as company_type_id , company_type_name'));
         $ctypelist = CHtml::listData($ctype, 'company_type_id', 'company_type_name');
         $IsActive = flattenText(Yii::app()->request->getPost("IsActive"));
         $addsummary = '';
         $aViewUrls = array();
         $is_Active = 0;
         if ($IsActive) {
             $is_Active = 1;
         }
         $sresult = Contact::model()->findAllByAttributes(array('contact_id' => $contact_id));
         $sresultcount = count($sresult);
         if (Permission::model()->hasGlobalPermission('superadmin', 'read') || $sresultcount > 0 && Permission::model()->hasGlobalPermission('contacts', 'update')) {
             if ($emsilp == '' || $phonep == '' || $company_name == '') {
                 $aViewUrls['mboxwithredirect'][] = $this->_messageBoxWithRedirect($clang->gT("Failed to add Company"), $clang->gT("Could not modify company."), "warningheader", $clang->gT("Company Name or Primary Email or Phone no not be empty."), $this->getController()->createUrl('admin/contact/sa/modifycontact'), $clang->gT("Back"), array('contact_id' => $contact_id));
             } elseif (Contact::model()->findByAttributes(array('company_name' => $company_name), 'contact_id != ' . $contact_id . '')) {
                 $aViewUrls['message'] = array('title' => $clang->gT("Failed to edit Company"), 'message' => $clang->gT("The Company Name already exist."), 'class' => 'warningheader');
             } elseif (Contact::model()->findByAttributes(array('primary_emailid' => $emsilp), 'contact_id != ' . $contact_id . '')) {
                 $aViewUrls['message'] = array('title' => $clang->gT("Failed to edit Company"), 'message' => $clang->gT("The Company Email Id already exist."), 'class' => 'warningheader');
             } else {
                 $oRecord = Contact::model()->findByPk($contact_id);
                 $oRecord->company_name = $company_name;
                 $oRecord->parent_contact_id = $parent_contact;
                 $oRecord->contact_group_id = $contact_group;
                 $oRecord->is_list_provider = $IsListProvider;
                 $oRecord->notes = $notes;
                 $oRecord->address1 = $add1;
                 $oRecord->address2 = $add2;
                 $oRecord->address3 = $add3;
                 $oRecord->country_id = $country;
                 $oRecord->zone_id = $zonelist;
                 $oRecord->state_id = $statelist;
                 $oRecord->city_id = $citylist;
                 $oRecord->zip = $zip;
                 $oRecord->fax = $fax;
                 $oRecord->primary_emailid = $emsilp;
                 $oRecord->primary_contact_no = $phonep;
                 $oRecord->other_emailid = $emails;
                 $oRecord->other_contact_no = $phones;
                 $oRecord->completionlink = $completionlink;
                 $oRecord->disqualifylink = $disqualifylink;
                 $oRecord->quatafulllink = $quatafulllink;
                 $oRecord->IsActive = $is_Active;
                 $oRecord->contact_type_id = COMPANY;
                 $EditCompany = $oRecord->save();
                 if ($EditCompany) {
                     // When saved successfully change mapping table
                     $dlt = "DELETE FROM {{map_company_n_types}} WHERE company_id = " . $contact_id;
                     $result = Yii::app()->db->createCommand($dlt)->query();
                     foreach ($company_type as $ky => $vle) {
                         $test = explode('__', $vle);
                         $sql = "INSERT INTO {{map_company_n_types}} (company_id, company_type_id)\n                                            VALUES('{$contact_id}', '{$test['0']}')";
                         $result = Yii::app()->db->createCommand($sql)->query();
                     }
                     Yii::app()->setFlashMessage($clang->gT("Company updated successfully"));
                     $this->getController()->redirect(array("admin/contact/index"));
                 } else {
                     $aViewUrls['mboxwithredirect'][] = $this->_messageBoxWithRedirect($clang->gT("Editing contact title"), $clang->gT("Could not modify contact title."), 'warningheader');
                 }
             }
         } else {
             Yii::app()->setFlashMessage(Yii::app()->lang->gT("You do not have sufficient rights to access this page."), 'error');
         }
     }
     $this->_renderWrappedTemplate('contacts/contact', $aViewUrls);
 }
        <tr>
            <td align="right" style="text-align: right;">
                <label for='company_type'><?php 
    $clang->eT("Company Type* : ");
    ?>
</label>
            </td>
            <td>
                <?php 
    $test = explode(',', $mrw['company_type_id']);
    $test1 = explode(',', $mrw['company_type']);
    $t = array_combine($test, $test1);
    foreach ($t as $key => $val) {
        $testkey[] = $key . '__' . $val;
    }
    $ctype = Company_type::model()->findAll(array('select' => 'concat(company_type_id,"__", company_type) as company_type_id , company_type_name', 'condition' => "Istitle = '1'"));
    $ctypelist = CHtml::listData($ctype, 'company_type_id', 'company_type_name');
    echo CHtml::checkBoxList('company_type', $testkey, $ctypelist, array('onChange' => 'javascript:hideshow(this.value,this.id)', 'required' => true));
    ?>
            </td>
        </tr>
        <tr>
            <td align="right" style="text-align: right; display: none">
                <label for='IsListProvider'><?php 
    $clang->eT("IsListProvider : ");
    ?>
</label>
            </td>
            <td style="display: none">
                <?php 
    echo CHtml::radioButtonList('IsListProvider', $mrw['is_list_provider'], array('1' => 'Yes', '0' => 'No'), array('separator' => ' '));
 /**
  * Modify User POST
  */
 function modcompany_type()
 {
     $clang = Yii::app()->lang;
     // old Details
     $company_type_id = (int) Yii::app()->request->getPost("company_type_id");
     $company_type_name = flattenText(Yii::app()->request->getPost("company_type_name"));
     $IsActive = flattenText(Yii::app()->request->getPost("IsActive"));
     $IsTitle = flattenText(Yii::app()->request->getPost("IsTitle"));
     $company_type = flattenText(Yii::app()->request->getPost("company_type"));
     //New Name
     $c_type_name = flattenText(Yii::app()->request->getPost("c_type_name"));
     $addsummary = '';
     $aViewUrls = array();
     // update defult value
     $is_Active = $Is_Title = 0;
     if ($IsActive) {
         $is_Active = 1;
     }
     if ($IsTitle) {
         $Is_Title = 1;
     }
     $sresult = Company_type::model()->findAllByAttributes(array('company_type_id' => $company_type_id));
     $sresultcount = count($sresult);
     if (Permission::model()->hasGlobalPermission('superadmin', 'read') || $sresultcount > 0 && Permission::model()->hasGlobalPermission('contacts', 'update')) {
         if ($c_type_name == '') {
             $aViewUrls['mboxwithredirect'][] = $this->_messageBoxWithRedirect($clang->gT("Editing Company Type"), $clang->gT("Could not modify Company Type."), "warningheader", $clang->gT("Company Type  name not be empty."), $this->getController()->createUrl('admin/company_type/sa/modifycontact_group'), $clang->gT("Back"), array('company_type_id' => $company_type_id));
         } elseif (Company_type::model()->findByAttributes(array('company_type_name' => $c_type_name, 'IsActive' => $is_Active, 'company_type' => $company_type, 'Istitle' => $Is_Title))) {
             $aViewUrls['message'] = array('title' => $clang->gT("Failed to Edit Company Type"), 'message' => $clang->gT("The Company Type already exists."), 'class' => 'warningheader');
         } else {
             $oRecord = Company_type::model()->findByPk($company_type_id);
             $oRecord->company_type_name = $this->escape($c_type_name);
             $oRecord->company_type = $this->escape($company_type);
             $oRecord->Istitle = $this->escape($Is_Title);
             $oRecord->IsActive = $this->escape($is_Active);
             $uresult = $oRecord->save();
             // store result of save in uresult
             if ($uresult) {
                 // When saved successfully
                 Yii::app()->setFlashMessage($clang->gT("Company Type updated successfully"));
                 $this->getController()->redirect(array("admin/company_type/index"));
             } else {
                 $aViewUrls['mboxwithredirect'][] = $this->_messageBoxWithRedirect($clang->gT("Editing contact type"), $clang->gT("Could not modify contact type."), 'warningheader');
             }
         }
     } else {
         Yii::app()->setFlashMessage(Yii::app()->lang->gT("You do not have sufficient rights to access this page."), 'error');
         $this->getController()->redirect(array("admin/index"));
     }
     $this->_renderWrappedTemplate('contacts/company_type', $aViewUrls);
 }