<?php
  //ini_set('display_errors', 1);
  //ini_set('display_startup_errors', 1);
  //error_reporting(E_ALL);
  session_start();

  if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  
  global $app_list_strings, $mod_strings, $app_strings, $sugar_config, $current_user, $moduleList;

  $site_base_url = $sugar_config['site_url'];
  $_SESSION['site_base_url'] = $site_base_url;

  $getall = $_REQUEST['getall'];

  if($getall != null){ $getall = $getall; }else{ $getall = 1; }

  function get_settings_grouping_options($module) { require_once 'modules/ModuleBuilder/parsers/ParserFactory.php'; $availableFields = array(); $parser_editview = ParserFactory::getParser("editview", $module); $parser_listview = ParserFactory::getParser("listview", $module); $parser_detailview = ParserFactory::getParser("detailview", $module); $parser_array = array($parser_editview, $parser_listview, $parser_detailview); foreach ($parser_array as $single_parser) { foreach ($single_parser->_fielddefs as $key => $def) { if($def['type'] == 'enum' || $def['type'] == 'DynamicEnum' || $def['type'] == 'date' || $def['type'] == 'datetime') { if($module == "nj_Attendance" || $module == "Meetings"){ if($def['name'] != 'last_email_verification_c' && $def['name'] != 'birthdate'){ $single_field['field_name'] = $key; $single_field['label'] = preg_replace("/([:]|\xEF\xBC\x9A)[\\s]*$/", "", translate(isset($def['label']) ? $def['label'] : $def['vname'], $module)); $single_field['type'] = $def['type']; if (!in_array($single_field, $availableFields['primarygroup'])) { if($def['type'] == 'date' || $def['type'] == 'datetime' || $def['type'] == 'datetimecombo'){ $availableFields['primarygroup'][] = $single_field; } } if (!in_array($single_field, $availableFields['secondarygroup'])) { if($def['type'] == 'enum'){ $availableFields['secondarygroup'][] = $single_field; } } } }else{ if($def['name'] != 'date_entered' && $def['name'] != 'date_modified' && $def['name'] != 'last_email_verification_c' && $def['name'] != 'birthdate'){ $single_field['field_name'] = $key; $single_field['label'] = preg_replace("/([:]|\xEF\xBC\x9A)[\\s]*$/", "", translate(isset($def['label']) ? $def['label'] : $def['vname'], $module)); $single_field['type'] = $def['type']; if (!in_array($single_field, $availableFields['primarygroup'])) { if($def['type'] == 'date' || $def['type'] == 'datetime' || $def['type'] == 'datetimecombo'){ $availableFields['primarygroup'][] = $single_field; } } if (!in_array($single_field, $availableFields['secondarygroup'])) { if($def['type'] == 'enum'){ $availableFields['secondarygroup'][] = $single_field; } } } } } } } return $availableFields; }
  
  $lat_enabled_modules = json_decode($sugar_config['web_dashboard']['enabled_modules'], true);
  // print_r($lat_enabled_modules);
  // echo $sugar_config['web_dashboard']['enabled_modules'];
  if(empty($lat_enabled_modules)){
    echo "Configuration is missing for web dashboard. Please check configuration file.";
    die;
  }

  $temp_lat_enabled_modules = array();

  foreach ($lat_enabled_modules as $key => $modulename) {
    $temp_lat_enabled_modules[$key]['module_name'] = $modulename;
    $bean = BeanFactory::newBean($modulename);
    foreach ($bean->field_name_map as $k => $module_details) {
    if($module_details['type'] == 'enum' || $module_details['type'] == 'datetime' || $module_details['type'] == 'date'){
      if($module_details['name'] != 'date_entered' && $module_details['name'] != 'date_modified' && $module_details['name'] != 'last_email_verification_c' && $module_details['name'] != 'birthdate'){
          $final_lat_enabled_module[$key] = $modulename; 
        }
      }
    }
  }

  $lat_enabled_modules = $final_lat_enabled_module;
  // print_r($lat_enabled_modules);
  $query = "select users_cstm.web_dashboard_c,users_cstm.user_wise_enable_c from users_cstm INNER JOIN  users ON users.id = users_cstm.id_c where users_cstm.id_c ='".$current_user->id."' and users.deleted=0 ";
  
  $result = $GLOBALS['db']->query($query);
  $data = array();
  $arr_web_dashboard_c = array();

  
  if (!$result) { echo "Required source or fields are missing. Please check enjaycrm log for more details.";
   die; } else { while ( $results = $GLOBALS["db"]->fetchByAssoc($result)) { $data = $results; }
  
  $_SESSION['user_wise_enable_c'] = $data['user_wise_enable_c'];

  if(!empty($data['web_dashboard_c'])){ $web_dashboard_c= base64_decode($data['web_dashboard_c']); }else{ $web_dashboard_c = '[{"date_label_field":"Start Date","module_name":"Calls","dropdown_label_field":"","dropdown_field":"","dom_field":"","date_field":"date_start"},{"date_label_field":"Followup Date","module_name":"Opportunities","dropdown_label_field":"Sales Stage","dropdown_field":"sales_stage","dom_field":"sales_stage_dom","date_field":"next_followup_date_c"},{"date_label_field":"Followup Date","module_name":"Leads","dropdown_label_field":"Status","dropdown_field":"status","dom_field":"lead_status_dom","date_field":"next_followup_date_c"},{"date_label_field":"","module_name":"Tasks","dropdown_label_field":"Status","dropdown_field":"status","dom_field":"task_status_dom","date_field":""},{"date_label_field":"","module_name":"Cases","dropdown_label_field":"Status","dropdown_field":"status","dom_field":"case_status_dom","date_field":""}]'; }
  
  $arr_web_dashboard_c = json_decode($web_dashboard_c);

  foreach ($arr_web_dashboard_c as $arr_web_dashboard_c_value) { $dropdown_field = $arr_web_dashboard_c_value->dropdown_field; $arr_web_dashboard_c_value->dropdown_field = explode(",",$dropdown_field); }}
  
// if($current_user->id == "c1546326-6b16-7dc5-1314-5c2af7e4787b"){

  ?>
  
  <?php include('custom/customization/modules/Home/Dashboard/php/dashboard_header.php');?>

    <img id="loading-image" src="custom/customization/modules/Home/Dashboard/image/ajax-loader.gif" style="display:none;position: absolute;z-index: 9;left: 45%;top: 45%;width: 100px;"/>
    <div id="wrapper">
      <div class="col-sm-12">
        <div class="p-20">
          <div class="" align="left">
            <div class="form-group menubar">
              <div class="dropdown checkboxes-and-radios companies-pri-grouping-part">
                <div align="left" style="float: left;width: 4%;">
                  <i class="fa fa-gear" style="font-size: 15px;cursor: pointer;color: #777777;margin-top: 3px;" id="setting_open" title="Click here for setting the modules!"></i>
                </div>
                <div align="left" style="float: left;width: 19%;margin-left: 7px;">
                  <b style="font-size: 14px;color: #777777;">My Records</b>  
                  <b style="float: right;">
                    <?php
                    if($getall == 0){?>
                     <input type="checkbox" id="my_record" name="my_record" checked value="1" data-plugin="switchery" data-color="#7266ba" data-size="small">
                    <?php }else{?>
                      <input type="checkbox" id="my_record" name="my_record" value="1" data-plugin="switchery" data-color="#7266ba" data-size="small">
                    <?php } ?>
                      <label for="my_record"></label>
                  </b>
                </div>
                <div align="left" style="float: left;width: 25%;margin-left: 16px;">
                  <b style="font-size: 14px;color: #777777;">All Users Records</b>  
                  <b style="float: right;">
                    <?php
                    if($getall == 1){?>
                     <input type="checkbox" id="all_records" name="all_records" checked value="1" data-plugin="switchery" data-color="#7266ba" data-size="small">
                    <?php }else{?>
                      <input type="checkbox" id="all_records" name="all_records" value="1" data-plugin="switchery" data-color="#7266ba" data-size="small">
                    <?php } ?>
                      <label for="all_records"></label>
                  </b>
                </div>

                <!-- <div class="dropdown-content">
                  <a class="dropdown-item" href="index.php?module=Home&action=dashboard&getall=0">My Records</a>
                  <a class="dropdown-item" href="index.php?module=Home&action=dashboard&getall=1">All Users Records</a>
                </div> -->
              </div>

              <!-- modal -->
              <div id="settings_modal" style="display: none;">
                <form method="post" name="settings_form" id="settings_form">
                      <input type="hidden" name="current_user_id" value="<?=$current_user->id?>">
                      <input type="hidden" name="getall" value="<?=$getall;?>">
                      <div class="modal-body edit view">
                        <div class="row">
                          <div class="col-md-12">
                            <div class="checkboxes-and-radios companies-pri-grouping-part">
                                <p align="left" style="width: 19%;float: left;margin-bottom: 0;">
                                  <b style="font-size: 14px;color: #777777;">User wise dashboard</b>  
                                  <b style="float: right;">
                                    <?php
                                    if($data['user_wise_enable_c'] == 1){?>
                                     <input type="checkbox" id="user_wise_enable_c" name="user_wise_enable_c" checked value="1" data-plugin="switchery" data-color="#7266ba" data-size="small">
                                    <?php }else{?>
                                      <input type="checkbox" id="user_wise_enable_c" name="user_wise_enable_c" value="1" data-plugin="switchery" data-color="#7266ba" data-size="small">
                                    <?php } ?>
                                      <label for="user_wise_enable_c"></label>
                                  </b>
                                </p>
                                <button type="button" class="button primary" id="cancel_settings_form" style="float: right;">Cancel</button>
                                <button type="submit" class="button primary" id="save_settings_form" style="float: right;">Save</button>
                            </div>
                            <hr>
                            <div class="form-group" id="module_selection_div">
                              <h6><b>Select Module</b></h6>
                              <?php
                                if(isset($lat_enabled_modules) && !empty($lat_enabled_modules))
                                {
                                  $i = 0;
                                  $sorted_arr = array();
                                  asort($app_list_strings['moduleList']);
                                  foreach ($app_list_strings['moduleList'] as $key => $value) {
                                    if(in_array($key, $lat_enabled_modules)){
                                        $sorted_arr[$value] = $key;
                                    }
                                  }
                                  foreach($sorted_arr as $enabled_modules)
                                  {
                                    if(isset($moduleList) && !empty($moduleList))
                                    {
                                      
                                      foreach($moduleList as $key_module_list=>$value_module_list)
                                      {
                                        if(ACLController::checkAccess($value_module_list, 'list', true))
                                        {
                                          $key_module_list = $value_module_list;
                                          if($enabled_modules == $key_module_list)
                                          { 
                                          ?>
                                            <div class="main-class-new-design-<?php echo $key_module_list; ?>">
                                              <div class="module-section" id="part1">
                                                <p align="left" class="font-size-20 module-section-p"><?php echo $app_list_strings['moduleList'][$value_module_list];?>
                                                  <b class="checkbox checkbox-custom checkboxes-and-radios" style="float: right;">
                                                  <input
                                                    id="<?php echo $key_module_list; ?>" name="module[<?php echo $key_module_list; ?>][module_name]"
                                                    type="checkbox" value="<?php echo $key_module_list; ?>" 
                                                    <?php
                                                      if(isset($arr_web_dashboard_c) && !empty($arr_web_dashboard_c))
                                                      {
                                                        foreach($arr_web_dashboard_c as $value_web_dashboard_c)
                                                        {
                                                          if($value_web_dashboard_c->module_name == $key_module_list)
                                                            {
                                                            ?>
                                                    checked="checked"
                                                    onclick="field_btn_display('module_settings_sec_<?php echo $key_module_list; ?>','<?php echo $key_module_list; ?>')"
                                                    <?php
                                                            }else{ if($key_module_list != "Calls"){?>
                                                    onclick="module_set('module_settings_sec_<?php echo $key_module_list; ?>','<?php echo $key_module_list; ?>','<?php echo $app_list_strings['moduleList'][$value_module_list];?>');"
                                                            <?php } }
                                                        }
                                                      }
                                                      ?>
                                                   ><!-- end input tag -->
                                                  <label for="<?php echo $key_module_list; ?>"></label>
                                                  </b>
                                                </p>
                                                <p id="down_click_<?php echo $key_module_list; ?>" style="display: none;">click</p>
                                                <span class="grp-setting">&#124;</span>
                                                <?php if($key_module_list != "Calls"){ ?>
                                                    <i title="Click here for more setting.&#10;This will open fields box and user must be select one field for displaying that module on dashboard counting." class="fa fa-gear grp-setting" aria-hidden="true" onclick="module_set('module_settings_sec_<?php echo $key_module_list; ?>','<?php echo $key_module_list; ?>','<?php echo $app_list_strings['moduleList'][$value_module_list];?>');" id="fa_<?php echo $key_module_list; ?>"></i>
                                                  <?php } ?>

                                                  
                                              </div>
                                              <div id="module_settings_sec_<?php echo $key_module_list; ?>" class="module-sub module_settings_sec_hide_<?php echo $key_module_list; ?> module-setting-div">
                                                <div id="pri_sec_group_settings_sec_<?php echo $key_module_list; ?>" >
                                                  <div class="companies-pri-grouping-sec">
                                                    <?php $settings_grouping_options =  get_settings_grouping_options($key_module_list);
                                                       if(!empty($settings_grouping_options['primarygroup'])){ ?>
                                                    <p align="left" style="font-size: 15px;color: #666;font-weight: 700;">Primary Grouping</p>
                                                    <hr>
                                                    <div>
                                                      <?php foreach ($settings_grouping_options['primarygroup'] as  $value_settings_grouping_options)
                                                        {
                                                          if($value_settings_grouping_options['type'] == 'datetime' || $value_settings_grouping_options['type'] == 'date' || $value_settings_grouping_options['type'] == 'datetimecombo')
                                                          { ?>
                                                            <div class="checkboxes-and-radios companies-pri-grouping-part">
                                                              <p align="left" > <?php echo  $value_settings_grouping_options['label']; ?>
                                                                <b style="float: right;">
                                                                <input type="radio" name="module[<?php echo $key_module_list;?>][date_field]" id="<?php echo $key_module_list.'_'.$value_settings_grouping_options['field_name'];
                                                                ?>" value="<?php echo $value_settings_grouping_options['field_name']; ?>"
                                                                <?php
                                                                if(isset($arr_web_dashboard_c) && !empty($arr_web_dashboard_c))
                                                                {
                                                                  foreach($arr_web_dashboard_c as $value_web_dashboard_c)
                                                                    {
                                                                      if($value_web_dashboard_c->date_field == $value_settings_grouping_options['field_name']  &&
                                                                      $value_web_dashboard_c->module_name == $key_module_list){ 
                                                                        $is_check = '1';
                                                                      ?> checked="checked" <?php }
                                                                    }
                                                                } ?> onclick="checkUncheckPrimaryGrouping(this);">

                                                            <label for="<?php echo $key_module_list.'_'.$value_settings_grouping_options['field_name'];
                                                            ?>"></label>
                                                            <p style="display: none;" id="is_primary_check_<?php echo $key_module_list.'_'.$value_settings_grouping_options['field_name'];
                                                                ?>"><?php echo $is_check;?></p>
                                                          </b>
                                                        </p>
                                                      </div>
                                                      <?php } } ?>
                                                    </div>
                                                    <hr>
                                                    <?php
                                                      }
                                                      if(!empty($settings_grouping_options['secondarygroup'])){ ?>
                                                    <p align="left" style="font-size: 15px;color: #666;font-weight: 700;">Secondary Grouping</p>
                                                    <div>
                                                      <?php
                                                        foreach ($settings_grouping_options['secondarygroup'] as  $value_settings_grouping_options)
                                                          {
                                                           if($value_settings_grouping_options['type'] != 'datetime' && $value_settings_grouping_options['type'] != 'date' && $value_settings_grouping_options['type'] != 'datetimecombo')
                                                           {
                                                           ?>
                                                      <div class="checkboxes-and-radios companies-pri-grouping-part">
                                                          <p align="left" ><?php echo  $value_settings_grouping_options['label'];
                                                          ?>
                                                          <b style="float: right;">
                                                          <input type="checkbox" id="<?php echo $key_module_list.'_'.$value_settings_grouping_options['field_name'];
                                                            ?>" name="module[<?php echo $key_module_list;?>][dropdown_field][]"  value="<?php echo $value_settings_grouping_options['field_name']; ?>"
                                                            data-plugin="switchery" data-color="#7266ba" data-size="small"
                                                            <?php
                                                              if(isset($arr_web_dashboard_c) && !empty($arr_web_dashboard_c))
                                                              {
                                                                  foreach($arr_web_dashboard_c as $value_web_dashboard_c)
                                                                        {
                                                              
                                                                         foreach ($value_web_dashboard_c->dropdown_field as  $dropdown_field)
                                                                         {
                                                                       if($dropdown_field == $value_settings_grouping_options['field_name']  &&
                                                                       $value_web_dashboard_c->module_name == $key_module_list)
                                                                         {
                                                                         ?>
                                                            checked="checked"
                                                            <?php
                                                              }
                                                                }
                                                               }
                                                              }
                                                              ?>
                                                            onclick="checkUncheckSecondaryGrouping(this);" />
                                                            <label for="<?php echo $key_module_list.'_'.$value_settings_grouping_options['field_name'];
                                                            ?>"></label>
                                                          </b>
                                                        </p>
                                                      </div>
                                                      <?php
                                                        }
                                                        }?>
                                                    </div>
                                                    <?php
                                                      } // End secodary grp condition
                                                    ?>
                                                  </div>
                                                </div>
                                              </div>
                                            </div>
                                            <?php  
                                            $i++;
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              ?>
                            </div>
                          </div>
                        </div>
                      </div>
                </form>
                  
                
              </div>
              <!-- /.modal -->
              
            </div>
          </div>
          <div class="form-group" id="dashboard_layout">
            
            
            <div class="tab-content">
              <div class="row">
                
                  <div class="col-sm">
                    
                    <?php include('custom/customization/modules/Home/Dashboard/php/dashboard_open.php');?>
                  </div>
                  <div class="col-sm">
                    
                    <?php include('custom/customization/modules/Home/Dashboard/php/dashboard_periodic.php');?>
                  </div>
                  <div class="col-sm">
                    
                    <?php include('custom/customization/modules/Home/Dashboard/php/dashboard_result.php');?>
                  </div>
                
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    
    <?php include('custom/customization/modules/Home/Dashboard/php/dashboard_footer.php');?>

<?php //}else{?>
  <!-- <h1>Dashboard is under development now!</h1> -->
  <?php //die; 
//} ?>