/**
  * @param array $options
  */
 public function save(array $options = [])
 {
     if (!$this->identifier || $this->identifier == '') {
         $this->identifier = \CoandaCMS\Coanda\Urls\Slugifier::convert($this->label);
     }
     parent::save($options);
 }
Esempio n. 2
0
 /**
  *
  */
 private function checkSetup()
 {
     if (!$this->name) {
         throw new \InvalidArgumentException('Please specify a name for this page type: ' . get_class($this));
     }
     if (!$this->identifier) {
         $this->identifier = str_replace('-', '_', Slugifier::convert($this->name));
     }
 }
Esempio n. 3
0
 /**
  * @param $data
  *
  * @throws ValidationException
  */
 private static function validateInput($data)
 {
     $invalid_fields = [];
     if (!isset($data['name']) || $data['name'] == '') {
         $invalid_fields['name'] = 'Please enter a name';
     }
     if (!isset($data['identifier']) || $data['identifier'] == '') {
         $data['identifier'] = Slugifier::convert($data['name']);
     }
     if (count($invalid_fields) > 0) {
         throw new ValidationException($invalid_fields);
     }
     return $data;
 }
 /**
  * @param $slug
  * @param $for
  * @param $for_id
  * @return bool
  * @throws \CoandaCMS\Coanda\Urls\Exceptions\UrlAlreadyExists
  * @throws \CoandaCMS\Coanda\Urls\Exceptions\InvalidSlug
  */
 public function canUse($slug, $for, $for_id = false)
 {
     $slug = trim($slug, '/');
     if (!$this->slugifier->validate($slug)) {
         throw new InvalidSlug('The slug is not valid');
     }
     // do we already have a record for this slug?
     $existing = $this->model->whereSlug($slug)->first();
     if ($existing) {
         if ($for_id) {
             // If the existing matches the type and id, then we can use it
             if ($existing->type == $for && $existing->type_id == $for_id) {
                 return true;
             }
         }
         // If the existing type is a url, then it can be overwritten (otherwise this would be 'reserved' forever)
         if ($existing->type == 'wildcard') {
             return true;
         }
         return false;
     }
     return true;
 }
 /**
  * @param $version
  * @return mixed|string
  */
 private function generateSlug($version)
 {
     $base_slug = $version->page->parent_slug;
     $page_id = $version->page->id;
     foreach ($version->attributes as $attribute) {
         if ($attribute->generates_slug) {
             $content = $attribute->content;
             if ($content && $content !== '') {
                 $new_slug = Slugifier::convert($content);
                 $tries = 50;
                 for ($i = 0; $i < $tries; $i++) {
                     if ($i > 0) {
                         $new_slug = $new_slug . '-' . $i;
                     }
                     if ($this->urls->canUse($base_slug . '/' . $new_slug, 'page', $page_id)) {
                         return $new_slug;
                     }
                 }
             }
         }
     }
     return '';
 }