2012-02-29 14:47:37 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-01 04:37:50 +02:00
|
|
|
namespace SilverStripe\FrameworkTest\Model;
|
|
|
|
|
2018-10-18 05:16:54 +02:00
|
|
|
use SilverStripe\Assets\Image;
|
2020-07-21 05:10:00 +02:00
|
|
|
use SilverStripe\Forms\NumericField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
2018-10-18 05:16:54 +02:00
|
|
|
use SilverStripe\ORM\Connect\MySQLSchemaManager;
|
2016-07-01 04:37:50 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
2018-10-18 05:16:54 +02:00
|
|
|
use SilverStripe\ORM\Filters\ExactMatchFilter;
|
|
|
|
use SilverStripe\ORM\Filters\FulltextFilter;
|
2016-07-01 04:37:50 +02:00
|
|
|
|
|
|
|
|
2012-02-29 14:47:37 +01:00
|
|
|
/**
|
2020-07-21 05:10:00 +02:00
|
|
|
* @property string $Name
|
|
|
|
* @property string $Biography
|
|
|
|
* @property string $DateOfBirth
|
|
|
|
* @property string $Category
|
|
|
|
* @property int $CompanyID
|
|
|
|
* @property int $ProfileImageID
|
|
|
|
* @method Company Company()
|
|
|
|
* @method Image ProfileImage()
|
2012-02-29 14:47:37 +01:00
|
|
|
*/
|
2015-12-17 21:20:49 +01:00
|
|
|
class Employee extends DataObject
|
|
|
|
{
|
2016-07-01 04:37:50 +02:00
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar',
|
2018-10-18 05:16:54 +02:00
|
|
|
'Biography' => 'HTMLText',
|
|
|
|
'DateOfBirth' => 'Date',
|
|
|
|
'Category' => 'Enum("marketing,management,rnd,hr")'
|
2015-12-17 21:20:49 +01:00
|
|
|
);
|
2013-01-15 00:43:43 +01:00
|
|
|
|
2018-10-18 05:16:54 +02:00
|
|
|
private static $has_one = [
|
|
|
|
'Company' => Company::class,
|
|
|
|
'ProfileImage' => Image::class
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $belongs_many_many = [
|
|
|
|
'PastCompanies' => Company::class
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $indexes = [
|
|
|
|
'SearchFields' => [
|
|
|
|
'type' => 'fulltext',
|
|
|
|
'columns' => ['Name', 'Biography'],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $create_table_options = [
|
|
|
|
MySQLSchemaManager::ID => 'ENGINE=MyISAM'
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $summary_fields = [
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Biography',
|
|
|
|
'CompanyID',
|
|
|
|
'Company.Name' => 'Company',
|
|
|
|
'ColleagueNames' => 'Colleague Names',
|
|
|
|
'DateOfBirth',
|
|
|
|
'Category'
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $searchable_fields = [
|
|
|
|
'Name',
|
|
|
|
'Company' => [
|
|
|
|
'title' => 'Company exact match filter',
|
|
|
|
'filter' => ExactMatchFilter::class,
|
|
|
|
],
|
|
|
|
'Company.Name' => array(
|
|
|
|
'title' => 'Company Name starting with',
|
|
|
|
'field' => TextField::class,
|
|
|
|
'filter' => 'StartsWithFilter'
|
|
|
|
),
|
|
|
|
'CompanyID' => [
|
|
|
|
'title' => 'Company ID less than',
|
|
|
|
'field' => NumericField::class,
|
|
|
|
'filter' => 'LessThanFilter'
|
|
|
|
],
|
|
|
|
'Company.Employees.Name' => array('title' => 'Colleague'),
|
|
|
|
'DateOfBirth',
|
|
|
|
'Category'
|
|
|
|
];
|
2013-01-15 00:43:43 +01:00
|
|
|
|
2016-07-20 00:35:01 +02:00
|
|
|
private static $table_name = 'Employee';
|
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2016-08-03 04:12:53 +02:00
|
|
|
// Use basic scaffolder (no tabs)
|
|
|
|
$fields = $this->scaffoldFormFields();
|
|
|
|
$fields->push(new NumericField('ManyMany[YearStart]', 'Year started (3.1, many-many only)'));
|
|
|
|
$fields->push(new TextField('ManyMany[Role]', 'Role (3.1, many-many only)'));
|
2015-12-17 21:20:49 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
2016-07-01 04:37:50 +02:00
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
public function requireDefaultRecords()
|
|
|
|
{
|
|
|
|
parent::requireDefaultRecords();
|
2018-10-18 05:16:54 +02:00
|
|
|
$employeeSet = DataObject::get(Employee::class);
|
2021-05-27 05:25:38 +02:00
|
|
|
if ($employeeSet->exists() && !static::config()->get('regenerate_on_build')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
foreach ($employeeSet as $employee) {
|
|
|
|
$employee->delete();
|
|
|
|
}
|
2016-07-01 04:37:50 +02:00
|
|
|
|
2018-10-18 05:16:54 +02:00
|
|
|
// By explicitly seeding our random generator, we'll always get the same sequence
|
|
|
|
srand(5);
|
|
|
|
|
|
|
|
$companyIDs = Company::get()->column();
|
2022-04-13 00:31:07 +02:00
|
|
|
$companyCount = sizeof($companyIDs ?? []);
|
2018-10-18 05:16:54 +02:00
|
|
|
|
|
|
|
$words = $this->words();
|
2022-04-13 00:31:07 +02:00
|
|
|
$wordCount = sizeof($words ?? []);
|
2018-10-18 05:16:54 +02:00
|
|
|
$categories = ['marketing', 'management', 'rnd', 'hr'];
|
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
foreach ($this->data() as $employeeName) {
|
2018-10-18 05:16:54 +02:00
|
|
|
$employee = Employee::create([
|
|
|
|
'Name' => $employeeName,
|
|
|
|
'CompanyID' => $companyIDs[rand(0, $companyCount-1)],
|
2020-07-21 05:10:00 +02:00
|
|
|
'Biography' => implode(' ', [
|
2018-10-18 05:16:54 +02:00
|
|
|
$words[rand(0, $wordCount-1)],
|
|
|
|
$words[rand(0, $wordCount-1)],
|
|
|
|
$words[rand(0, $wordCount-1)],
|
|
|
|
$words[rand(0, $wordCount-1)],
|
2020-07-21 05:10:00 +02:00
|
|
|
]),
|
2018-10-18 05:16:54 +02:00
|
|
|
'DateOfBirth' => date("Y-m-d", rand(0, 500) * 24 * 60 * 60),
|
|
|
|
'Category' => $categories[rand(0, 3)]
|
|
|
|
]);
|
2015-12-17 21:20:49 +01:00
|
|
|
$employee->write();
|
|
|
|
}
|
|
|
|
DB::alteration_message("Added default records to Employee table", "created");
|
2018-10-18 05:16:54 +02:00
|
|
|
|
|
|
|
// Let's rescrambled our random generator
|
|
|
|
srand();
|
2015-12-17 21:20:49 +01:00
|
|
|
}
|
2013-05-29 17:26:11 +02:00
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
if (!$this->Name) {
|
|
|
|
$result->error('"Name" can\'t be blank');
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2016-07-01 04:37:50 +02:00
|
|
|
|
2015-12-17 21:20:49 +01:00
|
|
|
/**
|
|
|
|
* Contains test data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function data()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'Hayley', 'Octavius', 'Walker', 'Gary','Elton','Janna','Ursa','Lars','Moses','Lareina',
|
|
|
|
'Elmo','Cara','Shea','Duncan','Velma','Acton','Galena','Heidi','Troy','Elliott','Cara',
|
|
|
|
'Whitney','Summer','Olga','Tatum','Zeph','Jared','Hilda','Quinlan','Chaim','Xenos',
|
|
|
|
'Cara','Tatiana','Tyrone','Juliet','Chester','Hannah','Imani','Quinn','Ariel','Abel',
|
|
|
|
'Aretha','Courtney ','Shellie','Garrett','Camilla','Simon','Mohammad','Kirby','Rae',
|
|
|
|
'Xena','Noel','Omar','Shannon','Iola','Maia','Serina','Taylor','Alice','Lucy','Austin',
|
|
|
|
'Abel','Quinn','Yetta','Ulysses','Donovan','Castor','Emmanuel','Nero','Virginia',
|
|
|
|
'Gregory','Neville','Abel','Len','Knox','Gavin','Pascale','Hyatt','Alden','Emerald',
|
|
|
|
'Cherokee','Zeph','Adam','Uma','Serena','Isabelle','Kieran','Moses','Gay','Lavinia',
|
|
|
|
'Elvis','Illana','Lee','Ariana','Hilel','Juliet','Gage','Larissa','Richard','Allen'
|
|
|
|
);
|
|
|
|
}
|
2018-10-18 05:16:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Random words used to build the BIO
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function words()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
"aquarist", "macedonian", "kalian", "knew", "cloisterless", "separation", "husker",
|
|
|
|
"subhall", "relatable", "gyrofrequency", "mandating", "presentability", "catt", "folioing",
|
|
|
|
"theodore", "yavar", "mutular", "umbrage", "reinstated", "sert", "nonemulous", "nutwood",
|
|
|
|
"perikeiromene", "moi", "wordless", "downstage", "rsj", "nonanesthetized", "dunker",
|
|
|
|
"preloan", "lev", "marlow", "unpaneled", "overliberally", "monasticism", "philoctetes",
|
|
|
|
"amba", "fluffier", "volley", "unwasteful", "helpless", "gallico", "superexpectation",
|
|
|
|
"quartermaster", "extenuative", "marriage", "tiberius", "horn", "yankeedom", "chabazite",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDefaultSearchContext()
|
|
|
|
{
|
|
|
|
$context = parent::getDefaultSearchContext();
|
|
|
|
|
|
|
|
$context->addField(TextField::create('SearchFields', 'Full Text search'));
|
|
|
|
$context->addFilter(FulltextFilter::create('SearchFields'));
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getColleagueNames()
|
|
|
|
{
|
2020-07-21 05:10:00 +02:00
|
|
|
return implode(' ', $this->Company()->Employees()->column('Name'));
|
2018-10-18 05:16:54 +02:00
|
|
|
}
|
2012-02-29 14:47:37 +01:00
|
|
|
}
|