<?php  


function module_list($name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array('customer' => _("Customer"),'supplier' => _("Supplier"), 'opportunities' => _("Opportunites"),'leads' => _("Leads"),'task' => _("Task"),'project' => _("Project"),'branch' => _("Branch"));

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?

}
function custom_filed_list($name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array('text' => _("Text"),'amount' => _("Amount"), 'dropdown' => _("Drop down"),'checkbox'=>_("Check box"),'radiobutton'=>_('Radio button'));

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?

}

function field_item_list($count ,$name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array();
      for ($i=1; $i <=$count; $i++) { 
          $items[$i] =$i;
      }

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?

}
function draw_dropdown($list,$name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array();
      for ($i=1; $i <=count($list); $i++) { 
          $items[$list[$i]] =$list[$i];
      }

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?

}

function draw_radiobutton($list,$name,$selected_id=null){
   // radio($list[1],$name,$list[1], null, true);
      $radio ='';
      display_error(json_encode($list));
      for ($i=1; $i <=count($list); $i++) {
        if($list[$i] != $selected_id){
          $selected_id =null;
        } 
        $radio .= radio($list[$i],$name,$list[$i], null);  
      }
      return $radio;

}
function draw_position_radiobutton($name,$selected_id=null){
   // radio($list[1],$name,$list[1], null, true);
      $radio ='';
      $select =null;
      $list=array(1=>'left',2=>'right');
      for ($i=1; $i <=count($list); $i++) {
        if($list[$i] == $selected_id){
          $select =null;
        } 
        $radio .= radio($list[$i],$name,$list[$i], null);  
      }
      return $radio;

}

function custom_field_section($module,$ref_id,$section_id){
  //for custom fields
  $field_values= array();
  if($section_id == 0 ||$section_id == 1 ){
    $section_position ='left';
  }elseif($section_id == 2){
    $section_position ='right';
  }
    // if($ref_id){ 
  
  //   $field_values =get_custom_values($module,$ref_id);
  //   $field_names =GetDataFilter('custom_fields',array('field_name'),array("module"=>$module));
  //   if($field_values){
  //     foreach ($field_names as $key => $value) {
  //       $_POST[$field_names[$key]['field_name']] = ($field_values[$field_names[$key]['field_name']] != '' ? $field_values[$field_names[$key]['field_name']] : '' );
  //     }
  //   }
  // }

  $data =GetDataJoin('custom_fields AS fields',array(
    0=>array('join' => 'INNER', 'table_name' => 'custom_headers AS headers', 'conditions' => 'fields.header_id = headers.id')),array('fields.*','headers.*'),array('fields.module'=>$module)
  );
  // $result =get_custom_fields($module);
  $result =$data;
  if($result){
    foreach ($result as $myrow) {
        if($myrow['position'] ==$section_position){
          $_POST[$myrow['field_name']] =null;
        }
    }
    if($ref_id){
    $field_values =get_custom_values($module,$ref_id);

    // $field_names =GetDataFilter('custom_fields',array('field_name'),array("module"=>$module));
    if($field_values){
      foreach ($field_names as $key => $value) {
        if($myrow['position'] ==$section_position){
          $_POST[$field_names[$key]['field_name']] =$field_values[$field_names[$key]['field_name']];
        }
      }
    }
  } 
  table_section_title(_("Custom Fields"));
    foreach ($result as $myrow) {
      if(!isset($_POST[$myrow['field_name']]) || !$field_values){
        $_POST[$myrow['field_name']] =null;
      }
      if($myrow['field']=='text'){
        text_row(_($myrow['label']), $myrow['field_name'], $_POST[$myrow['field_name']], 32, 30);
      }else if($myrow['field']=='amount'){
        amount_row(_($myrow['label']), $myrow['field_name'], $_POST[$myrow['field_name']]);
      }elseif ($myrow['field']=='dropdown') {
        label_row(_($myrow['label']),draw_dropdown(unserialize(base64_decode($myrow['field_items'])),$myrow['field_name'],$_POST[$myrow['field_name']],false));
      }elseif ($myrow['field']=='checkbox') {
        check_row(_($myrow['label']),$myrow['field_name'],$_POST[$myrow['field_name']]);
      }elseif ($myrow['field']=='radiobutton') {
        label_row(_($myrow['label']),draw_radiobutton(unserialize(base64_decode($myrow['field_items'])),$myrow['field_name'],$_POST[$myrow['field_name']]));
      }
    }
  }
 //end custom fields
}


function custom_field_update($module,$ref_id){

  //for update custom value 
  $result =get_custom_fields($module);
  if($result){
    $values = array();
    foreach ($result as $myrow) {
     
      if ($myrow['field']=='amount') {
        $values[$myrow['field_name']] =input_num($_POST[$myrow['field_name']]);
      }elseif ($myrow['field']=='checkbox') {
        $values[$myrow['field_name']] =check_value($myrow['field_name']);
      }else{
        $values[$myrow['field_name']] =$_POST[$myrow['field_name']];
      }
    }
    // if(has_custom_value($module,$ref_id)){
    //   update_custom_values($module,$ref_id,base64_encode(serialize($values)));
    // }else{
      add_custom_values($module,$ref_id,$values);
    // }
  }
  //end update custom value

}



function custom_field_insert($module,$ref_id){

  $result =get_custom_fields($module);
    if($result){
      $values = array();
      foreach ($result as $myrow) {
          // if ($myrow['field']=='amount' && !is_numeric($_POST[$myrow['field_name']])) {
          //   display_error($myrow['label']." must be a numeric value");
          //   cancel_transaction();
          //   return false;
          // }
          if ($myrow['field']=='checkbox') {
            $values[$myrow['field_name']] =['label'=>$myrow['label'],'value'=>check_value($myrow['field_name'])];
          }else{
            $values[$myrow['field_name']] =['label'=>$myrow['label'],'value'=>$_POST[$myrow['field_name']]];
          }
      }
      add_custom_values($module,$ref_id,base64_encode(serialize($values))); 
    }

}

function custom_header_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false,$module){
    $sql = "SELECT * FROM custom_headers WHERE module = ".db_escape($module);

    $ret = combo_input($name, $selected_id, $sql, 'id', 'header_label',
    array(
        'spec_option' => $spec_option === true ? _("All Users") : $spec_option,
        'spec_id' => 0,
        'select_submit'=> $submit_on_change,
        'async' => false,
    ),'' );
    
    return $ret;
} 

function custom_header_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false,
  $module){
    if ($label != null)
        echo "<td>$label</td>\n";
    echo "<td nowrap>";
    echo custom_header_list($name, $selected_id, $all_option, $submit_on_change,$module);
    echo "</td>\n";
}
?>