Using BeforeInsert in DHTMLX and passing variable to a form - php

I have a following problem: I have a scheduler that is used by different users. When a user adds an event to scheduler, a session variable containing his id must be passed to a processor where it is inserted into a database.
My first question is how do I bind a session variable to a form that is created with a scheduler.config.lightbox.sections:
scheduler.config.lightbox.sections=[
{name:"Customer Code", height:21, map_to:"text", type:"textarea" , focus:false},
{name:"Photographer", height:21, map_to:"pid", type:"select",
options:scheduler.serverList("type")},
{name:"time", height:72, type:"time", map_to:"auto"}
]
Is it possible to bind a session variable to it?
My second question is how do I get session variable in processor.php?
Please, correct me if I'm wrong, but according to the documentation it's going to be something like this:
//... connect here
function myInsert($action){
$new_value = rand(0,100);
$action->set_value("name",$new_value);
}
$conn->event->attach("beforeInsert","myInsert");
// ...some code here
$conn->render_table("photographers_at_work", "id", "time, end_time, customer_code, pid");

You can use onEventCreated to assign default values (such as user id) to a newly created event:
scheduler.attachEvent("onEventCreated", function(id,e){
var event = scheduler.getEvent(id);
event.pid = userId;
});
https://docs.dhtmlx.com/scheduler/api__scheduler_oneventcreated_event.html
This API event fires before lightbox is opened, so the lightbox will receive assigned values.
As for backend - yes, something like this should work. Couple of notes
$action->set_value - the first parameter is a column name
this column must be listed in properties columns you provide to connector (i.e. if you set value of column you don't have in your render_table parameters - connector will ignore it)
So something following should do:
//... connect here
function myInsert($action){
global $userId;
$action->set_value("pid",$userId);// ! set value of "pid" column
}
$conn->event->attach("beforeInsert","myInsert");
// ...some code here
$conn->render_table("photographers_at_work", "id", "time, end_time, customer_code, pid");
you can enable connector logging to see actual sql requests connector generates: https://docs.dhtmlx.com/connector__php__errors.html#serversidelogging

Related

Adding a new field to data extracted from a database call

As part of a search routine for a specific crop, I'm making two calls to the same database table, merging the results and sending them down the line. The first call looks for data relating to the searched-for crop (e.g. "beans"). The second call looks for data relating to the crop group of that crop (e.g. legumes).
Data returned from the first call will be more relevant/focused than that from the second call. I want to add an identifier to the respective data sets that reflects this so that I can subsequently sort/present the data on the basis of relevance in my Vue component.
The following code extracts the crop-specific information from the database; how can I add/insert/append a new variable (e.g. "relevance" = 1) to each row in $factsheets before I "array_merge" it with the data returned from the crop-group sql call?
(For sake of simplicitly, I've not included the code that determines the crops.id value from the name of the crop entered by the user.)
public function getFactsheets($cropId){
$factsheets = Factsheet::whereIn('crop_id',$cropId)
->join("crop_factsheet as cf","factsheets.id","=","cf.factsheet_id")
->join("crops as crops","crops.id","=","cf.crop_id")
->select('crops.name','title', 'factsheets.id', 'shortdesc', 'shortimg', 'factsheets.slug')
->orderBy('crops.name')
->get()->toArray();
return $factsheets;
}
Thanks, Tom.
If you give & to value so whatever changes will happen to value will directly saved on its address.
foreach($factsheets as $key => &$factsheet){
$factsheet['relevance'] = 1;
}
Working demo.
Here is concise explanation of references in official doc.
you can simply do a foreach
foreach($factsheets as $key => $factsheet){
$factsheet['relevance'] = 1;
}

Dragula - passing class on dragula drop event

So i have an object thats draggable to multiple columns and I need to make it so that when it is dropped a variable in that object gets updated depending on what column that is, as another object manipulates that variable for display. i have been unable to get the (ondrop) event to work from the tag, and the dragula event listener's value doesnt pass me any information that would allow me to get the object. Is there anyway to force the event listener to pass the object instead of the html tags? or is there some method im missing?
I think you can update data on drop. I've done it like this:
var drake = dragula({...});
function updateMyObject(elementId, listId) {
// update the object here, for example:
if (listId === 'firstList') {
// use the element id to find the item in your object and update it
myDataObject.filter(function(x) {
return x.id === elementId;
})[0].propertyToUpdate = listId;
}
}
drake.on('drop', function(el, target, source, sibling) {
var elementId = el.id;
updateMyObject(el.id, target.id);
});
This pen may help. I'm mixing Dragula with Angular.js for the data modelling. The event needs to update the data model on drop.
http://codepen.io/chris22smith/pen/37459a002cbe6b6cd37aa5e927698fba
The only solution I have found (short from using a different drag'n'drop module) is to save the order when the user closes the page or goes to something else. Or since the drop event is catch-able, but does not seem to be able to give a class object, you can still detect when there was a change and have it update everything, however that is not the best work around as it is far more taxing on system resources than updating one variable.
Pass the order # as an attribute in your element so it's accessible in your drake.on function. With the oder# and the ID you should be able to figure out what to do with it and make the right updates on the backend.

Sql conditional query parameters (include a parameter only when the corresponding form field is filled)

I'm writing a php program using mssql where I have a long query with many parameters and a quite big database. How could I solve that a parameter in the query only gets included when the corresponding form field is filled?
example:
SELECT * FROM Users WHERE UserdID='' AND Status='' AND ...
Lets say that this is an admin tool for seraching users but I only want to include those AND parameter='' sections where the corresponding form field has been filled.
I could check each form field and stich together the query but I feel like there is an easier and more elegant way.
Brother I hope you go with bellow algorithm
Create a Globel Variable for condition.
Check first input that value is not blank, if not blank then put into globel variable .
So on with other fileds.
After All input check the globel varriable content only those condtion which user submited.
Sample Example.
var a="";
if(txtUser!="")
{
a = a==""?"username= ".txtUser:"username= ".txtUser;
}
if(txtCountry!="")
{
a = a==""?"country= ".txtCountry:a." and country= ".txtCountry;
}
a variable for where condition

Setting cookie in codeigniter with one name and several values

I want to set a cookie in Codeigniter whenever the user clicks on "add to my favorites". But I'm confused. Because I have to add several items with one name at the same time. You know this is not possible and the CI overrides the previous values. Look at this:
$this->input->set_cookie(array("name"=>'fav', 'value'=>2500, 'expire'=>100000));
$this->input->set_cookie(array("name"=>'fav', 'value'=>3500, 'expire'=>100000));
$this->input->set_cookie(array("name"=>'fav', 'value'=>4500, 'expire'=>100000));
And when I try to get fav value using this function:
printer($this->input->cookie("fav"));
I get this result:
4500
How should I set a cookie for user when they ad an item to their favorite list so that in the moment of retrieving them I know what to retrieve. I cannot use database because this implementation is for the users who are not registered members.
You can use $this->session->set_userdata for storing values.
$fav = $this->session->userdata("my_favs");// get existing list
$fav[] = $new_fav; // append new items to list
$this->session->set_userdata(array("my_favs"=>$fav)); // update session with existing one.
To print all items
$fav = $this->session->userdata("my_favs")
foreach($fav as $fitems)
echo $fitems."<br/>";
I think you should use print_r() instead of printer().
more than that, you should use:
$this->input->get_cookie("fav");
Have a look here for more information: Cookie helper

Symfony forms question (restoring selected value of a dynamically populated sfWidgetFormSelect widget)

I am using Symfony 1.3.2 with Propel ORM on Ubuntu 9.10.
I have developed a form that dynamically populates a select widget with cities in a selected country, using AJAX.
Before the data entered on the form is saved, I validate the form. If validation fails, the form is presented back to the user for correction. However, because the country list is dynamically generated, the form that is presented for correction does not have a valid city selected (it is empty, since the country widget has not changed yet).
This is inconvenient for the user, because it means they have to select ANOTHER country (so the change event is fired), and then change back to the original country they selected, then FINALLY select the city which they had last selected.
All of this is forced on the user because another (possibly unrelated) field did not vaildate.
I tried $form->getValue('widget_name'), called immediately after $form->bind(), but it seems (infact, IIRC, if form fails to validate, all the values are reset to null) - so that does not work.
I am currently trying a nasty hack which involves the use of directly accesing the input (i.e. tainted) data via $_POST, and setting them into a flash variable - but I feel its a very nasty hack)
What I'm trying to do is a common use case scenario - is there a better way to do this, than hacking around with $_POST etc?
What I do for this exact issue is that I post the form to the same action that generated it, and in that action, I grab any selected countries/regions/cities as POST variables and pass them back to the template (regardless of validation). In the template, I then use JQuery to set the select values to what they were. When validation passes, they get used. When not, they get passed back to template.
If you can tolerate a little PHP in your JQuery, you could do this in the template:
$(document).ready(function()
{
$("#country-select").val('<?php echo $posted_country; ?>');
});
If you use this approach, don't forget to initialise $this->posted_country in your template the first time around or Jquery will get confused.
I guess you could also use $this->form->setWidget(...)->setDefault(...) or something similar, but I havent found a way around using $_POST as accessing the elements seems to need binding the form otherwise.
UPDATED CODE IN RESPONSE TO COMMENTS BELOW:
if($_POST['profile']['country_id'] != '')
{
$this->posted_country = $_POST['profile']['country_id'];
$q = Doctrine_Query::create()
->select('c.city_id, c.city_name')
->from('City c')
->where('c.country_id = ?', $this->posted_country);
$cities = $q->execute(array(), Doctrine_Core::HYDRATE_NONE);
foreach($cities as $city) $list[$city[0]] = $city[1];
$this->form->setWidget('city_id', new sfWidgetFormChoice(array('choices' => array('' => 'Please select') + $list)));
}
So... I get the country from the post, I query db with that, get cities, and craft cities back into a dropdown. Then in the template, you can set a default selected city with something like $this->posted_city (which would be a POST variable too, if exists).

Categories