/**
  * This function creates the initial `.xsl` template for the page, whether
  * that be from the `TEMPLATES/blueprints.page.xsl` file, or from an existing
  * template with the same name. This function will handle the renaming of a page
  * by creating the new files using the old files as the templates then removing
  * the old template. If a template already exists for a Page, it will not
  * be overridden and the function will return true.
  *
  * @see toolkit.PageManager#resolvePageFileLocation()
  * @see toolkit.PageManager#createHandle()
  * @param string $new_path
  *  The path of the Page, which is the handles of the Page parents. If the
  *  page has multiple parents, they will be separated by a forward slash.
  *  eg. article/read. If a page has no parents, this parameter should be null.
  * @param string $new_handle
  *  The new Page handle, generated using `PageManager::createHandle`.
  * @param string $old_path (optional)
  *  This parameter is only required when renaming a Page. It should be the 'old
  *  path' before the Page was renamed.
  * @param string $old_handle (optional)
  *  This parameter is only required when renaming a Page. It should be the 'old
  *  handle' before the Page was renamed.
  * @return boolean
  *  True when the page files have been created successfully, false otherwise.
  */
 public static function createPageFiles($new_path, $new_handle, $old_path = null, $old_handle = null)
 {
     $new = PageManager::resolvePageFileLocation($new_path, $new_handle);
     $old = PageManager::resolvePageFileLocation($old_path, $old_handle);
     $data = null;
     // Nothing to do:
     if (file_exists($new) && $new == $old) {
         return true;
     }
     // Old file doesn't exist, use template:
     if (!file_exists($old)) {
         $data = file_get_contents(self::getTemplate('blueprints.page'));
     } else {
         $data = file_get_contents($old);
     }
     /**
      * Just before a Page Template is about to be created & written to disk
      *
      * @delegate PageTemplatePreCreate
      * @since Symphony 2.2.2
      * @param string $context
      * '/blueprints/pages/'
      * @param string $file
      *  The path to the Page Template file
      * @param string $contents
      *  The contents of the `$data`, passed by reference
      */
     Symphony::ExtensionManager()->notifyMembers('PageTemplatePreCreate', '/blueprints/pages/', array('file' => $new, 'contents' => &$data));
     if (PageManager::writePageFiles($new, $data)) {
         // Remove the old file, in the case of a rename
         if (file_exists($old)) {
             General::deleteFile($old);
         }
         /**
          * Just after a Page Template is saved after been created.
          *
          * @delegate PageTemplatePostCreate
          * @since Symphony 2.2.2
          * @param string $context
          * '/blueprints/pages/'
          * @param string $file
          *  The path to the Page Template file
          */
         Symphony::ExtensionManager()->notifyMembers('PageTemplatePostCreate', '/blueprints/pages/', array('file' => $new));
         return true;
     }
     return false;
 }
 public function __actionTemplate()
 {
     $filename = $this->_context[1] . '.xsl';
     $file_abs = PAGES . '/' . $filename;
     $fields = $_POST['fields'];
     $this->_errors = array();
     if (!isset($fields['body']) || trim($fields['body']) == '') {
         $this->_errors['body'] = __('This is a required field.');
     } else {
         if (!General::validateXML($fields['body'], $errors, false, new XSLTProcess())) {
             $this->_errors['body'] = __('This document is not well formed.') . ' ' . __('The following error was returned:') . ' <code>' . $errors[0]['message'] . '</code>';
         }
     }
     if (empty($this->_errors)) {
         /**
          * Just before a Page Template is about to written to disk
          *
          * @delegate PageTemplatePreEdit
          * @since Symphony 2.2.2
          * @param string $context
          * '/blueprints/pages/template/'
          * @param string $file
          *  The path to the Page Template file
          * @param string $contents
          *  The contents of the `$fields['body']`, passed by reference
          */
         Symphony::ExtensionManager()->notifyMembers('PageTemplatePreEdit', '/blueprints/pages/template/', array('file' => $file_abs, 'contents' => &$fields['body']));
         if (!PageManager::writePageFiles($file_abs, $fields['body'])) {
             $this->pageAlert(__('Page Template could not be written to disk.') . ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')), Alert::ERROR);
         } else {
             /**
              * Just after a Page Template has been edited and written to disk
              *
              * @delegate PageTemplatePostEdit
              * @since Symphony 2.2.2
              * @param string $context
              * '/blueprints/pages/template/'
              * @param string $file
              *  The path to the Page Template file
              */
             Symphony::ExtensionManager()->notifyMembers('PageTemplatePostEdit', '/blueprints/pages/template/', array('file' => $file_abs));
             redirect(SYMPHONY_URL . '/blueprints/pages/template/' . $this->_context[1] . '/saved/');
         }
     }
 }