DatePicker inside of PHP block

Permalink
Hello all - Been staring this one down for hours now and have reached the end of google searches.

I am able to instantiate datepicker in my php block with the following
$dttEnd = Loader::helper('form/date_time');
print $dttEnd->date('check-out', "10/23/2011", true);

However, when I log in as a non-admin user, the same php block does not evoke the jquery datepicker and it looks like plain ol textbox with a default value of 10/23/2011.

Any help? I am 5.4.1.1. Thanks

 
jbx replied on at Permalink Reply
jbx
Sounds like it's dependent on one of the Concrete5 JS and/or CSS libraries. Try including ccm.ui.js and ccm.ui.css in your theme. If you do it properly with addHeaderItem() then I believe it will check and only include it if it isn't already included (avoiding conflicts when logged in...)

Hope that helps,

Jon
mkly replied on at Permalink Reply
mkly
in you block controller view function add
$html = Loader::helper('html');
$this->addHeaderItem($html->javascript('jquery.ui.js'));


jQueryUI is only automatically loaded when you are logged in as a user with edit privileges.
harpboy929 replied on at Permalink Reply
pardon :)
add -
$html = Loader::helper('html');
$this->addHeaderItem($html->javascript('jquery.ui.js'));

to view.php, not the actual block itself, correct?
mkly replied on at Permalink Reply
mkly
Sorry, I was a little short with that. I am referring to the controller.php that is your block's directory. You have likely created a function named
public function view() {


It would go in this if that makes sense.

It's part of how the MVC(model view controller) structure works. controller.php(the block's controller) is run before the page begins rendering so you still have time to add something to the documents "<head>". Once you get to view.php(the block's view) rendering of the page has already began and it would be too late to add something to the document head.

In concrete5's MVC implementation, you can think of the controller as a "staging area" for what you want to display to the user in your view.
harpboy929 replied on at Permalink Reply 2 Attachments
Thanks for these reply - when I made the adjustments to controller.php, nothing happened - when I hit view.php - see attached :) We're getting somewhere!
mkly replied on at Permalink Reply
mkly
Ugh. You saw my comment before I edited it probably.

You need place the
public function view() {
  $html = Loader::helper('html');
  $this->addHeaderItem($html->javascript('jquery.ui.js'));
}

in controller.php, not view.php. You may already have some things in the view function of controller.php. Just add the code part. Do not add another public function view()
jordanlev replied on at Permalink Reply
jordanlev
As others have mentioned, the jquery.ui library needs to be loaded in order for the datepicker to work. However, if you're using the PHP block I don't think it really gives you the opportunity to do something like this. The other responses here are all assuming that you're building your own block, but I don't think that's the case -- I think you're just using the "PHP Block" from the marketplace and pasting your code into that, correct?

What is it you want this block to actually do? There's probably a better way to achieve it than with the PHP block.
harpboy929 replied on at Permalink Reply
Jordan you are correct - I'm using the PHP Block from the Market place and pasting code in. All I'm trying to do is have a user-accessible date picker that returns a php variable that I can reference in different blocks. I need that date variable through the entire user session as I use it as a start/end point to run MYSQL queries against.

Does this make sense?
jordanlev replied on at Permalink Reply
jordanlev
It kind of makes sense, but I really don't think using the PHP block is ideal in this situation -- you should probably build a custom block for this. Running everything through an eval() statement (like the php block does) is generally considered a bad thing for performance and security and architectural reasons.
JohntheFish replied on at Permalink Reply
JohntheFish
I would be tempted to try adding the addheaderitem for jqueryui into the header part of the theme.
harpboy929 replied on at Permalink Reply 1 Attachment
John are you taking about adding addheaderitem to default.php as pictured in the attached?

I spent some time using single_pages (seemed kind of counter cultural) at first. I'm using C5 mainly as a user authencation hub which will present fetched data from MYSQL based of off parameters set by the autenticated users.

I'll probably retool and head here, but any additional tips you guys can throw my way would be greatly appreciated, but I would like to see if I could get date picker working as I'm attempting (now, out of curiosities sake).

http://www.concrete5.org/documentation/developers/blocks/understand...

Thanks so much for all the help on this guys
JohntheFish replied on at Permalink Reply
JohntheFish
My thought process was that you seemed to be getting on OK with the date picker via the php block when in edit mode, but that you needed to force the inclusion of jQuery ui when not in edit mode. (Or am I wrong on that?)

So the hack idea was just to make sure jquery ui was included all the time and putting it in the head part of header.php (referenced from default.php).

I would still try and do it with addheaderitem, as that should prevent duplicate inclusion (which would just make things worse).
harpboy929 replied on at Permalink Reply
John you correct Datepicker only works when in edit mode. I've found header.php inside /default --> is this the header.php that I need to target? the folders for my *active theme* does not contain an element/header.php
JohntheFish replied on at Permalink Reply
JohntheFish
No, it has to be in your active theme. If the active theme has no header.php, then the file you want will either be default for your active theme or a file referenced from it.

Rather than change default.php, you could copy it to ui_default.php, thus creating a new theme page type, then use that page type for the page you are working on.

If it gets any more complex than that, I would look for another solution as this is supposed to be an easy hack. If it isn't easy, then scratch my idea and look for an easier one.
harpboy929 replied on at Permalink Reply
I created a test block from scratch (basic_test from the above link) I edited the view.php to include
<?
$textHelper = Loader::helper("text");
$html = Loader::helper("html");
$this->addHeaderItem($html->css("jquery.ui.css"));
$this->addHeaderItem($html->javascript("jquery.ui.js"));
$dttEnd = Loader::helper('form/date_time');
print $dttEnd->date('check-out', "10/23/2011", true);

?>

And I have the exact same behavior. Datepicker will load in edit mode only. My problem is finding the exact .php file to revise and add $this->addHeaderItem($html->javascript("jquery.ui.js"));, what is the elegant, or progmatically preferred way of evoking datepicker?
jordanlev replied on at Permalink Best Answer Reply
jordanlev
The "addHeaderItem" code won't work in the "view" (neither the view of a block like you just tried nor the view of a page template, which is effectively what was happening when you used the php block) -- because by the time the "view" is rendered, the head of the page has already been outputted by C5.

So... instead you need to put that code into the block's controller.php file, specifically in the "on_page_view" function. Add this to your block's controller.php file:
public function on_page_view() {
    $textHelper = Loader::helper("text"); 
    $html = Loader::helper("html");
    $this->addHeaderItem($html->css("jquery.ui.css"));
    $this->addHeaderItem($html->javascript("jquery.ui.js"));
}


...and leave this part in the view.php file:
<?php
$dttEnd = Loader::helper('form/date_time');
print $dttEnd->date('check-out', "10/23/2011", true);
?>
harpboy929 replied on at Permalink Reply
Thank you so much! I'm Allset
JohntheFish replied on at Permalink Reply 1 Attachment
JohntheFish
I can think of circumstances where I would want a quick way to make jQuery UI available in view - when I am hacking a quick bit of script in an html block or when I want to use some ui styles or icons.

So I hacked together a quick add-on block package that does nothing but load jQuery UI and have just submitted it.

It will take a while to get through the PRB, but if you want it I have attached it here. Add the block to a page and jQuery UI will be available to that page.