public static function updatePagesOfPrototype($prototype_id, $types)
 {
     if (empty($prototype_id)) {
         return;
     }
     $prototype = PageManager::fetchPageByID($prototype_id);
     $pages = self::fetchPagesOfPrototype($prototype_id);
     $fields = array();
     if (is_array($pages) && !empty($pages)) {
         foreach ($pages as $page) {
             $fields['params'] = $prototype['params'];
             $fields['events'] = $prototype['events'];
             $fields['data_sources'] = $prototype['data_sources'];
             PageManager::edit($page['id'], $fields, true);
             PageManager::addPageTypesToPage($page['id'], array_values(array_diff($types, array('prototype'))));
         }
     }
 }
 public function __actionEdit()
 {
     if ($this->_context[0] != 'new' && !($page_id = (int) $this->_context[1])) {
         redirect(SYMPHONY_URL . '/blueprints/pages/');
     }
     $parent_link_suffix = NULL;
     if (isset($_REQUEST['parent']) && is_numeric($_REQUEST['parent'])) {
         $parent_link_suffix = '?parent=' . $_REQUEST['parent'];
     }
     if (@array_key_exists('delete', $_POST['action'])) {
         $this->__actionDelete($page_id, SYMPHONY_URL . '/blueprints/pages/' . $parent_link_suffix);
     }
     if (@array_key_exists('save', $_POST['action'])) {
         $fields = $_POST['fields'];
         $this->_errors = array();
         $autogenerated_handle = false;
         if (!isset($fields['title']) || trim($fields['title']) == '') {
             $this->_errors['title'] = __('This is a required field');
         }
         if (trim($fields['type']) != '' && preg_match('/(index|404|403)/i', $fields['type'])) {
             $types = preg_split('/\\s*,\\s*/', strtolower($fields['type']), -1, PREG_SPLIT_NO_EMPTY);
             if (in_array('index', $types) && PageManager::hasPageTypeBeenUsed($page_id, 'index')) {
                 $this->_errors['type'] = __('An index type page already exists.');
             } elseif (in_array('404', $types) && PageManager::hasPageTypeBeenUsed($page_id, '404')) {
                 $this->_errors['type'] = __('A 404 type page already exists.');
             } elseif (in_array('403', $types) && PageManager::hasPageTypeBeenUsed($page_id, '403')) {
                 $this->_errors['type'] = __('A 403 type page already exists.');
             }
         }
         if (trim($fields['handle']) == '') {
             $fields['handle'] = $fields['title'];
             $autogenerated_handle = true;
         }
         $fields['handle'] = PageManager::createHandle($fields['handle']);
         if (empty($fields['handle']) && !isset($this->_errors['title'])) {
             $this->_errors['handle'] = __('Please ensure handle contains at least one Latin-based character.');
         }
         /**
          * Just after the Symphony validation has run, allows Developers
          * to run custom validation logic on a Page
          *
          * @delegate PagePostValidate
          * @since Symphony 2.2
          * @param string $context
          * '/blueprints/pages/'
          * @param array $fields
          *  The `$_POST['fields']` array. This should be read-only and not changed
          *  through this delegate.
          * @param array $errors
          *  An associative array of errors, with the key matching a key in the
          *  `$fields` array, and the value being the string of the error. `$errors`
          *  is passed by reference.
          */
         Symphony::ExtensionManager()->notifyMembers('PagePostValidate', '/blueprints/pages/', array('fields' => $fields, 'errors' => &$errors));
         if (empty($this->_errors)) {
             $autogenerated_handle = false;
             if ($fields['params']) {
                 $fields['params'] = trim(preg_replace('@\\/{2,}@', '/', $fields['params']), '/');
             }
             // Clean up type list
             $types = preg_split('/\\s*,\\s*/', $fields['type'], -1, PREG_SPLIT_NO_EMPTY);
             $types = @array_map('trim', $types);
             unset($fields['type']);
             $fields['parent'] = $fields['parent'] != __('None') ? $fields['parent'] : null;
             $fields['data_sources'] = is_array($fields['data_sources']) ? implode(',', $fields['data_sources']) : NULL;
             $fields['events'] = is_array($fields['events']) ? implode(',', $fields['events']) : NULL;
             $fields['path'] = null;
             if ($fields['parent']) {
                 $fields['path'] = PageManager::resolvePagePath((int) $fields['parent']);
             }
             // Check for duplicates:
             $current = PageManager::fetchPageByID($page_id);
             if (empty($current)) {
                 $fields['sortorder'] = PageManager::fetchNextSortOrder();
             }
             $where = array();
             if (!empty($current)) {
                 $where[] = "p.id != {$page_id}";
             }
             $where[] = "p.handle = '" . $fields['handle'] . "'";
             $where[] = is_null($fields['path']) ? "p.path IS NULL" : "p.path = '" . $fields['path'] . "'";
             $duplicate = PageManager::fetch(false, array('*'), $where);
             // If duplicate
             if (!empty($duplicate)) {
                 if ($autogenerated_handle) {
                     $this->_errors['title'] = __('A page with that title already exists');
                 } else {
                     $this->_errors['handle'] = __('A page with that handle already exists');
                 }
             } else {
                 // New page?
                 if (empty($current)) {
                     $file_created = PageManager::createPageFiles($fields['path'], $fields['handle']);
                 } else {
                     $file_created = PageManager::createPageFiles($fields['path'], $fields['handle'], $current['path'], $current['handle']);
                 }
                 // If the file wasn't created, it's usually permissions related
                 if (!$file_created) {
                     $redirect = null;
                     return $this->pageAlert(__('Page Template could not be written to disk.') . ' ' . __('Please check permissions on %s.', array('<code>/workspace/pages</code>')), Alert::ERROR);
                 }
                 // Insert the new data:
                 if (empty($current)) {
                     /**
                      * Just prior to creating a new Page record in `tbl_pages`, provided
                      * with the `$fields` associative array. Use with caution, as no
                      * duplicate page checks are run after this delegate has fired
                      *
                      * @delegate PagePreCreate
                      * @since Symphony 2.2
                      * @param string $context
                      * '/blueprints/pages/'
                      * @param array $fields
                      *  The `$_POST['fields']` array passed by reference
                      */
                     Symphony::ExtensionManager()->notifyMembers('PagePreCreate', '/blueprints/pages/', array('fields' => &$fields));
                     if (!($page_id = PageManager::add($fields))) {
                         $this->pageAlert(__('Unknown errors occurred while attempting to save.') . '<a href="' . SYMPHONY_URL . '/system/log/">' . __('Check your activity log') . '</a>.', Alert::ERROR);
                     } else {
                         /**
                          * Just after the creation of a new page in `tbl_pages`
                          *
                          * @delegate PagePostCreate
                          * @since Symphony 2.2
                          * @param string $context
                          * '/blueprints/pages/'
                          * @param integer $page_id
                          *  The ID of the newly created Page
                          * @param array $fields
                          *  An associative array of data that was just saved for this page
                          */
                         Symphony::ExtensionManager()->notifyMembers('PagePostCreate', '/blueprints/pages/', array('page_id' => $page_id, 'fields' => &$fields));
                         $redirect = "/blueprints/pages/edit/{$page_id}/created/{$parent_link_suffix}";
                     }
                 } else {
                     /**
                      * Just prior to updating a Page record in `tbl_pages`, provided
                      * with the `$fields` associative array. Use with caution, as no
                      * duplicate page checks are run after this delegate has fired
                      *
                      * @delegate PagePreEdit
                      * @since Symphony 2.2
                      * @param string $context
                      * '/blueprints/pages/'
                      * @param integer $page_id
                      *  The ID of the Page that is about to be updated
                      * @param array $fields
                      *  The `$_POST['fields']` array passed by reference
                      */
                     Symphony::ExtensionManager()->notifyMembers('PagePreEdit', '/blueprints/pages/', array('page_id' => $page_id, 'fields' => &$fields));
                     if (!PageManager::edit($page_id, $fields, true)) {
                         return $this->pageAlert(__('Unknown errors occurred while attempting to save.') . '<a href="' . SYMPHONY_URL . '/system/log/">' . __('Check your activity log') . '</a>.', Alert::ERROR);
                     } else {
                         /**
                          * Just after updating a page in `tbl_pages`
                          *
                          * @delegate PagePostEdit
                          * @since Symphony 2.2
                          * @param string $context
                          * '/blueprints/pages/'
                          * @param integer $page_id
                          *  The ID of the Page that was just updated
                          * @param array $fields
                          *  An associative array of data that was just saved for this page
                          */
                         Symphony::ExtensionManager()->notifyMembers('PagePostEdit', '/blueprints/pages/', array('page_id' => $page_id, 'fields' => $fields));
                         $redirect = "/blueprints/pages/edit/{$page_id}/saved/{$parent_link_suffix}";
                     }
                 }
             }
             // Only proceed if there was no errors saving/creating the page
             if (empty($this->_errors)) {
                 /**
                  * Just before the page's types are saved into `tbl_pages_types`.
                  * Use with caution as no further processing is done on the `$types`
                  * array to prevent duplicate `$types` from occurring (ie. two index
                  * page types). Your logic can use the PageManger::hasPageTypeBeenUsed
                  * function to perform this logic.
                  *
                  * @delegate PageTypePreCreate
                  * @since Symphony 2.2
                  * @see toolkit.PageManager#hasPageTypeBeenUsed
                  * @param string $context
                  * '/blueprints/pages/'
                  * @param integer $page_id
                  *  The ID of the Page that was just created or updated
                  * @param array $types
                  *  An associative array of the types for this page passed by reference.
                  */
                 Symphony::ExtensionManager()->notifyMembers('PageTypePreCreate', '/blueprints/pages/', array('page_id' => $page_id, 'types' => &$types));
                 // Assign page types:
                 PageManager::addPageTypesToPage($page_id, $types);
                 // Find and update children:
                 if ($this->_context[0] == 'edit') {
                     PageManager::editPageChildren($page_id, $fields['path'] . '/' . $fields['handle']);
                 }
                 if ($redirect) {
                     redirect(SYMPHONY_URL . $redirect);
                 }
             }
         }
         // If there was any errors, either with pre processing or because of a
         // duplicate page, return.
         if (is_array($this->_errors) && !empty($this->_errors)) {
             return $this->pageAlert(__('An error occurred while processing this form. See below for details.'), Alert::ERROR);
         }
     }
 }