Save new event to MySQL

Hello there,

I’m new to dhtmlx and after spending a good ammount of time reading the docs I am still having trouble saving a new event from the calendar into the database. More specifically it seems no save event is being passed to the db connector. This is my PHP file that handles the databaseconnector

[code]<?

include ('library/javascript/calendar/codebase/connector/scheduler_connector.php');

$e3 = new Employees;

$sql = readdbconn();
$res = mysql_connect($sql[0],$sql[1],$sql[2]);// db connection
mysql_select_db($sql[3]); // db connection
$calendar = new SchedulerConnector($res, "MySQL");
//$calendar->enable_log("log.txt",true);
	
	if ($calendar->is_select_mode()) {
	
		//This works and reads the database
		
		$query = ...Big ol' MySQL query...
			
		$calendar->render_sql($query, "id", "event_start,event_end,text,staffcount,roleid");
	
	}
	else {
		//This part echos when "saving"
		echo "Attempting to Save";

		$calendar->event->attach("beforeInsert","CreateShift"); //This event doesn't fire?!
	}

	function CreateShift($action) {
		//This event NEVER gets fired?!?!
		$query = "INSERT INTO `schedule_planned` (`date`) VALUES(NOW())";
		//mysql_query($query);
		echo "Database Query";
	
	}

?>[/code]

When saving the new calendar event my custom function CreateShift is not getting triggered. Why am I missing in my Javascript for the Calendar?

[code]$(document).ready(function() {

if ($("#scheduler_here").length) {

	RunWeatherApp();


	if ($("#scheduler_here").hasClass("create")) {
	
		var createMode = true;
		//Creating new schedules
		scheduler.attachEvent("onTemplatesReady", function(){
			scheduler.templates.event_text=function(start,end,event){
				return "<b>" + event.text + " (" + event.staffcount + ")</b><br>";
			}
		});
		
		scheduler.form_blocks["my_editor"] = {
				render:function(sns) {
					
					if ($(roles).length > 0) {
						var option = '';
						for (i = 0; i < $(roles).length; i++){
						   option += '<option value="'+ roles[i]['value'] + '">' + roles[i]['text'] + '</option>';
						}
					
						return "<div class='dhx_cal_ltext' style='height:60px;'><label>Position</label><select name='role'>" + option + "</select><br/><label># of Staff</label><input name='count' type='number'></div>";
					}
					else {
						return "<div class='dhx_cal_ltext'><h2 class='bubble'>You don't have any active staff members to schedule</h2></div>";
					}
				},
				set_value:function(node, value, ev) {
					$(node).find("select").val(ev.roleid);
					$(node).find("input").val(ev.staffcount);
				},
				get_value:function(node, ev) {
					ev.staffcount = $(node).find("input").val();
					return $(node).find("select option:selected").text();
				},
				focus:function(node) {
					var a = node.childNodes[1];
					//a.select();
					a.focus();
				}
			};
		
		scheduler.config.lightbox.sections=[    
			{ name:"Shift", height:200, map_to:"text", type:"my_editor" , focus:true}
		]
		
	}
	else {
		var createMode = false;
		//Assigning shifts
		
		scheduler.attachEvent("onTemplatesReady", function(){
			scheduler.templates.event_text=function(start,end,event){
				return "<b>" + event.text + " (" + event.assigned + "/" + event.staffcount + " Assigned)</b><br>";
			}
		});
		
		scheduler.attachEvent("onBeforeDrag", function(){
			return false;
		})
	}
	
	//Standard init
	scheduler.config.details_on_dblclick = true; //Create details view on double click
	scheduler.config.details_on_create = true; //Create details view on create
	scheduler.config.first_hour = 6;
	scheduler.config.last_hour = 24;
	
	//Weather Setup
	scheduler.attachEvent("onViewChange", function (new_mode , new_date) { RunWeatherApp();	});
		var DisplayFormat = scheduler.date.date_to_str(scheduler.config.day_date);
		var ComputeFormat = scheduler.date.date_to_str("%d-%m-%Y");
		scheduler.templates.week_scale_date = function(date){
			return DisplayFormat(date) + "<aside class='weather' data-date='" + ComputeFormat(date) + "'></aside>";
		};
				
	scheduler.init('scheduler_here', new Date(),"week");
	
	
	if (createMode) {
		scheduler.load("/ui/schedule/");
		var dp = new dataProcessor("/?view=ui&var1=schedule");
		
		scheduler.EnableDataprocessor = true;
		
	}
	else {
		scheduler.load("/ui/assign/");
		var dp = new dataProcessor("/?view=ui&var1=assign");
	}
	
	//Disable Saving Attempt
	dp.init(scheduler);

}

});[/code]

Please help, thanks!
Brian

Hi,
how do you process the update requests?
From the code you’ve pasted, [code]if ($calendar->is_select_mode()) {

$calendar->render_sql($query, “id”, “event_start,event_end,text,staffcount,roleid”);
}else {

//This part echos when “saving”
echo “Attempting to Save”;

$calendar->event->attach("beforeInsert","CreateShift"); //This event doesn't fire?!

}[/code]it seems like connector handles the request only in case ‘select’ requests (branch where ‘render_sql’ is called). In the alternate code branch you seems to only add a listener to the event, but not trigger the processing of the request. Connector handles the request when you call ‘render_sql’, ‘render_table’ or ‘render_complex_sql’

Aha, I did not put

$calendar->render();

in my else{} statement. Maybe in the guides it could be a bit more clear on that subject? Ihave now got the custom MySQL statement to insert. While I search in the guides for the answer, I’ll ask it here as well:

What variable is the inserted (and updated when editing) date and time saved to in the dataprocessor? I tried using

$start = $action->get_value("start_date");

but got no value.

try the same name that is specified in connector constructor:$calendar->render_sql($query, "id", "event_start,event_end,text,staffcount,roleid"); ... $action->get_value("event_start")

Tried that one, didnt work. Maybe I dont have it being passed correctly by the javascript? This is from the logs

[code]DataProcessor object initialized
1412698447483_start_date => 10/07/2014 07:45
1412698447483_end_date => 10/07/2014 09:50
1412698447483_text => Human Resources
1412698447483_id => 1412698447483
1412698447483_staffcount => 1
1412698447483_role => 1
1412698447483_!nativeeditor_status => inserted
ids => 1412698447483

Undefined offset: 0 at /home/myfr6168/public_html/fusion.badiee.com/library/javascript/calendar/codebase/connector/scheduler_connector.php line 98

Undefined offset: 1 at /home/myfr6168/public_html/fusion.badiee.com/library/javascript/calendar/codebase/connector/scheduler_connector.php line 102

Undefined offset: 2 at /home/myfr6168/public_html/fusion.badiee.com/library/javascript/calendar/codebase/connector/scheduler_connector.php line 104

Row data [1412698447483]
=> Human Resources
dhx_auto_id => 1412698447483
staffcount => 1
role => 1
!nativeeditor_status => inserted

Incorrect field name used: event_start

data
=> Human Resources
dhx_auto_id => 1412698447483
staffcount => 1
role => 1
!nativeeditor_status => inserted

Incorrect field name used: event_end

data
=> Human Resources
dhx_auto_id => 1412698447483
staffcount => 1
role => 1
!nativeeditor_status => inserted

Event code for insert processed

Edit operation finished
0 => action:inserted; sid:1412698447483; tid:1412698447483;

Done in 0.0219609737396s[/code]

Seemed to get it working by including the function render_array

[code]if ($calendar->is_select_mode()) {

		//This works and reads the database
		
		$query = ...Big MySQL Query
			
		$calendar->render_sql($query, "id", "event_start,event_end,text,staffcount,roleid");
	
	}
	else {
		//This part works 
		echo "Attempting to Save";
		
		$calendar->event->attach("beforeInsert","CreateShift");
		$calendar->event->attach("beforeUpdate","EditShift");
		$calendar->event->attach("beforeDelete","DeleteShift");
		
		$calendar->render_array($action, "id", "event_start,event_end,title,staffcount,roleid");
	}[/code]