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.

Customising the CMS /

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

method visibility from module to Page_Controller


Go to End
Reply


8 Posts   2026 Views

Avatar
Pike

Community Member, 42 Posts

17 January 2010 at 3:12am

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?

Avatar
MateuszU

Community Member, 89 Posts

18 January 2010 at 4:16pm

Try changing Extension to DataObjectDecorator.

Avatar
Pike

Community Member, 42 Posts

18 January 2010 at 8:15pm

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.

Avatar
ajshort

Community Member, 244 Posts

19 January 2010 at 1:41am

Edited: 19/01/2010 1:42am

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?

Avatar
Pike

Community Member, 42 Posts

19 January 2010 at 2:04am

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.

Avatar
MateuszU

Community Member, 89 Posts

19 January 2010 at 8:54am

Edited: 19/01/2010 8:55am

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();
	}

}

?>

Avatar
Pike

Community Member, 42 Posts

19 January 2010 at 9:18am

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
Avatar
Pike

Community Member, 42 Posts

17 February 2010 at 11:31pm

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?