21/10/2010 12:11pm

PHP | Working Example - On After Write


<?php


class Designer extends Member {
static $db = array(
'About' => 'HTMLText',
'Country' => 'Text',
'URL' => 'Text',
"Approved" => "Boolean",
"VisibleOnSite" => "Boolean"
);

static $has_one = array(
'Designer_ProfilePicture' => 'Designer_ProfilePicture',
'Designer_ProfilePictureExtra' => 'Designer_ProfilePictureExtra',
'ReferenceType' => 'ReferenceType'
);

static $has_many = array(
'References' => 'Reference'
);

static $many_many = array(
'Regions' => 'Region'
);

static $field_labels = array(
'Designer_ProfilePicture' => 'Default Profile Picture',
'Designer_ProfilePictureExtra' => 'Extra Profile Picture',
);

public function getDefaultSearchContext() {
$context = parent::getDefaultSearchContext();
$member = Member::currentUser();
if(!Permission::checkMember($member, 'ADMIN')) {
$context->removeFieldByName('FirstName');
$context->removeFieldByName('Surname');
$context->removeFieldByName('Email');
if($member->Approved) {
$context->addField(new HiddenField ("Email", "Email", $member->Email));
} else {
$context->addField(new HiddenField ("Email", "Email", "abcedfeda"));
}
}
return $context;
}


function ExtraImg() {
$randomNumber = rand(1,8);
return ($randomNumber == 1);
}

public function getCMSFields()
{

$member = Member::currentUser();

$f = parent::getCMSFields();
$f->removeByName('About');
$f->removeByName('ReferenceTypeID');
$f->removeByName('MemberUserDetailsHeader');
$f->removeByName('Country');
$f->removeByName('URL');
if(!Permission::checkMember($member, 'ADMIN')) {
$f->removeByName('Approved');
$f->removeByName('VisibleOnSite');
}
$regions = DataObject::get('Region');
$regions = $regions->toDropdownMap('Title', 'Title');
$f->addFieldToTab("Root.Main", new DropDownField('Country', 'Country', $regions), 'Password');
$f->addFieldToTab("Root.Main", new TextField('URL', 'Website'), 'Country');
$f->addFieldToTab("Root.Main", new HtmlEditorField('About'), 'Password');
$f->addFieldToTab("Root.Main", new HeaderField('Password and Language', '2'), 'Password');
$f->addFieldToTab("Root.Main", new HeaderField('Approval', '2'), 'Approved');
$f->addFieldToTab("Root.Main", new HeaderField('Profile Pictures', '2'), 'Designer_ProfilePicture');
$f->addFieldToTab("Root.Main", new HeaderField('Regions this designer works in', '2'), 'Regions');
$f->addFieldToTab("Root.Main", new HeaderField('References', '2'), 'References');


// rebuild the regions tab
$f->removeByName('Regions');
$regionsTablefield = new ManyManyComplexTableField(
$this,
'Regions',
'Region',
array('Title' => 'Title')
);
$regionsTablefield->setAddTitle('Regions');
$regionsTablefield->setPageSize('50');
$f->addFieldToTab('Root.Regions', $regionsTablefield);

return $f;
}

public function getFrontEndFields() {
$fields = $this->scaffoldFormFields(array(
'restrictFields' => array(
'FirstName',
'Surname',
'Website',
'Country',
'About',
'Locale',
'Designer_ProfilePicture',
'Designer_ProfilePictureExtra'
),
'fieldClasses' => array(
'Email' => 'EmailField',
)
)
);

return $fields;
}

function AddReferenceForm() {
$fields = new FieldSet(
new TextField('FirstName', 'First Name')
);
//$regions = DataObject::get("Region",""); // getting all pages for the groups
//$map = $regions->toDropDownMap();

/*
$fields = new FieldSet(
new TextField('FirstName', 'First Name'),
);
// Create actions


$validator = new RequiredFields('FirstName', 'Surname', 'Email', 'Country');
*/
$actions = new FieldSet(
new FormAction('doForm', 'Submit')
);

return new Form($this, 'AddReferenceForm', $fields, $actions);

}


function Designers() {
return DataObject::get("Designer", "", "FirstName ASC, Surname ASC", "", "");
}

public function onAfterWrite() {
// add to designersgroup
$designerGroup = DataObject::get_one('Group', "Title = 'designers'");
$designerGroup->Members()->add($this);



//notify
$email = new Email(
"noreply@conceptsverde.com",
"stijn@defunkt.nu, pepijn@klassetuinen.be",
"New Designer"
);
$body = "<p>A new designer has registered: " . $this->FirstName . " " . $this->Surname . ".</p>";
$email->body = $body;
$email->send();

return parent::onAfterWrite();
}


function StrippedDescription($Description) {
return $Description;
}





function ExtraReferences() {
//return DataObject::get("Reference", "DesignerID = $this->ID", "Created DESC", "", "1, 4");
return DataObject::get("Reference", "DesignerID = $this->ID", "Created DESC", "", "1, 300");
}

function CleanURL() {
if(strstr($this->URL, 'http://')) {
return $this->URL;
} else {
$cleanURL = 'http://' . $this->URL;
return $cleanURL;
}
}

public function canCreate($member = null) {
if(!$member) $member = Member::currentUser();
if(!$member) return false;
return (
Permission::checkMember($member, 'ADMIN')
);
}

public function canView($member = null) {
if(!$member) $member = Member::currentUser();
if(!$member) return false;
return (
Permission::checkMember($member, 'ADMIN')
|| ($member && $member->ID == $this->ID && $this->Approved == 1)
);
}

public function canEdit($member = null) {
if(!$member) $member = Member::currentUser();
if(!$member) return false;
return (
Permission::checkMember($member, 'ADMIN')
|| ($member && $member->ID == $this->ID && $this->Approved == 1)
);
}

}



class Designer_ProfilePicture extends Image {

function generateSmallThumb($gd) {
return $gd->resizeRatio(108, 168, true);
}

public function canEdit($member = null) {
return true;
}

public function canCreate($member = null) {
return true;
}
}

class Designer_ProfilePictureExtra extends Image {

function generateSmallThumbExtra($gd) {
return $gd->resizeRatio(108, 168, true);
}
}

?>


Post Comment