Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

NewsArticle - > NewsArticle_Live and DataObjectAsPage -> DataObjectAsPage_Live!!


Reply

3 Posts   777 Views

Avatar
phokki

1 June 2012 at 1:13am (Last edited: 6 June 2012 12:10am), Community Member, 14 Posts

Hello SilverStripers!

I have got a newsarticlepage which show the latest news. The problem is, the news is only shown when i'm visiting the draft page and not the live page.

I use the DataObjectAsPage Module http://www.ssbits.com/tutorials/2012/dataobject-as-pages-the-module/
(i dont use the productadmin)..

My files are: newsarticle.php and newsarticleholder.php

<><><><><<><<><><><><<><><><><><><><><><><><><><><><><><><><><><><><><>
NewsArticle.php

<?php
class NewsArticle extends DataObjectAsPage {
   
   //The class of the page which will list this DataObject
static $listing_class = 'NewsArticleHolder';
//Class Naming (optional but reccomended)
static $plural_name = 'NewsArticles';
static $singular_name = 'NewsArticle';

   static $db = array (
      'Title' => 'Varchar(255)',
      'Date' => 'Date',
   'Author' => 'Text',
   'Content' => 'HTMLText',      
   );
   
   static $has_one = array (
      'NewsHolder' => 'NewsArticleHolder',
   );

static $summary_fields = array(
'Title',
'Date',
   'Author',    
   'Content',
   'Status'
);
   
   function fieldLabels($includerelations = true) {
    $labels = parent::fieldLabels($includerelations);
   
    $labels['Title'] = _t('newsarticle.TITLE', 'Titel');
    $labels['Date'] = _t('newsarticle.DATE', 'Datum');
    $labels['Author'] = _t('newsarticle.AUTHOR', 'Auteur');
    $labels['Status'] = _t('newsarticle.STATUS', 'Status');
    $labels['Content'] = _t('newsarticle.CONTENT', 'Niewsbericht');
    return $labels;
   }
   
//options for the new OptionsetField
protected $options = array(
    'Published' => 'Yes',
    'Draft' => 'No'
   );
   
   
       
   function getCMSFields() {

    $fields = new Fieldset();

      //Add the status/view link
      if($this->ID)
      {
         $color = '#E88F31';
         $links = '<a target="_blank" href="' . $this->Link('?stage=Stage') . '">Draft</a>';
         $status = $this->Status;
         
         if($this->Status == 'Published')
         {
            $color = '#000';
            $links .= ' | <a target="_blank" href="' . $this->Link('?stage=Live') . '">Published</a>';
            
            if($this->hasChangesOnStage())
            {
               $status .= ' (changed)';
               $color = '#428620';
            }
         }

         $fields->insertFirst(new LiteralField('',
            '<div class="doapToolbar">
               <h3 class="doapTitle" style="background: '.$color.';">'. $status . '</h3>
               <p class="doapViewLinks">
                  Page View:' . $links . '
               </p>
            </div>
            '
         ));
      }

    $fields->push(new TextField('Title','Titel'), 'Content');
    $fields->push($dateField = new DateField('Date','Datum'), 'Content');
    // $fields->push(new CheckboxField ('Status','Publish'),'Content') ;
    $fields->push(new OptionsetField(
         'Status',
         'Publishing',
         $this->options,
         0
         ));
      //$this->doPublish();
    $dateField->setConfig('showcalendar', true);
    $dateField->setConfig('dateformat', 'dd/MM/YYYY');
   
    $fields->push(new TextField('Author','Auteur'), 'Content');
   
    $fields->push(new HtmlEditorField("Content", _t('SiteTree.FOO', "Content", PR_MEDIUM, 'HTML editor title')));
   
    return $fields;
   }
   
}

<><><><><><><><><><><><><><><>><<><><><><><><><><><><><><><><><><><><><><>
NewsArticleHolder.php

<?php

class NewsArticleHolder extends DataObjectAsPageHolder {
   
   static $has_many = array (
       'NewsArticles' => 'NewsArticle',
   );
   
   
   public function getCMSFields() {
      $fields = parent::getCMSFields();
      $fields->addFieldsToTab("Root.Content.Nieuws", new DataObjectManager($this, 'NewsArticles', 'NewsArticle'));      
      return $fields;
   }
}

class NewsArticleHolder_Controller extends DataObjectAsPageHolder_Controller {
//This needs to know be the Class of the DataObject you want this page to list
static $item_class = 'NewsArticle';
//Set the sort for the items (defaults to Created DESC)
static $item_sort = 'Title ASC';

}

The problem is:

When i post a new newsarticle, it inserts it to DataObjectAsPage table
but not in the DataObjectAsPage_Live table.....

Same is for NewsArticle.. It inserts in NewsArticle table but not in NewsArticle_Live....

I wanted to fix this by using a checkbox for Publishing or Drafting, this works but i dont know where to use
doPublish()..

Does anyone knows a way to fix this?
I've got no solution yet...

Thanks!!!!!!:)

Attached Files
Avatar
phokki

4 June 2012 at 11:47pm (Last edited: 6 June 2012 12:10am), Community Member, 14 Posts

Another question: is the doPublish() function the right function to insert my page into the dataobjectaspage_live table or is there an another function for it?

And if i change the "article" to draft, i probably need doUnpublish() ?

But the main question: how can i use those functions!!

Thanks !

Avatar
phokki

11 June 2012 at 7:30pm Community Member, 14 Posts

bump