/**
  * Render delete Page Fields section admin page.  This deletes the Page Fields section.
  *
  */
 public function __viewDelete()
 {
     $sectionId = $this->_context[1];
     // Delete the section.
     //
     $sectionManager = new SectionManager($this->_Parent);
     $sectionManager->delete($sectionId);
     // Reditect to the manage page fields page.  (We can't just call __viewIndex()
     // to render the page as we need to redraw the menu).
     //
     redirect(URL . PF_MANAGE_URL);
 }
 public function __actionEdit()
 {
     if (@array_key_exists('save', $_POST['action']) || @array_key_exists('done', $_POST['action'])) {
         $canProceed = true;
         $fields = $_POST['fields'];
         $meta = $_POST['meta'];
         $section_id = $this->_context[1];
         $sectionManager = new SectionManager($this->_Parent);
         $existing_section = $sectionManager->fetch($section_id);
         $fieldManager = new FieldManager($this->_Parent);
         $this->_errors = array();
         ## Check to ensure all the required section fields are filled
         if (!isset($meta['name']) || trim($meta['name']) == '') {
             $required = array('Name');
             $this->_errors['name'] = __('This is a required field.');
             $canProceed = false;
         } elseif ($meta['name'] != $existing_section->get('name') && Symphony::Database()->fetchRow(0, "SELECT * FROM `tbl_sections` WHERE `name` = '" . $meta['name'] . "' AND `id` != {$section_id} LIMIT 1")) {
             $this->_errors['name'] = __('A Section with the name <code>%s</code> name already exists', array($meta['name']));
             $canProceed = false;
         }
         ## Check to ensure all the required section fields are filled
         if (!isset($meta['navigation_group']) || strlen(trim($meta['navigation_group'])) == 0) {
             $required = array('Navigation Group');
             $this->_errors['navigation_group'] = __('This is a required field.');
             $canProceed = false;
         } elseif (is_array($fields) && !empty($fields)) {
             ## Check for duplicate CF names
             if ($canProceed) {
                 $name_list = array();
                 foreach ($fields as $position => $data) {
                     if (trim($data['element_name']) == '') {
                         $data['element_name'] = $fields[$position]['element_name'] = Lang::createHandle($data['label'], NULL, '-', false, true, array('@^[\\d-]+@i' => ''));
                     }
                     if (trim($data['element_name']) != '' && in_array($data['element_name'], $name_list)) {
                         $this->_errors[$position] = array('label' => __('Two custom fields have the same element name. All element names must be unique.'));
                         $canProceed = false;
                         break;
                     }
                     $name_list[] = $data['element_name'];
                 }
             }
             if ($canProceed) {
                 $fieldManager = new FieldManager($this->_Parent);
                 $unique = array();
                 foreach ($fields as $position => $data) {
                     $required = NULL;
                     $field = $fieldManager->create($data['type']);
                     $field->setFromPOST($data);
                     if ($field->mustBeUnique() && !in_array($field->get('type'), $unique)) {
                         $unique[] = $field->get('type');
                     } elseif ($field->mustBeUnique() && in_array($field->get('type'), $unique)) {
                         ## Warning. cannot have 2 of this field!
                         $canProceed = false;
                         $this->_errors[$position] = array('label' => __('There is already a field of type <code>%s</code>. There can only be one per section.', array($field->name())));
                     }
                     $errors = array();
                     if (Field::__OK__ != $field->checkFields($errors, false, false) && !empty($errors)) {
                         $this->_errors[$position] = $errors;
                         $canProceed = false;
                         break;
                     }
                 }
             }
         }
         if ($canProceed) {
             $meta['handle'] = Lang::createHandle($meta['name']);
             $meta['hidden'] = isset($meta['hidden']) ? 'yes' : 'no';
             if (!$sectionManager->edit($section_id, $meta)) {
                 $this->pageAlert(__('An unknown database occurred while attempting to create the section.'), Alert::ERROR);
             } else {
                 ## Delete missing CF's
                 $id_list = array();
                 if (is_array($fields) && !empty($fields)) {
                     foreach ($fields as $position => $data) {
                         if (isset($data['id'])) {
                             $id_list[] = $data['id'];
                         }
                     }
                 }
                 $missing_cfs = Symphony::Database()->fetchCol('id', "SELECT `id` FROM `tbl_fields` WHERE `parent_section` = '{$section_id}' AND `id` NOT IN ('" . @implode("', '", $id_list) . "')");
                 if (is_array($missing_cfs) && !empty($missing_cfs)) {
                     foreach ($missing_cfs as $id) {
                         $fieldManager->delete($id);
                     }
                 }
                 ## Save each custom field
                 if (is_array($fields) && !empty($fields)) {
                     foreach ($fields as $position => $data) {
                         $field = $fieldManager->create($data['type']);
                         $field->setFromPOST($data);
                         $field->set('sortorder', (string) $position);
                         $field->set('parent_section', $section_id);
                         $bEdit = true;
                         if (!$field->get('id')) {
                             $bEdit = false;
                         }
                         ## Creation
                         if ($field->commit()) {
                             $field_id = $field->get('id');
                             ###
                             # Delegate: FieldPostCreate
                             # Delegate: FieldPostEdit
                             # Description: After creation/editing of an Field. New Field object is provided.
                             $this->_Parent->ExtensionManager->notifyMembers($bEdit ? 'FieldPostEdit' : 'FieldPostCreate', '/blueprints/sections/', array('field' => &$field, 'data' => &$data));
                         }
                     }
                 }
                 ## TODO: Fix Me
                 ###
                 # Delegate: Edit
                 # Description: After editing a Section. The ID is provided.
                 #$ExtensionManager->notifyMembers('Edit', getCurrentPage(), array('section_id' => $section_id));
                 redirect(URL . "/symphony/blueprints/sections/edit/{$section_id}/saved/");
             }
         }
     }
     if (@array_key_exists("delete", $_POST['action'])) {
         $section_id = $this->_context[1];
         $sectionManager = new SectionManager($this->_Parent);
         $sectionManager->delete($section_id);
         redirect(URL . '/symphony/blueprints/sections/');
     }
 }
 public function __actionNew()
 {
     if (@array_key_exists('save', $_POST['action']) || @array_key_exists('done', $_POST['action'])) {
         $canProceed = true;
         $edit = $this->_context[0] == "edit";
         $this->_errors = array();
         $fields = isset($_POST['fields']) ? $_POST['fields'] : array();
         $meta = $_POST['meta'];
         if ($edit) {
             $section_id = $this->_context[1];
             $existing_section = SectionManager::fetch($section_id);
         }
         // Check to ensure all the required section fields are filled
         if (!isset($meta['name']) || strlen(trim($meta['name'])) == 0) {
             $this->_errors['name'] = __('This is a required field.');
             $canProceed = false;
         } elseif ($edit) {
             $s = SectionManager::fetchIDFromHandle(Lang::createHandle($meta['name']));
             if ($meta['name'] !== $existing_section->get('name') && !is_null($s) && $s !== $section_id) {
                 $this->_errors['name'] = __('A Section with the name %s already exists', array('<code>' . $meta['name'] . '</code>'));
                 $canProceed = false;
             }
         } elseif (!is_null(SectionManager::fetchIDFromHandle(Lang::createHandle($meta['name'])))) {
             $this->_errors['name'] = __('A Section with the name %s already exists', array('<code>' . $meta['name'] . '</code>'));
             $canProceed = false;
         }
         // Check to ensure all the required section fields are filled
         if (!isset($meta['navigation_group']) || strlen(trim($meta['navigation_group'])) == 0) {
             $this->_errors['navigation_group'] = __('This is a required field.');
             $canProceed = false;
         }
         // Basic custom field checking
         if (is_array($fields) && !empty($fields)) {
             // Check for duplicate CF names
             if ($canProceed) {
                 $name_list = array();
                 foreach ($fields as $position => $data) {
                     if (trim($data['element_name']) == '') {
                         $data['element_name'] = $fields[$position]['element_name'] = $_POST['fields'][$position]['element_name'] = Lang::createHandle($data['label'], 255, '-', false, true, array('@^[\\d-]+@i' => ''));
                     }
                     if (trim($data['element_name']) != '' && in_array($data['element_name'], $name_list)) {
                         $this->_errors[$position] = array('element_name' => __('A field with this handle already exists. All handle must be unique.'));
                         $canProceed = false;
                         break;
                     }
                     $name_list[] = $data['element_name'];
                 }
             }
             if ($canProceed) {
                 $unique = array();
                 foreach ($fields as $position => $data) {
                     $field = FieldManager::create($data['type']);
                     $field->setFromPOST($data);
                     if (isset($existing_section)) {
                         $field->set('parent_section', $existing_section->get('id'));
                     }
                     if ($field->mustBeUnique() && !in_array($field->get('type'), $unique)) {
                         $unique[] = $field->get('type');
                     } elseif ($field->mustBeUnique() && in_array($field->get('type'), $unique)) {
                         // Warning. cannot have 2 of this field!
                         $canProceed = false;
                         $this->_errors[$position] = array('label' => __('There is already a field of type %s. There can only be one per section.', array('<code>' . $field->handle() . '</code>')));
                     }
                     $errors = array();
                     if (Field::__OK__ != $field->checkFields($errors, false) && !empty($errors)) {
                         $this->_errors[$position] = $errors;
                         $canProceed = false;
                     }
                 }
             }
         }
         if ($canProceed) {
             $meta['handle'] = Lang::createHandle($meta['name']);
             // If we are creating a new Section
             if (!$edit) {
                 $meta['sortorder'] = SectionManager::fetchNextSortOrder();
                 /**
                  * Just prior to saving the Section settings. Use with caution as
                  * there is no additional processing to ensure that Field's or Section's
                  * are unique.
                  *
                  * @delegate SectionPreCreate
                  * @since Symphony 2.2
                  * @param string $context
                  * '/blueprints/sections/'
                  * @param array $meta
                  *  The section's settings, passed by reference
                  * @param array $fields
                  *  An associative array of the fields that will be saved to this
                  *  section with the key being the position in the Section Editor
                  *  and the value being a Field object, passed by reference
                  */
                 Symphony::ExtensionManager()->notifyMembers('SectionPreCreate', '/blueprints/sections/', array('meta' => &$meta, 'fields' => &$fields));
                 if (!($section_id = SectionManager::add($meta))) {
                     $this->pageAlert(__('An unknown database occurred while attempting to create the section.'), Alert::ERROR);
                 }
             } else {
                 $meta['hidden'] = isset($meta['hidden']) ? 'yes' : 'no';
                 /**
                  * Just prior to updating the Section settings. Use with caution as
                  * there is no additional processing to ensure that Field's or Section's
                  * are unique.
                  *
                  * @delegate SectionPreEdit
                  * @since Symphony 2.2
                  * @param string $context
                  * '/blueprints/sections/'
                  * @param integer $section_id
                  *  The Section ID that is about to be edited.
                  * @param array $meta
                  *  The section's settings, passed by reference
                  * @param array $fields
                  *  An associative array of the fields that will be saved to this
                  *  section with the key being the position in the Section Editor
                  *  and the value being a Field object, passed by reference
                  */
                 Symphony::ExtensionManager()->notifyMembers('SectionPreEdit', '/blueprints/sections/', array('section_id' => $section_id, 'meta' => &$meta, 'fields' => &$fields));
                 if (!SectionManager::edit($section_id, $meta)) {
                     $canProceed = false;
                     $this->pageAlert(__('An unknown database occurred while attempting to create the section.'), Alert::ERROR);
                 }
             }
             if ($section_id && $canProceed) {
                 if ($edit) {
                     // Delete missing CF's
                     $id_list = array();
                     if (is_array($fields) && !empty($fields)) {
                         foreach ($fields as $position => $data) {
                             if (isset($data['id'])) {
                                 $id_list[] = $data['id'];
                             }
                         }
                     }
                     $missing_cfs = Symphony::Database()->fetchCol('id', "SELECT `id` FROM `tbl_fields` WHERE `parent_section` = '{$section_id}' AND `id` NOT IN ('" . @implode("', '", $id_list) . "')");
                     if (is_array($missing_cfs) && !empty($missing_cfs)) {
                         foreach ($missing_cfs as $id) {
                             FieldManager::delete($id);
                         }
                     }
                 }
                 // Save each custom field
                 if (is_array($fields) && !empty($fields)) {
                     foreach ($fields as $position => $data) {
                         $field = FieldManager::create($data['type']);
                         $field->setFromPOST($data);
                         $field->set('sortorder', (string) $position);
                         $field->set('parent_section', $section_id);
                         $newField = !(bool) $field->get('id');
                         $field->commit();
                         $field_id = $field->get('id');
                         if ($field_id) {
                             if ($newField) {
                                 /**
                                  * After creation of a Field.
                                  *
                                  * @delegate FieldPostCreate
                                  * @param string $context
                                  * '/blueprints/sections/'
                                  * @param Field $field
                                  *  The Field object, passed by reference
                                  * @param array $data
                                  *  The settings for ths `$field`, passed by reference
                                  */
                                 Symphony::ExtensionManager()->notifyMembers('FieldPostCreate', '/blueprints/sections/', array('field' => &$field, 'data' => &$data));
                             } else {
                                 /**
                                  * After editing of a Field.
                                  *
                                  * @delegate FieldPostEdit
                                  * @param string $context
                                  * '/blueprints/sections/'
                                  * @param Field $field
                                  *  The Field object, passed by reference
                                  * @param array $data
                                  *  The settings for ths `$field`, passed by reference
                                  */
                                 Symphony::ExtensionManager()->notifyMembers('FieldPostEdit', '/blueprints/sections/', array('field' => &$field, 'data' => &$data));
                             }
                         }
                     }
                 }
                 if (!$edit) {
                     /**
                      * After the Section has been created, and all the Field's have been
                      * created for this section, but just before the redirect
                      *
                      * @delegate SectionPostCreate
                      * @since Symphony 2.2
                      * @param string $context
                      * '/blueprints/sections/'
                      * @param integer $section_id
                      *  The newly created Section ID.
                      */
                     Symphony::ExtensionManager()->notifyMembers('SectionPostCreate', '/blueprints/sections/', array('section_id' => $section_id));
                     redirect(SYMPHONY_URL . "/blueprints/sections/edit/{$section_id}/created/");
                 } else {
                     /**
                      * After the Section has been updated, and all the Field's have been
                      * updated for this section, but just before the redirect
                      *
                      * @delegate SectionPostEdit
                      * @since Symphony 2.2
                      * @param string $context
                      * '/blueprints/sections/'
                      * @param integer $section_id
                      *  The edited Section ID.
                      */
                     Symphony::ExtensionManager()->notifyMembers('SectionPostEdit', '/blueprints/sections/', array('section_id' => $section_id));
                     redirect(SYMPHONY_URL . "/blueprints/sections/edit/{$section_id}/saved/");
                 }
             }
         }
     }
     if (@array_key_exists("delete", $_POST['action'])) {
         $section_id = array($this->_context[1]);
         /**
          * Just prior to calling the Section Manager's delete function
          *
          * @delegate SectionPreDelete
          * @since Symphony 2.2
          * @param string $context
          * '/blueprints/sections/'
          * @param array $section_ids
          *  An array of Section ID's passed by reference
          */
         Symphony::ExtensionManager()->notifyMembers('SectionPreDelete', '/blueprints/sections/', array('section_ids' => &$section_id));
         foreach ($section_id as $section) {
             SectionManager::delete($section);
         }
         redirect(SYMPHONY_URL . '/blueprints/sections/');
     }
 }