Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » method visibility from module to Page_Controller

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

Page: 1
Go to End
Author Topic: 1660 Views
  • Pike
    Avatar
    Community Member
    40 Posts

    method visibility from module to Page_Controller Link to this post

    Hi,

    I made module PageAddons (Object::add_extension('Page_Controller','PageAddons');)

    class PageAddons extends Extension {
    ..............................
    }

    When I add methods ToPrint() and ToPDf() to Page_Controller, it is visible and works.

    class Page_Controller extends ContentController {
    ............................
    public function ToPrint() {
          $absoluteBase    = Director::absoluteBaseURL();
          $current_theme   = SSViewer::current_theme();
          Requirements::insertHeadTags('<link rel="stylesheet" type="text/css" href="' . $absoluteBase . 'themes/' . $current_theme . '/css/print.css" media="all" />');
          $page_url = Controller::curr()->Link();
          $page_url = str_replace("/","", substr( $page_url, 1) );
          $filename = trim( $page_url . '.html' );
          $filename_path = ASSETS_PATH . '/.private/'. $filename;
          $htmlFile = Director::absoluteBaseURL() . ASSETS_DIR . '/.private/'. $filename;
          $Rendered = $this;
          $Rendered = $this->customise( $Rendered )->renderWith( array( 'Page_print','Page' ) );
          if ( file_exists( $filename_path ) ) unlink ( $filename_path );
          $fh = fopen( $filename_path, "wb" ) or user_error( "Couldn't open $filename for writing", E_USER_ERROR );
          fwrite( $fh, $Rendered ) or user_error( "Couldn't write content to $filename", E_USER_ERROR );
          fclose( $fh );
          Director::redirect( $htmlFile );
       }
       public function ToPdf() {
          $absoluteBase    = Director::absoluteBaseURL();
          $current_theme   = SSViewer::current_theme();
          Requirements::insertHeadTags('<link rel="stylesheet" type="text/css" href="' . $absoluteBase . 'themes/' . $current_theme . '/css/print.css" media="all" />');
          $page_url = Controller::curr()->Link();
          $page_url = str_replace("/",'', $page_url );
          $filename = $page_url; // no extension ".pdf"
          $Rendered = $this;
          $Rendered = $this->customise( $Rendered )->renderWith( array( 'Page_print','Page' ) );
          $pdf = new PageAddonsPDF();
          $pdf->setData( $Rendered );
          $pdf->setName( $filename );
          // param 1 => show page from memory
          // param 2 => save to file before show
          $pdf->getPDF();
       }
    .................................
    }

    But when I put thoses methods to PageAddons, it is not visible and not callable.

    class PageAddons extends Extension {
    ..............................
       public function ToPrint() {
          $page_url = Controller::curr()->Link();
          $page_url = str_replace("/","", substr( $page_url, 1) );
          $filename = trim( $page_url . '.html' );
          $filename_path = ASSETS_PATH . '/.private/'. $filename;
          $htmlFile = Director::absoluteBaseURL() . ASSETS_DIR . '/.private/'. $filename;
          $Rendered = $this;
          $Rendered = $this->customise( $Rendered )->renderWith( array( 'Page_print' ) );
          if ( file_exists( $filename_path ) ) unlink ( $filename_path );
          $fh = fopen( $filename_path, "wb" ) or user_error( "Couldn't open $filename for writing", E_USER_ERROR );
          fwrite( $fh, $Rendered ) or user_error( "Couldn't write content to $filename", E_USER_ERROR );
          fclose( $fh );
          Director::redirect( $htmlFile );
       }
       public function ToPdf() {
          $page_url = Controller::curr()->Link();
          $page_url = str_replace("/",'', $page_url );
          $filename = $page_url; // no extension ".pdf"
          $Rendered = $this;
          $Rendered = $this->customise( $Rendered )->renderWith( array( 'Page_print' ) );
          $pdf = new PageAddonsPDF();
          $pdf->setData( $Rendered );
          $pdf->setName( $filename );
          // param 1 => show page from memory
          // param 2 => save to file before show
          $pdf->getPDF();
       }
    ..............................
    }

    Why? Ho can I call those methods directly from PageAddons module?
    Can someone help me?

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Try changing Extension to DataObjectDecorator.

  • Pike
    Avatar
    Community Member
    40 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Hi,

    thx for reply.
    No success.
    Error:
    DataObjectDecorator->setOwner(): Trying to decorate an object of class 'Page_Controller' with 'PageAddons', only Dataobject subclasses are supported.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Where did you place the call to Object::add_extension? If you placed it in the same file as the class it won't get picked up as the class wouldn't be included. Is it in the module's _config.php file?

  • Pike
    Avatar
    Community Member
    40 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Hi,

    Yes, I placed Object::add_axtension into module/_config.
    It's wrong?
    I want to be all codes placed in its own module directory (the bailwick) .
    I hate fractionalism, placing all modules under root SS.

  • MateuszU
    Avatar
    Community Member
    89 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Ah yes, decorating non-DataObjects with DataObjectDecorators works only in 2.4. But I have just checked on 2.3.4 and Extension works fine for me. You have to define all the classes in files with appropriate names, the config file must be called _config.php and reside in the root of the module. Check if the config file gets executed at all. Then run dev/build, as otherwise the extension class will not get picked up.
    The following code works on vanilla 2.3 branch installation:

    module/_config.php

    <?php

    Object::add_extension('Page_Controller', 'Addon');

    module/code/Addon.php

    <?php

    class Addon extends Extension {
       public function addedFunction() {
          var_dump('works');
       }
    }

    mysite/code/Page.php

    <?php

    class Page extends SiteTree {

       static $db = array(
       );

       static $has_one = array(
    );

    }

    class Page_Controller extends ContentController {

       function init() {
          parent::init();      
          Requirements::themedCSS('layout');
          Requirements::themedCSS('typography');
          Requirements::themedCSS('form');
          
          $this->addedFunction();
       }

    }

    ?>

  • Pike
    Avatar
    Community Member
    40 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Hi,

    I'm not sure.........

    I like joomla article content properties, it's missing me in SS.
    So I'm trying to write yourself....

    It'll contains:

    Front-end:
    Rating
    ToPDF
    ToPrint
    ShareIt
    ToFriend (not finnished)
    Back-end:
    Checkboxes for show/hide those properties..........

    Can I send whole codes to you???

    Attached Files
  • Pike
    Avatar
    Community Member
    40 Posts

    Re: method visibility from module to Page_Controller Link to this post

    Hi,

    I want call method(s) directly from PageAddons/code/PageAddons.php module, in front-end.
    I don't want call method(s) from mysite/code/Page.php.
    Do you understend me?

    1660 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.