Creating pages programmatically - cDatePublic not saved properly

Permalink
Hi all

I've built a form to create pages programmatically (based on Andrew's HOWTO) and it works, except it's not saving the cDatePublic var properly (when I look at the created pages' properties in the Sitemap I see 1/1/1970).

If I edit the page (or view its properties in the Sitemap) then save it, without having changed anything, everything then works as it should.

The problem is obviously something to do with how the page is being created initially.

Here's the code that's creating the page:

view.php:
$df = Loader::helper('form/date_time');
<strong><?=$form->label('pageDatePublic', t('Date/Time'))?></strong>
<div><?=$df->datetime('pageDatePublic', $pageDatePublic)?></div>
<div class="form-spacer"></div>


controller.php:
public function add() {
      if($this->isPost()){
         Loader::model('collection_types');
         $path = "/created-pages";
         $newPage = Page::getByPath($path);
         $newPageData = array('cName' => $this->post('pageName'), 'cDescription' => $this->post('pageDesc'), 'cDatePublic' => Loader::helper('form/date_time')->translate('pageDatePublic'));
         $ct = CollectionType::getByHandle('dynamic');
         $p = $newPage->add($ct, $newPageData);
         $this->saveData($p);
         $this->redirect('/dashboard/page_creator/', 'page_added');
      }
   }


When I edit the page, using this code:

view.php
$df = Loader::helper('form/date_time');
<strong><?=$form->label('pageDatePublic', t('Date/Time'))?></strong>
<div><?=$df->datetime('pageDatePublic', $pageDatePublic)?></div>
<div class="form-spacer"></div>


controller.php:
public function update() {
      $this->edit($this->post('pageID'));
      if($this->isPost()) {
         $p = Page::getByID($this->post('pageID'));
         $updatedPageData = array('cName' => $this->post('pageName'), 'cDescription' => $this->post('pageDesc'), 'cDatePublic' => Loader::helper('form/date_time')->translate('pageDatePublic'));
         $p->update($updatedPageData);
         $this->saveData($p);
         $this->redirect('/dashboard/page_creator/', 'page_updated');
      }
   }


..the page is then saved 'properly', and the page appears as it should.

The problem is preventing my pages from appearing in Matogertel's Advanced Page List block, which is a critical problem - but I can see that rather than it being a bug with that block the problem is with the way I'm saving the pages.

Any help would be much appreciated, I'm on a tight deadline with this and would like to get the problem solved ASAP! :)

melat0nin
 
jordanlev replied on at Permalink Reply
jordanlev
I don't have an exact answer for you, but I know that editing a page shouldn't change its "date public", but rather you need to set the date on the CollectionVersion instead.
melat0nin replied on at Permalink Reply
melat0nin
Hi Jordan

Thanks for the reply. What I don't understand is why the update saves the page 'properly' but the initial creation doesn't, even though they're using exactly the same code.

Is it possible that the initial CollectionVersion isn't getting a date set, but when it's updated a new version is created and with it a date, therefore making the page 'complete'?
melat0nin replied on at Permalink Reply
melat0nin
Anyone got any thoughts? This is a serious bug in my web app and I'd love to have it fixed before the client finds it :)
jordanlev replied on at Permalink Reply
jordanlev
I wonder if it has to do with this thing:
'cDatePublic' => Loader::helper('form/date_time')->translate('pageDatePublic');


The fact that the date comes up as 1/1/1970 is indicative of the timestamp going in as 0 (that is, zero seconds after the UNIX epoch which is 1/1/1970). So maybe that helper function isn't actually returning a properly-formatted date? What happens if you just hard-code a date in there, like '2011-04-20' -- does it work then? If so, then the problem is with that helper thing. If not, then you're back to the problem of it going into the wrong field.
melat0nin replied on at Permalink Reply
melat0nin
Hi Jordan

Thanks for your reply. Alas it doesn't seem to work - I hard coded in the date like this:

public function add() {
      if($this->isPost()){
         Loader::model('collection_types');
         $path = "/candidate-services/vacancies";
         $newPage = Page::getByPath($path);
         $newPageData = array('cName' => $this->post('pageName'), 'cDescription' => $this->post('pageDesc'), 'cDatePublic' => '2011-05-01');
         $ct = CollectionType::getByHandle('vacancy');
         $p = $newPage->add($ct, $newPageData);
         $this->saveData($p);
         $this->redirect('/dashboard/vacancies/', 'job_added');
      }
   }


but the page still needs to be re-saved to show up. I think I might post a bug in the Search Tools addon because the problem is with that package - the standard page list works fine with no issues.
jordanlev replied on at Permalink Reply
jordanlev
Uhh... you probably should have mentioned that this was specific to the search tools package to begin with :)

That is a paid addon so you get support with it -- I would try asking the author of that package about this issue (go to its marketplace page and click the 'support' link in the sidebar).
melat0nin replied on at Permalink Reply
melat0nin
I did mention it :)

I figured since the problem appears to be with the way I was creating the pages, it probably meant something buggy there which I should fix to prevent other problems down the road.

But you're right, the addon's support area is probably the best place - have submitted a ticket and forum post :)
janwidmer replied on at Permalink Reply
janwidmer
Hey melat0nin,

In your original post you mentioned a tutorial from Andrew. Do you have the link to that tutorial?

I am trying to create a page from a block / form and I can't really find that much informations..

Thanks Jan
melat0nin replied on at Permalink Reply
melat0nin
It's this one here:
http://www.concrete5.org/documentation/how-tos/developers/build-a-s...

Note it's over 3 years old now though, so it might not be entirely accurate
any more.