<?php session_start();

			if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes'){ 
            include ('scripts/classes.php');
			if ($_SESSION['lang']=="EN"){include ('lang/en.php');}elseif ($_SESSION['lang']=="GR"){include ('lang/gr.php');}else{include ('lang/en.php');}
			if (isset($_GET['pos'])=='toggle') {$go = 'POS';}
			
			$mode_sms = $lbl_sms_module;
			if ($go == 'POS'){$page_title =$lbl_pos_activation; $mode=$lbl_pos_activation;}else{$page_title =$lbl_home_page; $mode=$lbl_welcome;}
			//GET MODULES AND SETTINGS	
			$array = getGlobalSettings();foreach ($array as $settings){extract($settings);}
			$mods = getCLSModules('CELEBRATIONS'); foreach ($mods as $modules){extract($modules);} if ($mod_name == "CELEBRATIONS") $_CELEBRATIONS = 1; //echo $_CELEBRATIONS;
            $mods = getCLSModules('SMS'); foreach ($mods as $cls_modules){extract($cls_modules);} if ($mod_name == "SMS") $_SMS = 1; //echo $_SMS		
			$today = date("Y-m-d"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="img/favicon.png" />
<link href="cms1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script>top.document.title=self.document.title;</script>
		<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
		<br />
		<?php 
			if ($mode == $lbl_welcome){
		 		echo "<div  style='padding-left:10px;padding-right:10px'>".$lbl_welcome_text."</div>";
		 	}
		 
			if ($go=='POS'){
				if(isset($_SESSION['POS']) && $_SESSION['POS'] == 'ON') {
					$_SESSION['POS'] = 'OFF';
					echo "<div class='critical' id='errorloc' style='visibility:visible'>
							<ul>
								<li>".$lbl_pos_deactivated."</li>
							</ul>	
						  </div>";						
					}else{
					$_SESSION['POS'] = 'ON';
					echo "<div class='completed' id='errorloc' style='visibility:visible'>
							<ul>
								<li>".$lbl_pos_activated."</li>
							</ul>	
						  </div>";		
				}
			}
			
		?>
			<br />
		</fieldset>
			<br />
<?php
//CELEBRATIONS MODULE
if ($_CELEBRATIONS==1){
?>
		<fieldset>
		<legend><?php echo $lbl_celebrations ?></legend>
			<br />
		<?php
			$sql_names_today ="SELECT `name` AS celebs FROM namedays WHERE `day`=DAY('".$today."') AND `month`= MONTH('".$today."')";		
			$result_names_today = mysqli_query($mysqli, $sql_names_today) or die("Query failed : " . mysqli_error($mysqli));
				if (mysqli_num_rows($result_names_today) == 0){
				echo "<div style='padding-left:10px'> $lbl_no_celebrations_today</div>";
				}else{
					echo "<img src='cls/modules/celebrations/img/people.gif' alt='$lbl_celebrations' title='$lbl_celebrations' />
					$lbl_namedays_today
					<br />";
					$celebs = array();
						while ($line = mysqli_fetch_array($result_names_today)) {	
							$celebs[] = $line[0]; 
							//print_r($celebs);
						}	
						$trim_celeb = implode(" ", $celebs);
						//print_r(implode(",", $celebs));		 
						$words = explode(" ", $trim_celeb);
						echo "<div class='page_tips' style='padding-left:10px;padding-right:10px'>";
							print_r(implode(", ", $words));
						echo "</div>";
				 
						FOR($i=0;$i<count($words);$i++)
						{$query_string .= "LIKE '%".$words[$i]."%' OR first_name ";}			 
						$query_string = SUBSTR($query_string,0,STRLEN($query_string)-14);
						//echo $query_string;
						
						$sql_fest = "SELECT COUNT(id) AS names_today FROM cls_customers WHERE first_name ".$query_string."";			 
					
						$result_fest = mysqli_query($mysqli, $sql_fest) or die ("Query failed : " . mysqli_error($mysqli));
						$line = mysqli_fetch_assoc($result_fest); 
						$names_today = $line['names_today'];
						if ($names_today == 0){
						echo "<div style='padding-left:10px;padding-right:10px'>$lbl_no_customers_name<div>";
						}else{		
							echo "<div style='padding-left:10px;padding-right:10px'>";
								echo "$lbl_found_plural <strong>".$names_today."</strong> $lbl_celebrating_customers  
								$lbl_click <a href='cls/modules/celebrations/celebrations_list.php?action=namedays&selected_day=".@date("d")."&selected_month=".@date("m").";'>$lbl_here</a> $lbl_to_see_the_list";
							echo "</div><br />";
						}
				}	    
		?> 
		<?php
			$sql_birthday ="SELECT birth_date FROM cls_customers WHERE DAY(birth_date)=DAY('".$today."') AND MONTH(birth_date)= MONTH('".$today."')";		
			$result_birthday = mysqli_query($mysqli, $sql_birthday) or die("Query failed : " . mysqli_error($mysqli));
				if (mysqli_num_rows($result_birthday) == 0){
					//echo "<div style='padding-left:10px'> $lbl_no_birthdays_today</div>";
					}else{
					$rows = mysqli_num_rows($result_birthday);
					echo "<div style='padding-left:10px'>$lbl_found_plural <strong>$rows</strong> $lbl_that_have_birthday
					$lbl_click <a href='cls/modules/celebrations/birthdays_list.php?action=birthdays&selected_day=".@date("d")."&selected_month=".@date("m").";'>$lbl_here</a> $lbl_to_see_the_list</div>";	
					}
		?>
		</fieldset>  
		<?php
		}
//END CELEBRATIONS MODULE
		?>
		<br />
		<?php
				if ($sms_flag ==1 and $_SMS == 1){ //1
				    $get_sms_provider = Core::GetSmsProvider();
				    $credit_balance = getSMSBalance();
				    if ($credit_balance <= 50 and $number > 10){ $div_class = "warning"; } elseif ($credit_balance <= 10){$div_class = "critical";}else{$div_class = "success";}
				    ?>
                    <fieldset><legend><?php echo _SMS_SUBMODULE ?></legend>
					<?php
                    if ($get_sms_provider == 'Clickatell') { //2
                    ?>
                    <div class="<?php echo $div_class ?>">
                    <?php
						echo "<ul><li>$lbl_credit_balance: <strong>$credit_balance</strong>&nbsp;";
						echo "<a href=\"main.php\"><img src=\"img/db_reload.gif\" /> $lbl_refresh_credits</a></li></ul>";
					?>
					</div>
                    <div>
                    <?php 
                    } else if ($get_sms_provider == 'SMSAPI') { //else 2
                         $static_SmsApi = SmsApi::GetSmsApiBalance();
                         if ($static_SmsApi['connected'] == 'true'){
                            $formatted_balance = explode(",", $static_SmsApi['credit_balance'], 2);
                            $first_part = $formatted_balance[0];
                            $second_part = ($formatted_balance[1]) ? ','.$formatted_balance[1] : '';
                            echo '<div class="flat-circle green-color">'.$first_part.'<span class="circle-small-letters">'.$second_part.'</span><br /><span class="circle-title">'._SMS_CREDITS.'</span></div>';
                         } else {
                            echo $static_SmsApi['system_message'];  
                            //Draw red circle 
                            echo '<div class="flat-circle red-color">'.$static_SmsApi['credit_balance'].'<br /><span class="circle-title">'._SMS_CREDITS.'</span></div>';
                         }
                         
                         //Check for Auto SMS errors
                         $auto_sms_exists = Core::AutoSmsSetupExists();
                         if ($auto_sms_exists === true) { //3
                            $auto_sms_errors = Core::AutoSmsHasErrors();
                            if ($auto_sms_errors === true) {
                                echo '<div class="flat-circle red-color">!<br /><span class="circle-title">'._AUTO_SMS_ERROR.'</span></div>';
                            } //end 4
                         } //end 3
                         
                    } //end 2
                    ?></div>
                    </fieldset>
				<?php
				}//end 1
				?>	
						
		</div>
</body>
</html>
<?php   
    mysqli_close($mysqli); 
} else {
    header('Location: login.php');
}
?>

