<?php

require ("config.php");
//ini_set('display_errors', '1');
//error_reporting(E_ALL);
if (isset($_SESSION["id_users"]) ) {
  //Condition is used to check the admin is active or not
  if($_SESSION["id_role"] == 5 OR $_SESSION["id_role"] == 6 OR $_SESSION["id_role"] == 7 OR  $_SESSION["id_role"] == 9 OR  $_SESSION["id_role"] == 10){
    header("Location:".$hosturl.'newsletter');
  }
  ////Condition is used to check the admin is active or not is ends
  
  $smarty->assign('userstat', 'out');
  $smarty->assign('loggedin', 1);
  $myuser = new User($_SESSION["id_users"]);
  $smarty->assign('email', $myuser->get_email());
  $smarty->assign('fname', $myuser->get_fname());
  $smarty->assign('lname', $myuser->get_lname());
  $smarty->assign('mar_style', $myuser->get_mar_style());
  $smarty->assign('ar_type', $myuser->get_id_role());
  $smarty->assign('ar_frequency', $myuser->get_ar_frequency());
  
  $ar_frequency_time =explode(',',$myuser->get_ar_frequency_time());
  
  $smarty->assign('ar_frequency_time',$ar_frequency_time);
  
  $smarty->assign('fav_company', $myuser->get_fav_company());
  $smarty->assign('layout_tbl_bg_color', $myuser->get_layout_tbl_bg_color());
  $smarty->assign('layout_tbl_border_color', $myuser->get_layout_tbl_border_color());
  $smarty->assign('layout_tbl_font_color', $myuser->get_layout_tbl_font_color());
  $smarty->assign('layout_font_color', $myuser->get_layout_font_color());
  $smarty->assign('layout_logo_align', $myuser->get_layout_logo_align());
  $smarty->assign('layout_inc_title', $myuser->get_layout_inc_title());
  $smarty->assign('layout_inc_brief', $myuser->get_layout_inc_brief());
  $smarty->assign('layout_inc_ave', $myuser->get_layout_inc_ave());
  $smarty->assign('layout_inc_pr_value', $myuser->get_layout_inc_pr_value());
  $smarty->assign('layout_inc_fp_ave', $myuser->get_layout_inc_fp_ave());
  
  //assign newsletter subscription
  $smarty->assign('news_subscription', $myuser->get_news_subscription());  
  //assign notifyme,topics,groups,folders
  $smarty->assign('notifyme', $myuser->get_notifyme());
  $alltopics = $myuser->get_alltopics();
  $smarty->assign('alltopics',$alltopics);
  $allgroups = $myuser->get_allgroups();
  $smarty->assign('allgroups',$allgroups);
  $allfolders = $myuser->get_allfolders();
  $smarty->assign('allfolders',$allfolders);
  
  
  $array = $myuser->getTimingarray();      //get the timing calculation
  $smarty->assign('timingarr', $array);
  $value = $myuser->getUserInterestscompanies();//  print_r($value);die("test");//get user interest companies
  $smarty->assign('companyids', $value);
  
  
  if (isset($_SESSION["userCompanies"])) {
    foreach($_SESSION["userCompanies"] as $c) {
      $tmpC = new Company($c[0]);
      $companyList[] = Array($c[0], str_replace("&", "&amp;", $tmpC->get_name_company()));
    }
    $smarty->assign('companyList', $companyList);
  }
  
  $includeCssArr = array("popup","customscroll/customscroll");
  $smarty->assign('includeCssArr', $includeCssArr);
  
  $includeJsArr = array("preferenceselectable", "customscroll/customscroll","jscolor/jscolor");
  $smarty->assign('includeJsArr', $includeJsArr);
  
  /*get the user selectable groups,folders,topics*/
  
  if(!empty($alltopics))
  {
    $select_topic_arr = split(',',$alltopics);
    $i = 0;
    $topic_arr = array();
    $getValue1 = 'id_company';
    $getValue2 = 'name_company';
    $table = 'company';
    $cond ='id_company';    
    foreach($select_topic_arr as $val)
    {
     $topic_arr[$i]=$myuser->commonFunctionToSelectValue($getValue1,$getValue2,$table,$cond,$val);
     $i++;
    }
    if(!empty($topic_arr))
      $smarty->assign('topic_arr',$topic_arr);
    else
      $smarty->assign('topic_arr','');
  } 
  
  if(!empty($allgroups))
  {
    $select_groups_arr = split(',',$allgroups);
    $j = 0;
    $groups_arr = array();
    $getValue1 = 'group_id';
    $getValue2 = 'group_name';
    $table = 'grouping_concepts';
    $cond ='group_id';    
    foreach($select_groups_arr as $val)
    {
      $groups_arr[$j] = $myuser->commonFunctionToSelectValue($getValue1,$getValue2,$table,$cond,$val);
      $j++;
    }
    if(!empty($groups_arr))
      $smarty->assign('groups_arr',$groups_arr);
    else
      $smarty->assign('groups_arr','');
  }
  
  if(!empty($allfolders))
  {
    $select_folders_arr = split(',',$allfolders);
    $k=0;
    $folder_arr = array();
    $getValue1 = 'id';
    $getValue2 = 'name';
    $table = 'folders';
    $cond ='id';    
    foreach($select_folders_arr as $val)
    {
     $folder_arr[$k] = $myuser->commonFunctionToSelectValue($getValue1,$getValue2,$table,$cond,$val);
     $k++;
    }
    if(!empty($folder_arr))
      $smarty->assign('folder_arr',$folder_arr);
    else
      $smarty->assign('folder_arr','');
  }
  /*get the user selectable groups,folders,topics ends*/  
  //print_r($topic_arr);echo "<br>";print_r($groups_arr);echo "<br>";print_r($folder_arr);die("test");
  
  /*get topic,groups and the folder of the particular users*/
  $user_topic = $myuser->getUserInterestscompaniesIeTopic($alltopics);
  $smarty->assign('user_topic', $user_topic);
  $user_groups = $myuser->getUserGroups($allgroups);
  $smarty->assign('user_groups', $user_groups);
  $user_folders = $myuser->getUserFolders($allfolders);
  $smarty->assign('user_folders', $user_folders);
  /*get topic,groups and the folder of the particular users ends*/
  

}
else {
  header("Location:".$hosturl.'login');// redirect the user when session out or before click login
  $smarty->assign('userstat', 'in');
  $smarty->assign('loggedin', 0);
  $smarty->assign('msg_title', 'Error');
  $smarty->assign('msg_detail', 'You are unauthorized to view this page. Please login first. If you do not have an account, feel free to <a href="http://www.mediastow.com/contact">contact</a> us about our services.');
  $smarty->display('unauthorized.tpl');
  exit;
}

/*
global $mydb;
$mydb->connect();
*/
$scriptname = str_replace(".php", $_SERVER["SCRIPT_NAME"], "");

$browse_info = split("/", $_SERVER["PATH_INFO"]);

$smarty->assign('scriptname', $scriptname);

$smarty->assign('fullname', $_SESSION["fname"].' '.$_SESSION['lname']);


//
if($_POST['loadcontent'] == 1)
  {
    $array = $myuser->getTimingarray();      //get the timing calculation
    $frequency_time =explode(',',$myuser->get_ar_frequency_time());
    
   
    for($i=0;$i<$_POST['frequency_id'];$i++)
      {
        //$check_value = count($frequency_time)-$i;        
        $times .= '<div class="alerts"><p>Alert '.$i.'</p>
            <select name="timing[]">';
            foreach($array as $key)
              {//echo $frequency_time[$check_value]."<br>".$key;
                $selected = "";
                $selected = (($frequency_time[$i]) == $key ? "selected":"");
                $times .= '<option value="'.$key.'"'.$selected.' >'.$key.'</option>';
              }
            $times .='</select>
        </div>';
      }
      echo "time|@|".$times;
      exit();
  }
  
//
//Ajax replace the preference dragabble selection setting starts
if($_POST['replaceall'] == 1)
  {
    //when i click clear i empty the newsletter 
    $myuser->set_topics_for_newsletter('');
    $myuser->set_groups_for_newsletter('');
    $myuser->set_folders_for_newsletter('');
    //ends
    $alltopics = '';
    $allgroups = '';
    $allfolders = '';
    
    $alltopics = '';
    $smarty->assign('alltopics',$alltopics);
    $allgroups = '';
    $smarty->assign('allgroups',$allgroups);
    $allfolders = '';
    $smarty->assign('allfolders',$allfolders);
    
    $user_topic = $myuser->getUserInterestscompaniesIeTopic($alltopics);
    $smarty->assign('user_topic', $user_topic);
    $user_groups = $myuser->getUserGroups($allgroups);
    $smarty->assign('user_groups', $user_groups);
    $user_folders = $myuser->getUserFolders($allfolders);
    $smarty->assign('user_folders', $user_folders);
    $smarty->display('preferencereplace.tpl');
    exit();
  }
//Ajax replace the preference dragabble selection setting ends



// action comes here
if (isset($_GET["action"])) {
  if ($_GET["fname"]) {
    $myuser->set_fname($_GET["fname"]);
  }
  if ($_GET["lname"]) {
    $myuser->set_lname($_GET["lname"]);
  }
  if($_GET["email"]) {
    if(filter_var($_GET["email"], FILTER_VALIDATE_EMAIL)){
          $myuser->set_email($_GET["email"]);
    }
    else{
        $emailerror = 1;
    }
  }

  if ($_GET["fav_company"]) {
    $myuser->set_fav_company($_GET["fav_company"]);
  }
  if ($_GET["mar_style"]) {
    $myuser->set_mar_style($_GET["mar_style"]);
  }
  if ($_GET["ar_type"]) {
    // the role and the alert report type are the same for clients
    // perhaps this is a bit confusing, needs to be changed later
    $myuser->set_id_role($_GET["ar_type"]);
  }
  
  if ($_GET["ar_frequency"]) {
      if(!empty($_GET['timing']))
        {
          $countOfTiming = count($_GET['timing']);
          if($countOfTiming == $_GET["ar_frequency"])
            {
              $new_array = array_unique($_GET['timing']);
              if($countOfTiming == count(array_unique($_GET['timing'])))
              {
                  $newgen = $_GET['timing'];
                  sort($newgen,SORT_NUMERIC,ASC);                  
                  $myuser->set_news_timing($newgen);
                  $myuser->set_ar_frequency($_GET["ar_frequency"]);
              }
              else
              {
                $differenttime = 2;
              }
            }
          else{
              $counttime = 1;
            }  
        }
      else{
            $selection = 1;
        }  
  }
  
  
  if ($_GET["layout_tbl_bg_color"]) {
    $myuser->set_layout_tbl_bg_color($_GET["layout_tbl_bg_color"]);
  }
  if ($_GET["layout_tbl_border_color"]) {
    $myuser->set_layout_tbl_border_color($_GET["layout_tbl_border_color"]);
  }
  if ($_GET["layout_tbl_font_color"]) {
    $myuser->set_layout_tbl_font_color($_GET["layout_tbl_font_color"]);
  }
  if ($_GET["layout_font_color"]) {
    $myuser->set_layout_font_color($_GET["layout_font_color"]);
  }
  if ($_GET["layout_logo_align"]) {
    $myuser->set_layout_logo_align($_GET["layout_logo_align"]);
  }
  if ($_GET["layout_inc_title"]) {
    $myuser->set_layout_inc_title(1);
  }
  else {
    $myuser->set_layout_inc_title(0);
  }

  if ($_GET["layout_inc_brief"]) {
    $myuser->set_layout_inc_brief(1);
  }
  else {
    $myuser->set_layout_inc_brief(0);
  }

  if ($_GET["layout_inc_ave"]) {
    $myuser->set_layout_inc_ave(1);
  }
  else {
    $myuser->set_layout_inc_ave(0);
  }
 
  if ($_GET["layout_inc_pr_value"]) {
    $myuser->set_layout_inc_pr_value(1);
  }
  else {
    $myuser->set_layout_inc_pr_value(0);
  }

  if ($_GET["layout_inc_fp_ave"]) {
    $myuser->set_layout_inc_fp_ave(1);
  }
  else {
    $myuser->set_layout_inc_fp_ave(0);
  }
  
  //function for update the newsletter subscription
  if($_GET['news_subscription']){
    $myuser->set_news_subscription($_GET['news_subscription']);
  }
  
  //function for update the user interested company
  if($_GET['getallcompany_id'])
    {
        //echo "<pre>";print_r($value);echo "</pre>";echo "<pre>";print_r($_GET['getallcompany_id']);echo "</pre>";die("kathir");
        $arranged_id = split(',',$_GET['getallcompany_id']);
        $i=0;
        foreach($value as $primaryids)
            {
                $myuser->UpdateUserInterestBasedonSelection($arranged_id[$i],$primaryids['id_users_company']);
                $i++;
            }
    }
    
  //this below are function to store the values for newsletter 
  if($_GET['topicsval'])
  {
    $myuser->set_topics_for_newsletter(''); //here before update the value empty the record in datbase
    $alltopics = $_GET['topicsval'];
    $myuser->set_topics_for_newsletter($alltopics);
  }
  else
  {
    $myuser->set_topics_for_newsletter(''); //here before update the value empty the record in datbase
  }
  
  if($_GET['groupsval'])
  {
    $myuser->set_groups_for_newsletter(''); //here before update the value empty the record in datbase
    $allgroups = $_GET['groupsval'];
    $myuser->set_groups_for_newsletter($allgroups);
  }
  else
  {
    $myuser->set_groups_for_newsletter(''); //here before update the value empty the record in datbase
  }
  
  if($_GET['foldersval'])
  {
    $myuser->set_folders_for_newsletter(''); //here before update the value empty the record in datbase
    $allfolders = $_GET['foldersval'];
    $myuser->set_folders_for_newsletter($allfolders);
  }
  else
  {
    $myuser->set_folders_for_newsletter(''); //here before update the value empty the record in datbase
  }
  
  if($_GET['notifyme'])
  {
    if($_GET['notifyme']=='Yes')
      $notifyme = 1;
    else
      $notifyme = 0;
    $myuser->setNotifyMe($notifyme);
  }
  //ends 
  
  if ($_GET["password1"] AND $_GET["password2"]) {
    // if both passwords are the same
    if (!strcmp($_GET["password1"], $_GET["password2"])) {
      $myuser->set_password($_GET["password1"]);
    }
    else{
        $passwordmismatch =1;
    }
  }
  if($passwordmismatch)
    {
      header("Location: ".$hosturl.'preferences?msg=passworderror');
    }
  elseif($emailerror){
        header("Location: ".$hosturl.'preferences?msg=emailerror');
    }
  elseif($counttime){
       header("Location: ".$hosturl.'preferences?msg=time&timevalue='.$countOfTiming);  
    }
  elseif($differenttime==2){
      header("Location: ".$hosturl.'preferences?msg=samevalue');  
    }  
  elseif($selection){
      header("Location: ".$hosturl.'preferences?msg=selection');
    }    
  else{
    header("Location: ".$hosturl.'preferences?msg=updatesucess');
  }
  $smarty->assign('prefsupdate', 1);
  $smarty->display('preferences.tpl');
  
}
elseif($_GET['unsubscribe']=='news'){
    $smarty->assign('conformpopup', 1);
    $smarty->display('preferences.tpl');
}
elseif(isset($_POST['submitconform']))
  {
    //this function for update the status of the newsletter subscription
    $myuser->set_news_subscription($status ='inactive');
    header("Location: ".$hosturl.'preferences');
  }
elseif($_GET['msg']=='selection')
  {
    $smarty->assign('timeselect', 'Please select timing To receive a mail');
    $smarty->display('preferences.tpl');
  }
elseif($_GET['msg']=='time')
  {
    $smarty->assign('timeselect', 'Please select timing equal to frequency');
    $smarty->display('preferences.tpl');
  }
elseif($_GET['msg']=='samevalue')
  {
    $smarty->assign('timeselect', 'Please select different time for alert mail');
    $smarty->display('preferences.tpl');
  }
elseif($_GET['msg']=='emailerror')
  {
    $smarty->assign('emailerror', 'Please give a valid email id');
    $smarty->display('preferences.tpl');
  }
elseif($_GET['msg']=='passworderror')
  {
    $smarty->assign('passwordmismatch', 'password cannot match');
    $smarty->display('preferences.tpl');
  }
elseif($_GET['msg'] == 'updatesucess')
  {
      $smarty->assign('updatesucess', 'Preferences update successfully');
      $smarty->display('preferences.tpl');
  }
else {
  
      //$array = $myuser->getTimingarray();      
      //$smarty->assign('timingarr', $array);      
      $smarty->assign('prefsupdate', 0);
      $smarty->display('preferences.tpl');
}


?>
