<?php

require_once "modules/Schedulers/Scheduler.php";

function outright_create_simple_form( $form_array ){

	$form ='<table align="centre" width ="100%">';
	$form .='<form method="POST">';
	$form .='<input type="hidden" value="outr_outr_licence" name="module">';
	$form .='<input type="hidden" value="create_cronjob" name="action">';
	$open_tr ='<tr><td>';
	$mid_tr ='</td><td>';
	$close_tr='</td></tr>';
	foreach( $form_array as $form_field){
	$form .=$open_tr.$form_field['label'].$mid_tr;
			if( $form_field['type'] =='text' ){
			$form .='<input type="text" name ="'.$form_field['name'].'">';
			 }
			 else if( $form_field['type'] =='textarea' ){		
			$form .= '<textarea name ="'.$form_field['name'].'" rows="10" cols="100">';
			$form .='outright_retrieve("Users","1");
			$sql ="select * from users where deleted =0";
			$result =outright_run_sql($sql);
			echo "<pre>";
			print_r($result);
			echo "</pre><br/>";
			</textarea>';
		 }
		  else if( $form_field['type'] =='select' ){
			$form .='<select name ="'.$form_field['name'].'">';
			  foreach( $form_field['options'] as $form_key=>$form_value){
				$form .='<option value="'.$form_key.'" label="'.$form_value.'"></option>';
			  }

			  $form .='</select>';
			$form .=$close_tr;
		 }
	}
		 	$form .='<tr><td colspan="2"><input type="submit" value="Create cronjob" name="create_cronjob"></td></tr>';

	 $form .='</form>';

	$form .='</table>';
	
	return $form;
}
class outright_create_cronjob{

var $enp_file_name;
var $enp_dir;
var $enp_ext_dir;
var $default_code;
var $cronjob_location;


       public function __construct(){
		   	$this->enp_file_name =$_REQUEST['cronjob_name'].".php";
			$this->enp_dir ="custom/outright_products/cronJobs/";
			$this->enp_ext_dir ='custom/Extension/modules/Schedulers/Ext/ScheduledTasks/';
			$this->includer_cronjob ='custom/Extension/application/Ext/Utils/';
			$this->cronjob_lang ='custom/Extension/modules/Schedulers/Ext/Language/';
			$this->lang_text =strtoupper("lbl_".$_REQUEST['cronjob_name']);

			$this->enp_file =$this->enp_dir.$this->enp_file_name;
			$this->cronjob_location =$this->enp_ext_dir.$this->enp_file_name;
			$this->includer_cronjob_file =$this->includer_cronjob.$this->enp_file_name;
			$this->cronjob_lang_file =$this->cronjob_lang."en_us.".$this->enp_file_name;


			if(!is_dir( $this->enp_dir)){
					if (!mkdir($this->enp_dir, 0777, true)) {
					die('Failed to create folders...'.$this->enp_dir);
					}
			}

			if(!is_dir( $this->enp_ext_dir )){
					if (!mkdir($this->enp_ext_dir, 0777, true)) {
					die('Failed to create folders...'.$this->enp_ext_dir);
					}
			}

			if(!is_dir( $this->includer_cronjob)){
					if (!mkdir($this->includer_cronjob, 0777, true)) {
					die('Failed to create folders...'.$this->includer_cronjob);
					}
			}

			if(!is_dir( $this->cronjob_lang)){
				if (!mkdir($this->cronjob_lang, 0777, true)) {
					die('Failed to create folders...'.$this->cronjob_lang);
					}
			}
						
	   }

		function outright_create_cronjob_files(){
			$sch = new Scheduler();
		
			$enp_text ='<?php

			array_push($job_strings, "'.$_REQUEST['cronjob_name'].'");

			';
		
			file_put_contents( $this->cronjob_location ,$enp_text);
			
			$default_code ='<?php
function '.$_REQUEST['cronjob_name'].'(){
echo "CRON JOB '.$_REQUEST[cronjob_name].' is succesfully created";
}';

$lang_text ='<?php

$mod_strings["'.$this->lang_text.'"]= "'.$_REQUEST[cronjob_name].'";';

			file_put_contents( $this->enp_file ,$default_code);
			file_put_contents( $this->cronjob_lang_file ,$lang_text);

				$include_file_text ='<?php

require_once "'.$this->enp_file.'";';
			file_put_contents( $this->includer_cronjob_file ,$include_file_text);				
			outright_do_repair();

			$sch->name =$_REQUEST[cronjob_name]." By Outright Store";
			$sch->job_function ="function::".$_REQUEST[cronjob_name];
			$sch->job ="function::".$_REQUEST[cronjob_name];
			//$sch->date_time_start ='2015-01-01';
			$sch->job_interval ='1';
			$sch->status ='Active';
			$sch->save();
		}
}
if($_REQUEST['cronjob_name']){
	echo "cronjob Created for ".$_REQUEST['cronjob_name'];
	echo '<hr/>';

	echo '<b>Go To  <a target="_blank" href="index.php?entryPoint='.$_REQUEST['enp_name'].'">click here</a></b>';
$enp_create = new outright_create_cronjob();
$enp_create->outright_create_cronjob_files();
}
else{
	$form_array =array(

		'job_name'=>array(
			'name'=>'cronjob_name',
			'type'=>'text',
			'label'=>'Cron Job Name :',
		),

		
	);
echo outright_create_simple_form( $form_array );
}


