Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Client extends DataObject {
- private static $db = array(
- 'Title' => 'Text',
- 'URLSegment' => 'Varchar(255)',
- 'Content' => 'HTMLText',
- 'Facebook' => 'Varchar(255)',
- 'Twitter' => 'Varchar(255)',
- 'LinkedIn' => 'Varchar(255)',
- 'Youtube' => 'Varchar(255)',
- 'GooglePlus' => 'Varchar(255)',
- 'SortOrder' => 'Int'
- );
- private static $defaults = array(
- 'URLSegment' => 'new-client'
- );
- private static $has_one = array(
- 'Logo' => 'Image',
- 'Banner' => 'Image'
- );
- private static $many_many = array (
- 'Job' => 'Job'
- );
- private static $belongs_many_many = array(
- 'Countries' => 'Country'
- );
- private static $summary_fields = array(
- 'Title' => 'Title',
- 'URLSegment' => 'URLSegment'
- );
- private static $indexes = array(
- "URLSegment" => true
- );
- private static $description = 'Client';
- private static $default_sort = 'SortOrder';
- public function getCMSFields() {
- $fields = parent::getCMSFields();
- $fields->removeByName('Countries');
- $fields->removeByName('Job');
- $fields->addFieldToTab('Root.Main', new TextField('Title', 'Client Name'), 'Content');
- $fields->addFieldToTab('Root.Main', new TextField('URLSegment', 'URL Segment'), 'Content');
- $countries = new ListboxField('Countries', 'Select countries', Country::get()->map()->toArray());
- $countries->setMultiple(true);
- $fields->addFieldToTab('Root.Main', $countries, 'Content');
- /* -----------------------------------------
- * Imagery
- ------------------------------------------*/
- $fields->addFieldToTab('Root.Images', new UploadField('Logo', 'Company Logo'));
- $fields->addFieldToTab('Root.Images', new UploadField('Banner', 'Banner'));
- /* -----------------------------------------
- * Social Media
- ------------------------------------------*/
- $fields->addFieldToTab('Root.SocialMedia', new TextField('Facebook', 'Link to Facebook'));
- $fields->addFieldToTab('Root.SocialMedia', new TextField('Twitter', 'Link to Twitter'));
- $fields->addFieldToTab('Root.SocialMedia', new TextField('LinkedIn', 'Link to LinkedIn'));
- $fields->addFieldToTab('Root.SocialMedia', new TextField('Youtube', 'Link to Youtube'));
- $fields->addFieldToTab('Root.SocialMedia', new TextField('GooglePlus', 'Link to Google+'));
- return $fields;
- }
- public function onBeforeWrite(){
- if((!$this->URLSegment || $this->URLSegment == 'new-client') && $this->Title != 'New Client'){
- $filter = URLSegmentFilter::create();
- $this->URLSegment = $filter->filter($this->Title);
- }
- else if($this->isChanged('URLSegment')){
- $segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
- $segment = preg_replace('/-+/','-',$segment);
- if(!$segment) {
- $segment = "client-$this->ID";
- }
- $this->URLSegment = $segment;
- }
- $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 Client
- public function LookForExistingURLSegment($URLSegment){
- return (DataObject::get_one('Client', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
- }
- public function Link(){
- //if we are on a category page return that
- if(Director::get_current_page()->ClassName == 'ClientCountryPage'){
- $Category = Director::get_current_page();
- }
- //Otherwise just grab the first category this product is in
- else{
- $Category = $this->Countries()->First()->ClientCountryPages()->First();
- }
- //Check we have a category then return the link
- if($Category){
- return $Category->absoluteLink() . 'show/' . $this->URLSegment;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement