Form and availability of "formers" - laravel

I am stuck on a problem that I did not think before.
I have a form formers with 3 fields (name, firstname, status).
Then, my second form is the trainings with 2 fields (date_sitting, fk_former).
Here, I have 1 sitting date -> 16/07/2019 and the former is Dujardin Luc.
Now my problem is that if I want to create a new sitting for on 18/07/2019.
How to do for that the former Dujardin Luc to be available in my dropdownlist for on 18/07/2019 or another date??
Is it, in my case, I must to create 2 files create.blade ?
The first will just have a field -> date_sitting
And the second form will have the field -> fk_former ?
Here is my TrainingController for information
public function index()
{
$trainings = Training::oldest()->paginate(5);
$formersNoTrainingToday = Training::whereDate('date_sitting', "!=", Carbon::today())
->orWhere('date_sitting', null)->get();
$formers = Former::doesntHave('trainings')->get();
return view('admin.trainings.index', compact('trainings'))
->with('i', (request()->input('page',1) -1)*5);
}

I can't give you the whole answer, as there are too many things to update and a few things to decide.
First, you'll need to change your index() method to both send more and different data. If a former can train on multiple days, the $formers = Former::doesntHave('trainings')->get(); is no longer valid -- you want all formers now so that you can add a new date. So just $formers = Former::get(); now.
Next, your $formersNoTrainingToday is now obsolete, as you don't want to identify those who are unavailable just today, you want a list of all training dates so compare against whatever date the user is looking for in the form. Example: If Dujardin Luc wants training on 18/07/20XX, we don't want him in a list of NoTraining because we want him available, even if he is unavailable today.
Last on the controller side, depending on how you want to do the blade side, you might want to include a variable for an AJAX date to use to send through those formers who are available on the specific date requested by the user through AJAX. For example, if you go through AJAX, you might have a method to just send those available on the date requested like this in your controller (Not exact code):
public function getFormersWithoutTrainingOnDateRequested($dateRequested){
$formersAvailableToday = Training::whereDate('date_sitting', "!=", Carbon::createFromFormat('Y-m-d', $dateRequested))
->orWhere('date_sitting', null)->get();
return json_encode($formersAvailableToday);
}
The next part is on the blade side. You have quite a few choices here, but both of the ones I recommend involve some type of JS or Jquery. In order to populate the list of formers for the day that the user requests, for best user experience, you should get them the list quickly.
Fastest would be to provide a list of trainings (date_sitting, fk_former) to the page from the index. This would be stored as a data-object on some element of the blade page. Then, when the user clicks to change the date, a JS or Jquery function would pull the list into a var, compare it against another list of former's ids stored the same way, and re-write the select list immediately, using only those formers whose ids were not matched for the date.
Second way to do this would be a little slower, but a little easier. It would involve going back to the server to get the list of formers available. So you would have an AJAX function on the same select that goes back to the server to pull those formers available on the date chosen. The success() method of the AJAX pull would provide the info to re-write the select box, or perhaps even re-make the whole form.
Sorry I can't write all the code, you'll have to fill in the blanks where you can, but this is hopefully enough to help you figure out where to go.

Related

JavaScript: how to trigger update form after setting input

I am writing a simple App Inventor program to populate a Covid-19 school health check page. I am stuck on selecting a school. Normally you type in a school name, the page finds string matches and you select from the list of string matched school. I am able to simply set the field manually but the rest of the form doesn't update. I know I must trigger an update but I cannot make head or tails of how this page works.
image of school selection
typing in part of school name
From the Chrome console I can do the following:
x = document.getElementsByClassName("k-textbox")
x[1].value = "Horace"
From the picture you can see the text was updated to "Horace" but the results have not updated. What should I do to force an update so the results list shows proper matches? Also, how do I query the matching results so I can confirm that my input was explicit enough to return a single match? FYI, this form entry page will be hidden to the user.
You can get the kendoDropDownList, call the read method from it's dataSource, and pass the locationName value of what you want. This won't set the text in the textbox, but it will filter your list down like you want.
To test, click on the input, open your console, and run the following:
$('#Location').data('kendoDropDownList').dataSource.read({ locationName: 'horace' })

Allow One setAttribute() to Prevent Another One

I have an entity which I've created two setAttribute functions for:
public function setStartAttribute($value) { }
and
public function setEndAttribute($value) { }
These attributes, start and end, are both datetimes which I check against some criteria in each of my setter function before allowing. Under certain conditions, I prevent or allow the start or end attributes to be updated.
I've hit a wall, however, in that if I prevent one of these from being updated, I need to prevent both. In other words, if the user tries to update the entity with a start date which is out of bounds, I need to prevent the start date from being updated, but I also need to prevent the end date from being update.
As these are two separate functions, I'm not sure how to use one to prevent the other in a case like this.
EDIT:
Since the answer is extremely obvious (just do it both in one function) without adding this extra info, I'll add that the part that makes this less straightforward is that I'm using Backpack for Laravel. Within the Backpack admin panel is the CRUD that lets me create or update my entity. I'm using the date_range field type to allow setting the start and end time/dates on my entity. It's upon saving this that I need to be able to pass both the start and end values to a function and validate them, prior to setting them on my entity. I found that creating the two separate functions above setStartAttribute() and setEndAttribute() allowed me to validate those values and choose whether to assign them to the entity, however I need to be able to use one unified function rather than two separate ones. It is this integration with Backpack which makes this problem less straightforward for me.
If those start and end attributes are connected somehow (one can't be set if another is invalid), you better make one method to set both of them. Something like this:
public function setStartAndEnd($start, $end)
{
if ($start is valid && $end is valid)
{
$this->start = $start;
$this->end = $end;
}
}
Which you can use as follows:
$entity->setStartAndEnd($date, $another_date);

Creating a unique ID in a form

I have a form that I have users fill out and then it gets e-mailed to me.
I am trying to get an example of how I would create an ID (based on my own conventions) that I can use to keep track of responses (and send back to the user so they can reference it later).
This is the convention I am striving for:
[YEAR]-[SERVICE CODE]-[DATE(MMDD)]-[TIME]
For example: "2012-ABC-0204-1344". I figured to add the TIME convention in the instance that two different users pick the same service on the same date rather than try to figure out how to only apply it IF two users picked the same service on the same date.
So, the scenario is that after the user goes through my wizards inputting their information and then click "Submit" that this unique ID would be created and attached to the model. Maybe something like #Model.UniqueID so that in an e-mail response I send to the user it shows up and says "Reference this ID for any future communication".
Thanks for any advice/help/examples.
In your post action
[HttpPost]
public ActionResult Create(YourModel model)
{
model.UniqueId = GenerateUniqueId(serviceCode);
}
public string GenerateUniqueId(string serviceCode)
{
return string.Format("{0}-{1}-{2}", DateTime.Now.Year, serviceCode, Guid.NewGuid().ToString().Replace("-",""); //remove dashes so its fits into your convention
}
but this seems as I'm missing part of your question. If you really want unique, use a Guid. This is what we've used in the past to give to customers - a guid or a portion of one. IF you use a portion of one ensure you have logic to handle a duplicate key. You don't need to worry about this though if using a full guid. If the idea is just to give to a customer then ignore the rest of the data and just use a guid, since it can easily be looked up in the database.

Forcing JQuery Datatable to load data via AJAX upon selection of a date range

I have the wonderful jquery data tables currently loading data using ajax.
The code is pretty much the same as the example I took it from which can be found at: http://www.datatables.net/release-datatables/examples/server_side/pipeline.html
I found this the best example because it incoporates pagination, sorting and also the search box into the ajax requests. The search box allows you to type in key words and this triggers the ajax function which includes the search value as a $_GET var to the server script.
This is a small preview of my table with table tools etc. loaded.
http://img828.imageshack.us/img828/9778/previewxjh.png
As you can see the main focus here is the filament groups date range plugin which I have added. I have finished with this now, and have a fail safe for the duplicate event firing problem etc. its ready to go and just needs including in the ajax pipeline - which is where I have been stuck for the last day or so.
fnDataTablesPipeline appears to be just an interim and doesnt reference the search box at all, so I can't figure out how the search box is working, and I am unsure if this is the right place to go including my date range value (everything I have tried just leads me to a dead end)
I want to use my onChange event for the date range filter, and apply it exactly the same way that the search box works. When the value is changed, simply pass it as a get variable so that my php script can deal with it there. I have concerns regarding the paging, and I will probably need to just reset back to page one after the date is changed (not sure how I am going to deal with this just yet, but thats the next step)
I need help telling datatables to refresh from the ajax source, and include the date range as a get parameter to the server side script (like when the search box value changes)
- sounds straight forward, but this is where I am breaking down and not making good use of my time due to the lack of understanding.
Is there anyone that has implemented similar that can help me?
The biggest problem right now is how to force the refresh from my date range onChange event, and of course include the single string value which contains my dates (which I know how to cover server side)
Many Thanks,
Chris
EDIT: I actually managed to get this working before I finished for the day. I'll post my code when I get back in the office tomorrow, it was actually surprisingly easy - I was tackling it completely wrong.
After changing date..
oTable.fnClearTable(0);
oTable.fnDraw();
Include var in pipeline
function fnDataTablesPipeline ( sSource, aoData, fnCallback, dateRange ) {
aoData.push( { "name": "dateRange", "value": $('#dateRangePicker').val(), } );
...
Obtain var in php script
$_GET['dateRange']

CakePHP, organize site structure around groups

So, I'm not quite sure how I should structure this in CakePHP to work correctly in the proper MVC form.
Let's, for argument sake, say I have the following data structure which are related in various ways:
Team
Task
Equipment
This is generally how sites are and is quite easy to structure and make in Cake. For example, I would have the a model, controller and view for each item set.
My problem (and I'm sure countless others have had it and already solved it) is that I have a level above the item sets. So, for example:
Department
Team
Task
Equipment
Department
Team
Task
Equipment
Department
Team
Task
Equipment
In my site, I need the ability for someone to view the site at an individual group level as well as move to view it all together (ie, ignore the groups).
So, I have models, views and controls for Depart, Team, Task and Equipment.
How do I structure my site so that from the Department view, someone can select a Department then move around the site to the different views for Team/Task/Equipment showing only those that belong to that particular Department.
In this same format, is there a way to also move around ignoring the department associations?
Hopefully the following example URLs clarifies anything that was unclear:
// View items while disregarding which group-set record they belong to
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id
http://www.example.com/Departments
// View items as if only those associated with the selected group-set record exist
http://www.example.com/Department/HR/Team/action/id
http://www.example.com/Department/HR/Task/action/id
http://www.example.com/Department/HR/Equipment/action/id
Can I get the controllers to function in this manner? Is there someone to read so I can figure this out?
Thanks to those that read all this :)
I think I know what you're trying to do. Correct me if I'm wrong:
I built a project manager for myself in which I wanted the URLs to be more logical, so instead of using something like
http://domain.com/project/milestones/add/MyProjectName I could use
http://domain.com/project/MyProjectName/milestones/add
I added a custom route to the end (!important) of my routes so that it catches anything that's not already a route and treats it as a "variable route".
Router::connect('/project/:project/:controller/:action/*', array(), array('project' => '[a-zA-Z0-9\-]+'));
Whatever route you put means that you can't already (or ever) have a controller by that name, for that reason I consider it a good practice to use a singular word instead of a plural. (I have a Projects Controller, so I use "project" to avoid conflicting with it.)
Now, to access the :project parameter anywhere in my app, I use this function in my AppController:
function __currentProject(){
// Finding the current Project's Info
if(isset($this->params['project'])){
App::import('Model', 'Project');
$projectNames = new Project;
$projectNames->contain();
$projectInfo = $projectNames->find('first', array('conditions' => array('Project.slug' => $this->params['project'])));
$project_id = $projectInfo['Project']['id'];
$this->set('project_name_for_layout', $projectInfo['Project']['name']);
return $project_id;
}
}
And I utilize it in my other controllers:
function overview(){
$this->layout = 'project';
// Getting currentProject id from App Controller
$project_id = parent::__currentProject();
// Finding out what time it is and performing queries based on time.
$nowStamp = time();
$nowDate = date('Y-m-d H:i:s' , $nowStamp);
$twoWeeksFromNow = $nowDate + 1209600;
$lateMilestones = $this->Project->Milestone->find('all', array('conditions'=>array('Milestone.project_id' => $project_id, 'Milestone.complete'=> 0, 'Milestone.duedate <'=> $nowDate)));
$this->set(compact('lateMilestones'));
$currentProject = $this->Project->find('all', array('conditions'=>array('Project.slug' => $this->params['project'])));
$this->set(compact('currentProject'));
}
For your project you can try using a route like this at the end of your routes.php file:
Router::connect('/:groupname/:controller/:action/*', array(), array('groupname' => '[a-zA-Z0-9\-]+'));
// Notice I removed "/project" from the beginning. If you put the :groupname first, as I've done in the last example, then you only have one option for these custom url routes.
Then modify the other code to your needs.
If this is a public site, you may want to consider using named variables. This will allow you to define the group on the URL still, but without additional functionality requirements.
http://example.com/team/group:hr
http://example.com/team/action/group:hr/other:var
It may require custom routes too... but it should do the job.
http://book.cakephp.org/view/541/Named-parameters
http://book.cakephp.org/view/542/Defining-Routes
SESSIONS
Since web is stateless, you will need to use sessions (or cookies). The question you will need to ask yourself is how to reflect the selection (or not) of a specific department. It could be as simple as putting a drop down selection in the upper right that reflects ALL, HR, Sales, etc. When the drop down changes, it will set (or clear) the Group session variable.
As for the functionality in the controllers, you just check for the Session. If it is there, you limit the data by the select group. So you would use the same URLs, but the controller or model would manage how the data gets displayed.
// for all functionality use:
http://www.example.com/Team/action/id
http://www.example.com/Task/action/id
http://www.example.com/Equipment/action/id
You don't change the URL to accommodate for the functionality. That would be like using a different URL for every USER wanting to see their ADDRESS, PHONE NUMBER, or BILLING INFO. Where USER would be the group and ADDRESS, PHONE NUMBER< and BILLING INFO would be the item sets.
WITHOUT SESSIONS
The other option would be to put the Group filter on each page. So for example on Team/index view you would have a group drop down to filter the data. It would accomplish the same thing without having to set and clear session variables.
The conclusion is and the key thing to remember is that the functionality does not change nor does the URLs. The only thing that changes is that you will be working with filtered data sets.
Does that make sense?

Resources