Page 1 of 1

Is there any event when user copy the page?

Posted: Wed Oct 03, 2012 5:39 pm
by cve
Hi, i'm working on a module, and i need run some kind of code when user copy the page in cms made simple... Is there any event when this action is runing?

Re: Is there any event when user copy the page?

Posted: Wed Oct 03, 2012 5:48 pm
by calguy1000
Nope. Sorry, there is not.

Re: Is there any event when user copy the page?

Posted: Fri Oct 05, 2012 12:38 pm
by psy
What Calguy says is true but there's more than one way to skin a cat.

There are events you can compare, ie ContentEditPre and ContentEditPost. If the page has a content id of -1, it's a new page. Doesn't matter how/where it was created, whether by manually adding a new page or copying an another one.

Firstly, create an event in ContentEditPre that stores the page in a session var if it's active, and test for the content id. If it's -1, it's a new page.

Code: Select all

$contentobj = $params['content'];
if ($contentobj->Id() == -1){ // It's a new page 
  // do somthing;
}

// make sure the new page is Active
if ($contentobj->Active() == 1) {
  $_SESSION['mynewpage_active'] = 1;
}
OK, so we've made a temporary session var letting us know we have a new, active page.

Next compare that with the event ContentEditPost:

Code: Select all

$content = $params['content'];
$contentops = ContentOperations::get_instance();
$content_id = $content->Id();
$contentobj = $contentops->LoadContentFromId($content_id);

if ($contentobj)
{
  $hid = $contentobj->Hierarchy();
  if (!$hid) {
    // New pages are only partially saved. We need to set/get the hierarchy stuff
    $contentops->SetHierarchyPosition($contentobj->Id());
    $hid = $contentobj->Hierarchy();
  }
  $contentops->CreateFriendlyHierarchyPosition($hid);

  // Ensure the newly created page is active
  if (isset($_SESSION['mynewpage_active'])  ) {
    unset ($_SESSION['mynewpage_active']);    
    // do what you want with the new active page that has now been fully written to the db....
}
hth
psy