Source of file TableFilterSortServerSaver.php
Size: 3,616 Bytes - Last Modified: 2021-12-23T10:47:07+00:00
/var/www/docs.ssmods.com/process/src/src/Model/TableFilterSortServerSaver.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 | <?php namespace Sunnysideup\TableFilterSort\Model; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Convert; use SilverStripe\ORM\DataObject; class TableFilterSortServerSaver extends DataObject { private static $singular_name = 'Server Data'; private static $plural_name = 'Server Data'; private static $table_name = 'TableFilterSortServerSaver'; private static $db = [ 'URLSegment' => 'Varchar(50)', 'Title' => 'Varchar(50)', 'Author' => 'Varchar(50)', 'Description' => 'Varchar(200)', 'ParentPageID' => 'Varchar(200)', 'Data' => 'Text', ]; private static $many_many = [ 'Tags' => TableFilterSortTag::class, ]; private static $default_sort = [ 'ID' => 'DESC', ]; private static $required_fields = [ 'Title', 'ParentPageID', ]; private static $summary_fields = [ 'Created' => 'Created', 'Title' => 'Title', 'Author' => 'Title', 'Description' => 'Description', 'ParentPageID' => 'Show in', ]; private static $field_labels = [ 'Title' => 'Title', 'Author' => 'Author', 'Description' => 'Description', 'ParentPageID' => 'Show in', 'Tags' => 'Tag', ]; private static $indexes = [ 'Title_ParentPageIDUnique' => [ 'type' => 'unique', 'columns' => [ 'ParentPageID', 'Title', ], ], 'Title' => true, 'ParentPageID' => true, ]; public function i18n_singular_name() { return Config::inst()->get(self::class, 'singular_name'); } public function i18n_plural_name() { return Config::inst()->get(self::class, 'plural_name'); } /** * see README.md for usage ... * * @param string $title * @param string $parentPageID * * @return DataObject */ public static function find_or_create($title, $parentPageID) { $title = trim($title); $titleToLower = strtolower($title); $className = static::class; if (! $title || ! $parentPageID) { return $className::create(); } $obj = DataObject::get_one( $className, 'LOWER("Title") =\'' . Convert::raw2sql($titleToLower) . "' AND ParentPageID = '" . Convert::raw2sql($parentPageID) . "'", $cacheDataObjectGetOne = false ); if (! $obj) { $obj = $className::create(); } $obj->Title = $title; $obj->ParentPageID = $parentPageID; $obj->write(); return $obj; } /** * Event handler called before writing to the database. */ protected function onBeforeWrite() { parent::onBeforeWrite(); // if (! $this->Title){ // /*this Title is in string, does throw an error */ // // $this->Title = (int)$this.rand(0, getrandmax()); // } $iteration = 2; $originalName = $this->Title; while ($this->titleExists()) { $this->Title = $originalName . ' ' . $iteration; ++$iteration; } $this->URLSegment = urlencode( strtolower( str_replace(' ', '-', trim($this->Title)) ) ); } protected function titleExists() { return self::get() ->filter(['Title' => $this->Title, 'ParentPageID' => $this->ParentPageID]) ->exclude(['ID' => $this->ID]) ->exists() ; } } |