-
-
Save hailwood/23ba66a1db76902a9dfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Business | |
* | |
* @property String $Name | |
* @property String $Address1 | |
* @property String $Address2 | |
* @property String $City | |
* @property String $Postcode | |
* @property String $Website | |
* @property Boolean $SubscribedToNewsletter | |
* @property Boolean $ContactFirstName | |
* @property Boolean $ContactLastName | |
* @property Boolean $ContactTitle | |
* @property Boolean $ContactEmail | |
* @property Boolean $ContactPhone | |
* @property Boolean $ContactMobile | |
* | |
* @method HasManyList|Service[] Services() | |
* @method Category Category() | |
*/ | |
class Business extends DataObject { | |
private static $db = array( | |
/** Company Information */ | |
'Name' => 'Varchar', | |
'Address1' => 'Varchar', | |
'Address2' => 'Varchar', | |
'City' => 'Varchar', | |
'Postcode' => 'Varchar', | |
'Website' => 'Varchar', | |
'Approved' => 'Boolean', | |
/** Contact Information */ | |
'ContactFirstName' => 'Varchar', | |
'ContactLastName' => 'Varchar', | |
'ContactTitle' => 'Varchar', | |
'ContactEmail' => 'Varchar', | |
'ContactPhone' => 'Varchar', | |
'ContactMobile' => 'Varchar', | |
); | |
static $default_sort = "Name ASC"; | |
private static $has_one = array( | |
"Category" => "Category", | |
); | |
private static $many_many = array( | |
"Services" => "Service" | |
); | |
private static $field_labels = array( | |
'Name' => 'Company', | |
'ApprovedNice' => 'Approved', | |
'ContactNameNice' => 'Contact', | |
); | |
private static $summary_fields = array( | |
'Name', | |
'ContactNameNice', | |
'City', | |
'ApprovedNice' | |
); | |
private static $searchable_fields = array( | |
"Name", | |
"City", | |
"Approved" | |
); | |
public function canView($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_VIEW'), 'any', $member); | |
} | |
public function canEdit($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_EDIT'), 'any', $member); | |
} | |
public function canDelete($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_DELETE'), 'any', $member); | |
} | |
public function canCreate($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_CREATE'), 'any', $member); | |
} | |
public function populateDefaults() { | |
parent::populateDefaults(); | |
} | |
public function getCMSFields() { | |
$fields = parent::getCMSFields(); | |
$categoryField = $fields->fieldByName('Root.Main.CategoryID'); | |
$servicesSource = function ($val) { | |
/** @var Category $category */ | |
$category = Category::get_by_id('Category', $val); | |
return $category->Services()->map()->toArray(); | |
}; | |
$defaultServices = $this->Services()->column('ID'); | |
/** @var DependentListboxField $servicesField */ | |
$servicesField = DependentListboxField::create('AssignedServices', 'Select Services', $servicesSource); | |
$servicesField->setMultiple(true); | |
$servicesField->setDefaultItems($defaultServices); | |
$servicesField->setDepends($fields->fieldByName('Root.Main.CategoryID')); | |
$fields->removeFieldFromTab('Root', 'Services'); | |
$fields->addFieldToTab('Root.Main', $categoryField, 'Address1'); | |
$fields->addFieldToTab('Root.Main', $servicesField, 'Address1'); | |
/** @var TextField $firstNameField */ | |
$firstNameField = $fields->fieldByName('Root.Main.ContactFirstName'); | |
$firstNameField->setTitle('First Name'); | |
/** @var TextField $lastNameField */ | |
$lastNameField = $fields->fieldByName('Root.Main.ContactLastName'); | |
$lastNameField->setTitle('Last Name'); | |
/** @var TextField $titleField */ | |
$titleField = $fields->fieldByName('Root.Main.ContactTitle'); | |
$titleField->setTitle('Title/Position'); | |
/** @var TextField $emailField */ | |
$emailField = $fields->fieldByName('Root.Main.ContactEmail'); | |
$emailField->setTitle('Email Address'); | |
/** @var TextField $phoneField */ | |
$phoneField = $fields->fieldByName('Root.Main.ContactPhone'); | |
$phoneField->setTitle('Primary Phone'); | |
/** @var TextField $mobileField */ | |
$mobileField = $fields->fieldByName('Root.Main.ContactMobile'); | |
$mobileField->setTitle('Mobile Phone'); | |
$fields->addFieldsToTab('Root.CompanyContact', array( | |
$firstNameField, | |
$lastNameField, | |
$titleField, | |
$emailField, | |
$phoneField, | |
$mobileField | |
)); | |
$servicesRequiringApproval = $this->Services()->exclude("Approved", 1); | |
if($servicesRequiringApproval->count()){ | |
$tab = new Tab('ServicesRequiringApproval'); | |
$tab->setTitle('Services Requiring Approval ('. $servicesRequiringApproval->count().')'); | |
$fields->addFieldToTab('Root', $tab); | |
$gridFieldConfig = GridFieldConfig::create()->addComponents( | |
new GridFieldToolbarHeader(), | |
new GridFieldSortableHeader(), | |
new GridFieldDataColumns(), | |
new GridFieldPaginator(10), | |
new GridFieldEditButton(), | |
new GridFieldDeleteAction(), | |
new GridFieldDetailForm() | |
); | |
$gridField = new GridField("ServicesRequiringApproval", null, $servicesRequiringApproval, $gridFieldConfig); | |
$fields->addFieldToTab('Root.ServicesRequiringApproval', $gridField); | |
} | |
return $fields; | |
} | |
public function onBeforeWrite() { | |
$changedFields = $this->getChangedFields(); | |
//AssignedFields is a pseudofield for our ListField | |
if (isset($changedFields['AssignedServices'])) { | |
$servicesComponent = $this->getManyManyComponents('Services'); | |
$currentServices = $this->Services()->column(); | |
$newServices = explode(',', $this->AssignedServices); | |
$toRemove = array_filter(array_diff($currentServices, $newServices)); | |
$toAdd = array_filter(array_diff($newServices, $currentServices)); | |
if (!empty($toRemove)) { | |
$servicesComponent->removeMany($toRemove); | |
} | |
if (!empty($toAdd)) { | |
$servicesComponent->addMany($toAdd); | |
} | |
} | |
parent::onBeforeWrite(); | |
} | |
public function onAfterWrite() { | |
} | |
public function getCMSValidator() { | |
// Retrieve the default validator from the parent page type | |
return new RequiredFields( | |
'Name', | |
'Address1', | |
'City', | |
'Postcode', | |
'ContactFirstName', | |
'ContactLastName', | |
'ContactTitle', | |
'ContactEmail', | |
'ContactPhone' | |
); | |
} | |
public function ContactNameNice() { | |
return $this->ContactFirstName . ' ' . $this->ContactLastName; | |
} | |
public function ApprovedNice() { | |
return $this->Approved ? 'Yes' : 'No'; | |
} | |
public function CreateServicesRelation($title) { | |
/** @var Service $model */ | |
$model = Injector::inst()->create(self::$many_many['Services']); | |
$existingModel = Service::get() | |
->where("Service.CategoryID = '" . Convert::raw2sql($this->CategoryID) . "'") | |
->find('Title', $title); | |
if ($existingModel) { | |
return $existingModel; | |
} | |
if (!$model->canCreate()) { | |
return false; | |
} | |
$model->Title = $title; | |
$model->CategoryID = $this->CategoryID; | |
$model->Approved = false; | |
$model->write(); | |
return $model; | |
} | |
public function OnApiCreate($payload = array()) { | |
if(array_key_exists('SubscribedToNewsletter', $payload) && $payload['SubscribedToNewsletter']){ | |
SS_Log::log("Subscribing " . $this->ContactEmail, SS_Log::DEBUG); | |
try{ | |
$this->subscribeToMailchimp($this->ContactEmail, $this->ContactFirstName, $this->ContactLastName); | |
} catch(Exception $e){ | |
SS_Log::log("Subscription Exception " . $e->getMessage(), SS_Log::DEBUG); | |
//silent fail usually email is already subscribed | |
} | |
} elseif(!array_key_exists('SubscribedToNewsletter', $payload)) { | |
SS_Log::log("Not Subscribing " . $this->ContactEmail.': Key not found', SS_Log::DEBUG); | |
} else { | |
SS_Log::log("Not Subscribing " . $this->ContactEmail.': Value not true', SS_Log::DEBUG); | |
} | |
$toEmail = SiteConfig::current_site_config()->NotificationEmail; | |
$email = new StyledHtmlEmail('alerts@***.co.nz', $toEmail, $this->Name . ' is awaiting approval', null, null, null); | |
$email->setTemplate('NewBusinessEmail'); | |
// You can call this multiple times or bundle everything into an array, including DataSetObjects | |
$email->populateTemplate(array( | |
'Business' => $this, | |
'AdminLink' => Director::absoluteURL('admin/directory/Business/EditForm/field/Business/item/' . $this->ID) | |
)); | |
return $email->send(); | |
} | |
protected function subscribeToMailchimp($email, $firstName, $lastName) { | |
$siteConfig = SiteConfig::current_site_config(); | |
$mc = new Mailchimp($siteConfig->MailchimpApiKey); | |
/** @noinspection PhpParamsInspection */ | |
$mc->lists->subscribe( | |
$siteConfig->MailchimpListID, | |
array('email' => $email), | |
array('FNAME' => $firstName, 'LNAME' => $lastName), | |
'html', | |
false, | |
false, | |
false, | |
false | |
); | |
} | |
protected function unsubscribeFromMailchimp($email) { | |
$siteConfig = SiteConfig::current_site_config(); | |
$mc = new Mailchimp($siteConfig->MailchimpApiKey); | |
$mc->lists->unsubscribe( | |
$siteConfig->MailchimpListID, | |
array('email' => $email), | |
false, | |
false, | |
false | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Category | |
* | |
* @property String $Name | |
* | |
* @method HasManyList|Business[] Businesses() | |
* @method HasManyList|Service[] Services() | |
* | |
*/ | |
class Category extends DataObject { | |
static $default_sort = "Title ASC"; | |
private static $db = array( | |
'Title' => 'Varchar', | |
); | |
private static $has_many = array( | |
"Businesses" => "Business", | |
"Services" => "Service" | |
); | |
private static $field_labels = array( | |
'ServicesCount' => "Services", | |
'BusinessesCount' => "Businesses" | |
); | |
private static $summary_fields = array( | |
'Title', | |
'ServicesCount', | |
'BusinessesCount' | |
); | |
public function canView($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain','RESTfulAPI_VIEW'), 'any', $member); | |
} | |
public function canEdit($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain','RESTfulAPI_EDIT'), 'any', $member); | |
} | |
public function canDelete($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain','RESTfulAPI_DELETE'), 'any', $member); | |
} | |
public function canCreate($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain','RESTfulAPI_CREATE'), 'any', $member); | |
} | |
public function populateDefaults() { | |
parent::populateDefaults(); | |
} | |
public function getCMSFields() { | |
$fields = parent::getCMSFields(); | |
return $fields; | |
} | |
public function onBeforeWrite() { | |
parent::onBeforeWrite(); | |
} | |
public function getCMSValidator() { | |
// Retrieve the default validator from the parent page type | |
return new RequiredFields( | |
'Title' | |
); | |
} | |
public function ServicesCount(){ | |
return $this->Services()->count(); | |
} | |
public function BusinessesCount(){ | |
return $this->Businesses()->count(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "a-ss-site", | |
"description": "A SilverStripe site", | |
"require": { | |
"php": ">=5.3.2", | |
"silverstripe/cms": "~3.1", | |
"silverstripe/framework": "~3.1", | |
"silverstripe/html5": "~1.0", | |
"silverstripe/userforms": "~2.0", | |
"silverstripe/googlesitemaps": "~1.2", | |
"colymba/silverstripe-restfulapi": "~2.0", | |
"unclecheese/betterbuttons": "dev-master", | |
"sheadawson/quickaddnew": "~1.1", | |
"raveren/kint": "v0.9.1", | |
"pelago/emogrifier": "dev-master", | |
"markguinn/silverstripe-email-helpers": "dev-master", | |
"webfox/silverstripe-dependentdropdownfield": "dev-master", | |
"mailchimp/mailchimp": "dev-master" | |
}, | |
"config": { | |
"process-timeout": 600 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DirectoryAdmin extends ModelAdmin { | |
private static $menu_icon = 'mysite/images/book.png'; | |
private static $managed_models = array('Business', 'Category', 'Service'); // Can manage multiple models | |
private static $url_segment = 'directory'; // Linked as /admin/directory/ | |
private static $menu_title = 'Business Directory'; | |
public $showImportForm = false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Service | |
* | |
* @property String $Name | |
* @property Boolean $Approved | |
*/ | |
class Service extends DataObject { | |
static $default_sort = "Title ASC"; | |
private static $db = array( | |
'Title' => 'Varchar', | |
'Approved' => "Boolean" | |
); | |
private static $has_one = array( | |
'Category' => 'Category' | |
); | |
private static $belongs_many_many = array( | |
"Businesses" => "Business", | |
); | |
private static $field_labels = array( | |
'ApprovedNice' => 'Approved', | |
'Category.Title' => 'Category', | |
'CategoryID' => 'Category' | |
); | |
private static $summary_fields = array( | |
'Title', | |
'Category.Title', | |
'ApprovedNice' | |
); | |
private static $searchable_fields = array( | |
"Title", | |
"CategoryID", | |
"Approved" | |
); | |
public function canView($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_VIEW'), 'any', $member); | |
} | |
public function canEdit($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_EDIT'), 'any', $member); | |
} | |
public function canDelete($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_DELETE'), 'any', $member); | |
} | |
public function canCreate($member = null) { | |
return Permission::check(array('CMS_ACCESS_CMSMain', 'RESTfulAPI_CREATE'), 'any', $member); | |
} | |
public function populateDefaults() { | |
parent::populateDefaults(); | |
$this->Approved = true; | |
} | |
public function getCMSFields() { | |
$fields = parent::getCMSFields(); | |
$categorySource = function () { | |
return Category::get()->map()->toArray(); | |
}; | |
/** @var DropdownField $categoryField */ | |
$categoryField = $fields->fieldByName('Root.Main.CategoryID'); | |
$categoryField->setSource($categorySource()); | |
$categoryField->useAddNew('Category', $categorySource); | |
return $fields; | |
} | |
public function onBeforeWrite() { | |
parent::onBeforeWrite(); | |
} | |
public function getCMSValidator() { | |
return new RequiredFields( | |
'Title' | |
); | |
} | |
public function ApprovedNice() { | |
return $this->Approved ? 'Yes' : 'No'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment