public function __form($readonly = false)
 {
     $formHasErrors = is_array($this->_errors) && !empty($this->_errors);
     if ($formHasErrors) {
         $this->pageAlert(__('An error occurred while processing this form. See below for details.'), Alert::ERROR);
         // These alerts are only valid if the form doesn't have errors
     } elseif (isset($this->_context[2])) {
         $time = Widget::Time();
         switch ($this->_context[2]) {
             case 'saved':
                 $this->pageAlert(__('Event updated at %s.', array($time->generate())) . ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">' . __('View all Events') . '</a>', Alert::SUCCESS);
                 break;
             case 'created':
                 $this->pageAlert(__('Event created at %s.', array($time->generate())) . ' <a href="' . SYMPHONY_URL . '/blueprints/events/new/" accesskey="c">' . __('Create another?') . '</a> <a href="' . SYMPHONY_URL . '/blueprints/events/" accesskey="a">' . __('View all Events') . '</a>', Alert::SUCCESS);
                 break;
         }
     }
     $isEditing = $readonly ? true : false;
     $fields = array("name" => null, "filters" => null);
     $about = array("name" => null);
     $providers = Symphony::ExtensionManager()->getProvidersOf(iProvider::EVENT);
     if (isset($_POST['fields'])) {
         $fields = $_POST['fields'];
         if ($this->_context[0] == 'edit') {
             $isEditing = true;
         }
     } elseif ($this->_context[0] == 'edit' || $this->_context[0] == 'info') {
         $isEditing = true;
         $handle = $this->_context[1];
         $existing = EventManager::create($handle);
         $about = $existing->about();
         if ($this->_context[0] == 'edit' && !$existing->allowEditorToParse()) {
             redirect(SYMPHONY_URL . '/blueprints/events/info/' . $handle . '/');
         }
         $fields['name'] = $about['name'];
         $fields['source'] = $existing->getSource();
         $provided = false;
         if (!empty($providers)) {
             foreach ($providers as $providerClass => $provider) {
                 if ($fields['source'] == call_user_func(array($providerClass, 'getClass'))) {
                     $fields = array_merge($fields, $existing->settings());
                     $provided = true;
                     break;
                 }
             }
         }
         if (!$provided) {
             if (isset($existing->eParamFILTERS)) {
                 $fields['filters'] = $existing->eParamFILTERS;
             }
         }
     }
     // Handle name on edited changes, or from reading an edited datasource
     if (isset($about['name'])) {
         $name = $about['name'];
     } elseif (isset($fields['name'])) {
         $name = $fields['name'];
     }
     $this->setPageType('form');
     $this->setTitle(__($isEditing ? '%1$s &ndash; %2$s &ndash; %3$s' : '%2$s &ndash; %3$s', array($about['name'], __('Events'), __('Symphony'))));
     $this->appendSubheading($isEditing ? $about['name'] : __('Untitled'));
     $this->insertBreadcrumbs(array(Widget::Anchor(__('Events'), SYMPHONY_URL . '/blueprints/events/')));
     if (!$readonly) {
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings');
         $fieldset->appendChild(new XMLElement('legend', __('Essentials')));
         // Target
         $sources = new XMLElement('div', null, array('class' => 'apply actions'));
         $div = new XMLElement('div');
         $label = Widget::Label(__('Target'), null, 'apply-label-left');
         $sources->appendChild($label);
         $sources->appendChild($div);
         $sections = SectionManager::fetch(null, 'ASC', 'name');
         $options = array();
         $section_options = array();
         $source = isset($fields['source']) ? $fields['source'] : null;
         if (is_array($sections) && !empty($sections)) {
             $section_options = array('label' => __('Sections'), 'options' => array());
             foreach ($sections as $s) {
                 $section_options['options'][] = array($s->get('id'), $source == $s->get('id'), General::sanitize($s->get('name')));
             }
         }
         $options[] = $section_options;
         // Loop over the event providers
         if (!empty($providers)) {
             $p = array('label' => __('From extensions'), 'options' => array());
             foreach ($providers as $providerClass => $provider) {
                 $p['options'][] = array($providerClass, $fields['source'] == $providerClass, $provider);
             }
             $options[] = $p;
         }
         $div->appendChild(Widget::Select('source', $options, array('id' => 'event-context')));
         if (isset($this->_errors['source'])) {
             $this->Context->prependChild(Widget::Error($sources, $this->_errors['source']));
         } else {
             $this->Context->prependChild($sources);
         }
         $this->Form->appendChild(Widget::Input('fields[source]', $options[0]['options'][0][0], 'hidden', array('id' => 'event-source')));
         // Name
         $group = new XMLElement('div');
         $label = Widget::Label(__('Name'));
         $label->appendChild(Widget::Input('fields[name]', General::sanitize($fields['name'])));
         $div = new XMLElement('div');
         $div->setAttribute('class', 'column');
         if (isset($this->_errors['name'])) {
             $div->appendChild(Widget::Error($label, $this->_errors['name']));
         } else {
             $div->appendChild($label);
         }
         $group->appendChild($div);
         $fieldset->appendChild($group);
         $this->Form->appendChild($fieldset);
         // Filters
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings pickable');
         $fieldset->appendChild(new XMLElement('legend', __('Filters')));
         $p = new XMLElement('p', __('Event Filters add additional conditions or actions to an event.'));
         $p->setAttribute('class', 'help');
         $fieldset->appendChild($p);
         $filters = isset($fields['filters']) ? $fields['filters'] : array();
         $options = array(array('admin-only', in_array('admin-only', $filters), __('Admin Only')), array('send-email', in_array('send-email', $filters), __('Send Notification Email')), array('expect-multiple', in_array('expect-multiple', $filters), __('Allow Multiple')));
         /**
          * Allows adding of new filter rules to the Event filter rule select box
          *
          * @delegate AppendEventFilter
          * @param string $context
          * '/blueprints/events/(edit|new|info)/'
          * @param array $selected
          *  An array of all the selected filters for this Event
          * @param array $options
          *  An array of all the filters that are available, passed by reference
          */
         Symphony::ExtensionManager()->notifyMembers('AppendEventFilter', '/blueprints/events/' . $this->_context[0] . '/', array('selected' => $filters, 'options' => &$options));
         $fieldset->appendChild(Widget::Select('fields[filters][]', $options, array('multiple' => 'multiple', 'id' => 'event-filters')));
         $this->Form->appendChild($fieldset);
         // Connections
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings');
         $fieldset->appendChild(new XMLElement('legend', __('Attach to Pages')));
         $p = new XMLElement('p', __('The event will only be available on the selected pages.'));
         $p->setAttribute('class', 'help');
         $fieldset->appendChild($p);
         $div = new XMLElement('div');
         $label = Widget::Label(__('Pages'));
         $pages = PageManager::fetch();
         $event_handle = str_replace('-', '_', Lang::createHandle($fields['name']));
         $connections = ResourceManager::getAttachedPages(RESOURCE_TYPE_EVENT, $event_handle);
         $selected = array();
         foreach ($connections as $connection) {
             $selected[] = $connection['id'];
         }
         $options = array();
         foreach ($pages as $page) {
             $options[] = array($page['id'], in_array($page['id'], $selected), PageManager::resolvePageTitle($page['id']));
         }
         $label->appendChild(Widget::Select('fields[connections][]', $options, array('multiple' => 'multiple')));
         $div->appendChild($label);
         $fieldset->appendChild($div);
         $this->Form->appendChild($fieldset);
         // Providers
         if (!empty($providers)) {
             foreach ($providers as $providerClass => $provider) {
                 if ($isEditing && $fields['source'] !== call_user_func(array($providerClass, 'getSource'))) {
                     continue;
                 }
                 call_user_func_array(array($providerClass, 'buildEditor'), array($this->Form, &$this->_errors, $fields, $handle));
             }
         }
     } else {
         // Author
         if (isset($about['author']['website'])) {
             $link = Widget::Anchor($about['author']['name'], General::validateURL($about['author']['website']));
         } elseif (isset($about['author']['email'])) {
             $link = Widget::Anchor($about['author']['name'], 'mailto:' . $about['author']['email']);
         } else {
             $link = $about['author']['name'];
         }
         if ($link) {
             $fieldset = new XMLElement('fieldset');
             $fieldset->setAttribute('class', 'settings');
             $fieldset->appendChild(new XMLElement('legend', __('Author')));
             $fieldset->appendChild(new XMLElement('p', $link->generate(false)));
             $this->Form->appendChild($fieldset);
         }
         // Version
         $fieldset = new XMLElement('fieldset');
         $fieldset->setAttribute('class', 'settings');
         $fieldset->appendChild(new XMLElement('legend', __('Version')));
         $version = array_key_exists('version', $about) ? $about['version'] : null;
         $release_date = array_key_exists('release-date', $about) ? $about['release-date'] : filemtime(EventManager::__getDriverPath($handle));
         if (preg_match('/^\\d+(\\.\\d+)*$/', $version)) {
             $fieldset->appendChild(new XMLElement('p', __('%1$s released on %2$s', array($version, DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))));
         } elseif (!is_null($version)) {
             $fieldset->appendChild(new XMLElement('p', __('Created by %1$s at %2$s', array($version, DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))));
         } else {
             $fieldset->appendChild(new XMLElement('p', __('Last modified on %s', array(DateTimeObj::format($release_date, __SYM_DATE_FORMAT__)))));
         }
         $this->Form->appendChild($fieldset);
     }
     // If we are editing an event, it assumed that the event has documentation
     $fieldset = new XMLElement('fieldset');
     $fieldset->setAttribute('id', 'event-documentation');
     $fieldset->setAttribute('class', 'settings');
     if ($isEditing && method_exists($existing, 'documentation')) {
         $doc = $existing->documentation();
         if ($doc) {
             $fieldset->setValue('<legend>' . __('Documentation') . '</legend>' . PHP_EOL . General::tabsToSpaces(is_object($doc) ? $doc->generate(true, 4) : $doc));
         }
     }
     $this->Form->appendChild($fieldset);
     $div = new XMLElement('div');
     $div->setAttribute('class', 'actions');
     $div->appendChild(Widget::Input('action[save]', $isEditing ? __('Save Changes') : __('Create Event'), 'submit', array('accesskey' => 's')));
     if ($isEditing) {
         $button = new XMLElement('button', __('Delete'));
         $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'button confirm delete', 'title' => __('Delete this event'), 'type' => 'submit', 'accesskey' => 'd', 'data-message' => __('Are you sure you want to delete this event?')));
         $div->appendChild($button);
     }
     if (!$readonly) {
         $this->Form->appendChild($div);
     }
 }