Advertisement
Guest User

Untitled

a guest
Mar 19th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.24 KB | None | 0 0
  1. <?php
  2.  
  3. class Client extends DataObject {
  4.  
  5.     private static $db = array(
  6.         'Title' => 'Text',
  7.         'URLSegment' => 'Varchar(255)',
  8.         'Content' => 'HTMLText',
  9.         'Facebook' => 'Varchar(255)',
  10.         'Twitter' => 'Varchar(255)',
  11.         'LinkedIn' => 'Varchar(255)',
  12.         'Youtube' => 'Varchar(255)',
  13.         'GooglePlus' => 'Varchar(255)',
  14.         'SortOrder' => 'Int'
  15.     );
  16.  
  17.     private static $defaults = array(
  18.         'URLSegment' => 'new-client'
  19.     );
  20.  
  21.     private static $has_one = array(
  22.         'Logo' => 'Image',
  23.         'Banner' => 'Image'
  24.     );
  25.  
  26.     private static $many_many = array (
  27.         'Job' => 'Job'
  28.     );
  29.  
  30.     private static $belongs_many_many = array(
  31.         'Countries' => 'Country'
  32.     );
  33.  
  34.     private static $summary_fields = array(
  35.         'Title' => 'Title',
  36.         'URLSegment' => 'URLSegment'
  37.     );
  38.  
  39.     private static $indexes = array(
  40.         "URLSegment" => true
  41.     );
  42.  
  43.     private static $description = 'Client';
  44.  
  45.     private static $default_sort = 'SortOrder';
  46.  
  47.     public function getCMSFields() {
  48.  
  49.         $fields = parent::getCMSFields();
  50.  
  51.         $fields->removeByName('Countries');
  52.         $fields->removeByName('Job');
  53.  
  54.         $fields->addFieldToTab('Root.Main', new TextField('Title', 'Client Name'), 'Content');
  55.         $fields->addFieldToTab('Root.Main', new TextField('URLSegment', 'URL Segment'), 'Content');
  56.  
  57.         $countries = new ListboxField('Countries', 'Select countries', Country::get()->map()->toArray());
  58.         $countries->setMultiple(true);
  59.         $fields->addFieldToTab('Root.Main', $countries, 'Content');
  60.  
  61.         /* -----------------------------------------
  62.         * Imagery
  63.         ------------------------------------------*/
  64.  
  65.         $fields->addFieldToTab('Root.Images', new UploadField('Logo', 'Company Logo'));
  66.         $fields->addFieldToTab('Root.Images', new UploadField('Banner', 'Banner'));
  67.  
  68.         /* -----------------------------------------
  69.         * Social Media
  70.         ------------------------------------------*/
  71.  
  72.         $fields->addFieldToTab('Root.SocialMedia', new TextField('Facebook', 'Link to Facebook'));
  73.         $fields->addFieldToTab('Root.SocialMedia', new TextField('Twitter', 'Link to Twitter'));
  74.         $fields->addFieldToTab('Root.SocialMedia', new TextField('LinkedIn', 'Link to LinkedIn'));
  75.         $fields->addFieldToTab('Root.SocialMedia', new TextField('Youtube', 'Link to Youtube'));
  76.         $fields->addFieldToTab('Root.SocialMedia', new TextField('GooglePlus', 'Link to Google+'));
  77.  
  78.         return $fields;
  79.  
  80.     }
  81.  
  82.     public function onBeforeWrite(){
  83.         if((!$this->URLSegment || $this->URLSegment == 'new-client') && $this->Title != 'New Client'){
  84.             $filter = URLSegmentFilter::create();
  85.             $this->URLSegment = $filter->filter($this->Title);
  86.         }
  87.         else if($this->isChanged('URLSegment')){
  88.             $segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
  89.             $segment = preg_replace('/-+/','-',$segment);
  90.             if(!$segment) {
  91.                 $segment = "client-$this->ID";
  92.             }
  93.             $this->URLSegment = $segment;
  94.         }
  95.         $count = 2;
  96.         while($this->LookForExistingURLSegment($this->URLSegment)){
  97.             $this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
  98.             $count++;
  99.         }
  100.  
  101.         parent::onBeforeWrite();
  102.     }
  103.  
  104.     //Test whether the URLSegment exists already on another Client
  105.     public function LookForExistingURLSegment($URLSegment){
  106.         return (DataObject::get_one('Client', "URLSegment = '" . $URLSegment ."' AND ID != " . $this->ID));
  107.     }
  108.  
  109.     public function Link(){
  110.         //if we are on a category page return that
  111.         if(Director::get_current_page()->ClassName == 'ClientCountryPage'){
  112.             $Category = Director::get_current_page();
  113.         }
  114.         //Otherwise just grab the first category this product is in
  115.         else{
  116.             $Category = $this->Countries()->First()->ClientCountryPages()->First();
  117.         }
  118.         //Check we have a category then return the link
  119.         if($Category){
  120.             return $Category->absoluteLink() . 'show/' . $this->URLSegment;
  121.         }
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement