Hi all,
is there way to have comments in DataObject page?
I can view module in the page, i can insert comment, send email to admin when comment is
published, but the comment is published in all DataObject Pages.
Sorry for my english..
my Holder page is this:
CategoryPage.php
-----------------
<?php
class CategoryPage extends Page
{
public static $has_one = array(
'CategoryBanner' => 'Image'
);
public static $many_many = array(
'Products' => 'Product'
);
public static $has_many = array (
'Immagini' => 'Immagine',
'ImmaginiGalleria' => 'ImmagineGalleria'
);
static $allowed_children = array(
'none' => 'none'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields = parent::getCMSFields();
$manager = new FileDataObjectManager(
$this, // Controller
'ImmaginiGalleria', // Source name
'ImmagineGalleria', // Source class
'ImgGal',
array(
'Descrizione' => 'Descrizione',
), // Headings
'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
);
$fields->addFieldToTab("Root.Content.ImmaginiGalleria", $manager);
return $fields;
}
}
class CategoryPage_Controller extends Page_Controller
{
static $allowed_actions = array(
'show',
'InfoForm',
'PageComment'
);
public function init()
{
parent::init();
Requirements::css('products/css/products.css');
}
//Return the list of products for this category
public function getProductsList()
{
return $this->Products(Null, 'Epoca ASC');
}
//Get's the current product from the URL, if any
public function getCurrentProduct()
{
$Params = $this->getURLParams();
$URLSegment = Convert::raw2sql($Params['ID']);
if($URLSegment && $Product = DataObject::get_one('Product', "URLSegment = '" . $URLSegment . "'"))
{
return $Product;
}
}
//Shows the Product detail page
function show()
{
//Get the Product
if($Product = $this->getCurrentProduct())
{
$Data = array(
'Product' => $Product,
'MetaTitle' => $Product->MetaTitle
);
//return our $Data array to use, rendering with the ProductPage.ss template
return $this->customise($Data)->renderWith(array('ProductPage', 'Page'));
}
else //Product not found
{
return $this->httpError(404, 'Sorry that product could not be found');
}
}
//Generate out custom breadcrumbs
public function Breadcrumbs() {
//Get the default breadcrumbs
$Breadcrumbs = parent::Breadcrumbs();
if($Product = $this->getCurrentProduct())
{
//Explode them into their individual parts
$Parts = explode(SiteTree::$breadcrumbs_delimiter, $Breadcrumbs);
//Count the parts
$NumOfParts = count($Parts);
//Change the last item to a link instead of just text
$Parts[$NumOfParts-1] = ('<a href="' . $this->Link() . '">' . $Parts[$NumOfParts-1] . '</a>');
//Add our extra piece on the end
$Parts[$NumOfParts] = $Product->Title;
//Return the imploded array
$Breadcrumbs = implode(SiteTree::$breadcrumbs_delimiter, $Parts);
}
return $Breadcrumbs;
}
function InfoForm() {
// Create fields
$Params = Director::urlParams();
$fields = new FieldSet(
new TextField('Nome', 'Nome *'),
new TextField('Cognome', 'Cognome *'),
new EmailField('Email', 'Email *'),
new TextareaField('Note','Note'),
new HiddenField ('Info','Info',$this->Titolo),
new PhpCaptchaField('Captcha','')
);
// Create action
$actions = new FieldSet(
new FormAction('SendInfoForm', 'Invia')
);
// Create action
$validator = new RequiredFields('Nome', 'Cognome', 'Email');
return new Form($this, 'InfoForm', $fields, $actions, $validator);
}
function SendInfoForm($data) {
//Set data
$From = $data['Email'];
$To = "max@quamm.it";
$Subject = "Modulo Informazioni dal sito Giuseppediprinzio";
$email = new Email($From, $To, $Subject);
//set template
$email->setTemplate('ContactEmail');
//populate template
$email->populateTemplate($data);
//send mail
$email->send();
//return to submitted message
Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
}
public function Success()
{
return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
}
}
---------
My Product Page is this
Product.php
-------------
<?php
class Product extends DataObject
{
static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'HTMLText',
'URLSegment' => 'Varchar(255)',
'MetaTitle' => 'Varchar(255)',
"Epoca" => "Enum(' , Anni ’20 - ’40, Anni ’40 - ’50, Anni ’50 - ’60, Anni ’60 - ’80, Anni ’80 - ’90, Anni ’90 - ’99', '')",
"Materiale" => "Enum(' , Terracotta, Ceramica Policroma, Carta, Gesso, Bronzo, Cera, Pietra, Argento', '')",
"Tipologia" => "Enum(' , Bassorilievo, Altorilievo, Tuttotondo, Incisione, Schizzo, Disegno b/n, Disegno colorato, Piatto Smaltato, Piastrella Dipinta', '')",
"Cani" => "Boolean",
"Cavalli" => "Boolean",
"CavalliAlati" => "Boolean",
"Centauri" => "Boolean",
"Donne" => "Boolean",
"Uomini" => "Boolean",
"Angeli" => "Boolean",
"Santi" => "Boolean",
"Bambini" => "Boolean",
"Paesaggi" => "Boolean",
"Barche" => "Boolean",
"Case" => "Boolean",
"Paesi" => "Boolean",
"Astratto" => "Boolean",
"Colombe" => "Boolean",
"Nudo" => "Boolean"
);
//Set our defaults
static $defaults = array(
'Title' => 'New Product',
'URLSegment' => 'new-product'
);
static $has_one = array(
'Image' => 'Image'
);
//Relate to the category pages
static $belongs_many_many = array(
'Categories' => 'CategoryPage'
);
//Fields to show in ModelAdmin table
static $summary_fields = array(
'Thumb' => 'Foto',
'Title' => 'Title',
'URLSegment' => 'URLSegment',
);
//Add an SQL index for the URLSegment
static $indexes = array(
"URLSegment" => true
);
//Fields to search in ModelAdmin
static $searchable_fields = array (
'Title',
'URLSegment',
'Description',
'Categories.ID' => array(
'title' => 'Category'
)
);
//Generate our thumbnail for the DOM
public function getThumb()
{
if($this->ImageID && $this->Image())
return $this->Image()->CMSThumbnail();
else
return '(No Image)';
}
function getCMSFields()
{
$fields = parent::getCMSFields();
//Main Tab
$fields->addFieldToTab("Root.Main", new TextField('Title', 'Title'));
$fields->addFieldToTab("Root.Main", new TextField('URLSegment', 'URL Segment'));
$fields->addFieldToTab("Root.Main", new TextField('MetaTitle', 'Meta Title')); $fields->addFieldsToTab('Root.SchedaOpera', array(new DropdownField('Epoca', 'Epoca', $this->dbObject('Epoca')->enumValues())));
$fields->addFieldsToTab('Root.SchedaOpera', array(new DropdownField('Materiale', 'Materiale', $this->dbObject('Materiale')->enumValues())));
$fields->addFieldsToTab('Root.SchedaOpera', array(new DropdownField('Tipologia', 'Tipologia', $this->dbObject('Tipologia')->enumValues())));
$fields->addFieldToTab('Root.SchedaOpera', new LabelField ("Soggetti","Soggetti:"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Cavalli","Cavalli"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("CavalliAlati","Cavalli Alati"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Cani","Cani"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Centauri","Centauri"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Donne","Donne"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Uomini","Uomini"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Angeli","Angeli"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Santi","Santi"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Bambini","Bambini"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Paesaggi","Paesaggi"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Barche","Barche"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Case","Case"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Paesi","Paesi"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Astratto","Astratto"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Colombe","Colombe"));
$fields->addFieldsToTab('Root.SchedaOpera', new CheckBoxField("Nudo","Nudo"));
$fields->addFieldToTab("Root.Main", new HTMLEditorField('Description','Descrizione'));
//Categories
$Categories = DataObject::get('CategoryPage');
$fields->addFieldToTab("Root.Categories", new CheckboxsetField('Categories', 'Categorie', $Categories));
//Images
$fields->addFieldToTab("Root.Immagini", new ImageField('Image', 'Immagine', Null, Null, Null, 'Opere/'));
return $fields;
}
//Set URLSegment to be unique on write
function onBeforeWrite()
{
// If there is no URLSegment set, generate one from Title
if((!$this->URLSegment || $this->URLSegment == 'new-product') && $this->Title != 'New Product')
{
$this->URLSegment = SiteTree::generateURLSegment($this->Title);
}
else if($this->isChanged('URLSegment'))
{
// Make sure the URLSegment is valid for use in a URL
$segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
$segment = preg_replace('/-+/','-',$segment);
// If after sanitising there is no URLSegment, give it a reasonable default
if(!$segment) {
$segment = "product-$this->ID";
}
$this->URLSegment = $segment;
}
// Ensure that this object has a non-conflicting URLSegment value.
$count = 2;
while($this->LookForExistingURLSegment($this->URLSegment))
{
$this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
$count++;
}
parent::onBeforeWrite();
}
//Test whether the URLSegment exists already on another Product
function LookForExistingURLSegment($URLSegment)
{
return (DataObject::get_one('Product', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
}
//Generate the link for this product
function Link()
{
//if we are on a category page return that
if(Director::CurrentPage()->ClassName == 'CategoryPage')
{
$Category = Director::CurrentPage();
}
//Otherwise just grab the first category this product is in
else
{
$Category = $this->Categories()->First();
}
//Check we have a category then return the link
if($Category)
{
return $Category->absoluteLink() . 'show/' . $this->URLSegment;
}
}
function AbsoluteLink() {
return Director::absoluteURL($this->Link());
}
}
If I add $PageComments in my .ss template when submit it, comment appear in each product...
I hope anyone help me.
Thanks
Max