private function recalculateStages()
 {
     if (self::$supress_triggers) {
         return;
     }
     $id = $this->owner->ID;
     // ID gets set to 0 on publish, so we need to save it
     ModeratableState::push_state("any");
     self::$supress_triggers = true;
     if ($approved = $this->latestVersionMatching($id, $this->where('approved'))) {
         $this->owner->ID = $id;
         self::$generate_new_version = true;
         $this->owner->publish($approved, $this->liveStage);
     } else {
         ModeratableState::push_state('approved');
         $this->owner->delete();
         ModeratableState::pop_state();
     }
     /* Update the Default stage */
     $this->owner->ID = $id;
     if ($latest = $this->latestVersionMatching($id)) {
         $this->owner->ID = $id;
         self::$generate_new_version = true;
         $this->owner->publish($latest, $this->defaultStage);
     } else {
         $this->owner->delete();
     }
     self::$supress_triggers = false;
     ModeratableState::pop_state();
 }
 public function Results($searchCriteria)
 {
     switch ($searchCriteria['State']) {
         case 'approved':
             $moderationState = "approved";
             $title = "Approved";
             $commands = array('unapprove' => 'Unapprove', 'isspam' => 'Is Spam');
             break;
         case 'unapproved':
             $moderationState = "unapproved";
             $title = "Waiting Moderation";
             $commands = array('approve' => 'Approve', 'isspam' => 'Is Spam');
             break;
         default:
             $moderationState = "spam";
             $title = "Spam";
             $commands = array('approve' => 'Approve', 'isham' => 'Not Spam');
     }
     $commands['delete'] = 'Delete';
     if (($class = $this->getModelClass()) == 'All') {
         $ds = new DataObjectSet();
         foreach ($this->parentController->getManagedModels() as $class) {
             if ($class != 'All') {
                 $ds->merge(singleton($class)->getModeratedItems($moderationState, '', 'Created'));
             }
         }
     } else {
         ModeratableState::push_state($moderationState);
         $ds = DataObject::get($class, "{$this->getSearchQuery($searchCriteria)->getFilter()}", 'Created', null, $searchCriteria['Page'] * self::$page_length . ',' . self::$page_length);
         ModeratableState::pop_state();
     }
     if (!$ds) {
         return '<p>No Results</p>';
     }
     $blocks = array();
     $paging = array();
     $fields = new FieldSet();
     foreach ($searchCriteria as $k => $v) {
         if ($k != 'SecurityID') {
             $fields->push(new HiddenField($k, $k, $v));
         }
     }
     $form = new Form($this, 'SearchForm', $fields, new FieldSet());
     $form->setHTMLID('Form_CurrentSearchForm');
     $blocks[] = $form->forTemplate();
     if ($ds) {
         foreach ($ds as $do) {
             $links = array();
             foreach ($commands as $command => $text) {
                 $links[] = "<input class='action ajaxaction' type='button' value='{$text}' action='{$this->parentController->Link("{$do->ClassName}/{$do->ID}/{$moderationState}/{$command}")}' />";
             }
             $templates = array();
             foreach (array_reverse(ClassInfo::ancestry($do->ClassName)) as $class) {
                 if ($class == 'DataObject') {
                     break;
                 }
                 $templates[] = $class . 'Moderation';
             }
             $data = new ArrayData(array('ID' => $do->ID, 'ModerationLinks' => implode('', $links), 'Preview' => $do->renderWith($templates)));
             $blocks[] = $data->renderWith('ModerationPreview');
         }
     }
     if ($ds->MoreThanOnePage()) {
         // Build search info
         $paging[] = '<div>Viewing Page ' . $ds->CurrentPage() . ' of ' . $ds->TotalPages() . '</div>';
         if ($ds->NotFirstPage()) {
             $paging[] = "<input class='action pageaction' type='button' value='Prev' action='prev' />";
         }
         if ($ds->NotLastPage()) {
             $paging[] = "<input class='action pageaction' type='button' value='Next' action='next' />";
         }
     }
     $data = new ArrayData(array('State' => ucwords($searchCriteria['State']), 'Class' => $this->getModelClass(), 'Pagination' => implode("\n", $paging), 'Moderation' => implode("\n", $blocks)));
     return $data->renderWith('Moderation');
 }