Welcome, Guest
Username: Password: Remember me

TOPIC: Where to place custom functions

Where to place custom functions 23 Feb 2017 14:18 #15010

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
Hello all,
Where is now the best place to add custom functions to my cook project ?
I would like to implement some string or date manipulations and have them returned in the grid view.
Was able to do it in previous versions of cook, doesn't work anymore and gives an error of Call to undefined function blablabla

Thanks,
V.
The administrator has disabled public write access.

Where to place custom functions 26 Feb 2017 08:25 #15011

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
The fork system still works.
Where / how did you do it in previous versions?
The administrator has disabled public write access.

Where to place custom functions 03 Mar 2017 13:04 #15022

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
Hello,
Fork works indeed, was not the problem i was talking about.
I used to do much modifications to the model, and it doesn't seem to work like this anymore.
I've gone another way now, in the default.php that loads default_grid.php in a collection view.
Question was more of a theorical one, kind of "What is the best place to do such things"
Thanx for your answer anyway
The administrator has disabled public write access.

Where to place custom functions 03 Mar 2017 14:12 #15023

  • dphc
  • dphc's Avatar
  • Offline
  • New Member
  • Posts: 1
  • Karma: 0
Ah, well... the best place is to use the fork system ;)

You can fork the administrator and site code.
For the functions you name, I would probably fork the helper in the admin and then call it by xxxHelper::yourFunction()
The administrator has disabled public write access.

Where to place custom functions 03 Mar 2017 15:36 #15024

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Fork or not, that's not the question

It depends what your function is for.
Always keep in mind the MCV structure :
- Controller is for actions and treatment
- Model is for preparing datas
- View is to prepare the ingredients.
- Template is for composing the page
- Helper is a place for static calls and nothing else.

The question is generally double :
- Where I declare it ?
- Where I call it ?

To answer VeCrea :
Maybe the template file i not the best place. Put it in the View file it is much better.
The templates should not involve any calculation or process, it should have done it before.

The template can eventually work with the model directly, but only for READING data (such as ACL checks)
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: organicwebs

Where to place custom functions 04 Mar 2017 12:23 #15025

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
Let's have three practical examples >>> where do you place them, in your opinion (I don't need the code, i already have code that works, i just want to know where you think i should place them)
1) Function to get a time difference between two DateTime : should be a function called to calculate the difference between two dateTime. I have a start time, a end time, and want to create a function with those two arguments, that returns the time difference in minutes
2) Display data from another table / model in a view : i want to display a list of problems that have occured during a day at work : on the same page, i have informations about that day (model dailyReport) but i also need to display the informations of the "Incidents" table)
3) Add a button to send a mail containing the informations of an item page.

Again, i don't need the code, i need opinion on best practice, best place to place it.
The administrator has disabled public write access.
The following user(s) said Thank You: organicwebs

Where to place custom functions 04 Mar 2017 12:29 #15026

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
1) Helper (static call)
Helpers can also be sub divided by purpose : dates / html / files / ...
For date manipulation : XxxHelperDate::diff($start, $end)

2) Load the concerned model into the view, display data in template
3) Controller
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: VeCrea, organicwebs
Time to create page: 0.080 seconds

Get Started