Source of file SecondHandProduct.php
Size: 31,091 Bytes - Last Modified: 2021-12-23T10:41:14+00:00
/var/www/docs.ssmods.com/process/src/src/SecondHandProduct.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889 | <?php namespace Sunnysideup\EcommerceSecondHandProduct; use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\DateField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer; use SilverStripe\Forms\HeaderField; use SilverStripe\Forms\NumericField; use SilverStripe\Forms\ReadonlyField; use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DB; use SilverStripe\ORM\FieldType\DBBoolean; use SilverStripe\ORM\FieldType\DBDate; use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Security\PermissionProvider; use SilverStripe\Versioned\Versioned; use Sunnysideup\Ecommerce\Api\ClassHelpers; use Sunnysideup\Ecommerce\Config\EcommerceConfig; use Sunnysideup\Ecommerce\Forms\Fields\EcommerceCMSButtonField; use Sunnysideup\Ecommerce\Pages\Product; use Sunnysideup\EcommerceSecondHandProduct\Cms\SecondHandProductAdmin; use Sunnysideup\EcommerceSecondHandProduct\Model\SecondHandArchive; use Sunnysideup\GoogleAddressField\GoogleAddressField; use Sunnysideup\PermissionProvider\Api\PermissionProviderFactory; class SecondHandProduct extends Product implements PermissionProvider { /** * @var string */ protected static $treshold_sql_cache = ''; private static $can_be_root = false; /** * halt purchase for ... number of days * from the day of creation. * * @var int */ private static $embargo_number_of_days = 0; /** * halt purchase for ... number of days * from the day of creation. * * @var int */ private static $max_number_of_days_for_sale = 999; /** * Restrict GoogleAddressField to a specific Country * E.g. for New Zealand, $country_code = 'NZ'. * * @var string */ private static $country_code; /** * stadard SS declaration. * * @var array */ private static $table_name = 'SecondHandProduct'; private static $db = [ 'SoldPrice' => 'Currency', 'PurchasePrice' => 'Currency', 'ProductQuality' => 'Enum("1, 2, 3, 4, 5, 6, 7, 8, 9, 10","10")', 'IncludesBoxOrCase' => "Enum('No, Box, Case, Both','No')", 'SellingOnBehalf' => 'Boolean', 'OriginalManual' => 'Boolean', 'DateItemWasBought' => 'Date', 'DateItemWasSold' => 'Date', 'SerialNumber' => 'Varchar(50)', 'SellersName' => 'Varchar(50)', 'SellersPhone' => 'Varchar(30)', 'SellersEmail' => 'Varchar(255)', 'SellersAddress' => 'Varchar(255)', 'SellersAddress2' => 'Varchar(255)', 'SellersCity' => 'Varchar(100)', 'SellersPostalCode' => 'Varchar(50)', 'SellersRegionCode' => 'Varchar(100)', 'SellersCountry' => 'Varchar(50)', 'SellersIDType' => 'Enum(",Drivers Licence, Firearms Licence, Passport","")', 'SellersIDNumber' => 'Varchar(50)', 'SellersDateOfBirth' => 'Date', 'SellersIDExpiryDate' => 'Date', 'SellersIDPhotocopy' => 'Boolean', ]; private static $has_one = [ 'BasedOn' => SecondHandProduct::class, 'ArchivedBy' => Member::class, ]; private static $default_sort = [ 'ID' => 'DESC', ]; private static $defaults = [ 'ShowInMenus' => false, ]; private static $indexes = [ 'SerialNumber' => true, ]; private static $casting = [ 'SellersSummary' => 'Varchar', 'CreatedNice' => 'Varchar', ]; /** * Standard SS variable. */ private static $summary_fields = [ ]; private static $seller_summary_detail_fields = [ 'SellersName', 'SellersPhone', 'SellersEmail', 'SellersAddress', 'SellersAddress2', 'SellersCity', 'SellersPostalCode', 'SellersRegionCode', 'SellersCountry', 'SellersIDType', 'SellersIDNumber', 'SellersDateOfBirth', 'SellersIDExpiryDate', 'SellersIDPhotocopy', ]; private static $searchable_fields = [ 'FullName' => [ 'title' => 'Keyword', 'field' => TextField::class, ], 'Price' => [ 'title' => 'Price', 'field' => NumericField::class, ], 'InternalItemID' => [ 'title' => 'Internal Item ID', 'filter' => 'PartialMatchFilter', ], 'SellersName' => [ 'title' => 'Sellers Name', 'filter' => 'PartialMatchFilter', ], 'SellersPhone' => [ 'title' => 'Sellers Phone', 'filter' => 'PartialMatchFilter', ], 'SellersEmail' => [ 'title' => 'Sellers Email', 'filter' => 'PartialMatchFilter', ], 'AllowPurchase', 'PurchasePrice' => [ 'title' => 'Purchase Price', 'field' => NumericField::class, 'filter' => 'ExactMatchFilter', ], 'SerialNumber' => 'PartialMatchFilter', ]; private static $second_hand_admin_group_code = 'second-hand-managers'; private static $second_hand_admin_group_name = 'Second Hand Product Managers'; private static $second_hand_admin_role_title = 'Second Hand Product Management'; private static $second_hand_admin_permission_code = 'CMS_ACCESS_SECOND_HAND_PRODUCTS'; private static $second_hand_admin_permission_title = 'Second Hand Product Manager'; private static $second_hand_admin_permission_help = 'Manages the second product products'; private static $second_hand_admin_user_email = ''; private static $second_hand_admin_user_firstname = ''; private static $second_hand_admin_user_surname = ''; private static $second_hand_admin_user_password = ''; /** * standard SS declaration. * * @var string */ private static $icon = 'sunnysideup/ecommerce_second_hand_product: client/images/treeicons/SecondHandProduct-file.gif'; /** * Standard SS variable. */ private static $singular_name = 'SecondHand Product'; /** * Standard SS variable. */ private static $plural_name = 'SecondHand Products'; /** * standard SS declaration. * * @var string */ private static $description = 'This page displays a single secondhand product that can only be sold once'; public function SummaryFields() { return [ 'Image.CMSThumbnail' => 'Image', 'Title' => 'Title', 'InternalItemID' => 'Code', 'Price' => 'Price', 'AllowPurchaseNice' => 'For Sale', 'CreatedNice' => 'Entered', ]; } public function getSellerSummary() { $list = Config::inst()->get(SecondHandProduct::class, 'seller_summary_detail_fields'); $array = []; foreach ($list as $field) { if (trim($this->{$field})) { $array[] = $this->{$field}; } } return implode('; ', $array); } public function i18n_singular_name() { return self::$singular_name; } public function i18n_plural_name() { return self::$plural_name; } /** * standard SS method. * * @param null|mixed $member * @param mixed $context * * @return bool */ public function canCreate($member = null, $context = []) { return Permission::check( EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_code'), 'any', $member ); } /** * standard SS method. * * @param null|mixed $member * * @return bool */ public function canPublish($member = null) { return Permission::check( EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_code'), 'any', $member ); } /** * standard SS method. * * @param null|mixed $member * @param mixed $context * * @return bool */ public function canEdit($member = null, $context = []) { return Permission::check( EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_code'), 'any', $member ); } /** * standard SS method. * * @param null|mixed $member * * @return bool */ public function canDelete($member = null) { return Permission::check( EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_code'), 'any', $member ); } public function onBeforeDelete() { if ('Stage' !== Versioned::get_stage()) { //do nothing } else { //page is being deleted permanently so create archived version SecondHandArchive::create_from_page($this); } parent::onBeforeDelete(); } /** * stadard SS method. * * @return \SilverStripe\Forms\FieldList */ public function getCMSFields() { $fields = parent::getCMSFields(); //remove all unneccessary fields and tabs $fields->removeByName('AlsoShowHere'); $fields->removeByName('Tax'); $fields->removeByName('Links'); $fields->removeByName('Details'); $fields->removeByName('Images'); $fields->removeFieldFromTab('Root', 'Title'); $fields->removeFieldFromTab('Root', 'MenuTitle'); $fields->removeFieldFromTab('Root', 'ShortDescription'); $fields->removeFieldFromTab('Root', 'Content'); $fields->removeFieldFromTab('Root', 'Metadata'); $fields->removeFieldFromTab('Root', 'AddToCartLink'); $fields->dataFieldByName('URLSegment')->setReadonly(true); $fields->addFieldsToTab( 'Root.Main', [ $allowPurchaseField = CheckboxField::create( 'AllowPurchase', DBField::create_field( 'HTMLText', '<strong>Allow product to be purchased</strong>' ) ), $sellinOnBehalf = CheckboxField::create( 'SellingOnBehalf', DBField::create_field( 'HTMLText', '<strong>Selling on behalf</strong>' ) ), $featuredProductField = CheckboxField::create( 'FeaturedProduct', DBField::create_field( 'HTMLText', _t('Product.FEATURED', '<strong>Featured Product</strong>') ) ), TextField::create('Title', 'Product Title'), ] ); $secondhandProductCategories = SecondHandProductGroup::get(); if ($secondhandProductCategories->exists()) { $fields->addFieldToTab( 'Root.Main', $categoryField = DropdownField::create( 'ParentID', 'Product Category', $secondhandProductCategories->map() ) ); } $fields->addFieldsToTab( 'Root.Main', [ ReadonlyField::create('CanBeSold', 'For Sale', DBField::create_field(DBBoolean::class, $this->canPurchase())->Nice()), ReadonlyField::create('CreatedNice', 'First Entered', $this->getCreatedNice()), TextField::create('InternalItemID', 'Product Code'), $purchasePriceField = NumericField::create('PurchasePrice', 'Purchase Price')->setScale(2), $salePriceField = NumericField::create('Price', 'Sale Price')->setScale(2), $soldPriceField = NumericField::create('SoldPrice', 'Sold Price')->setScale(2), $serialNumberField = TextField::create('SerialNumber', 'Serial Number'), $productQualityField = DropdownField::create( 'ProductQuality', 'Product Condition/Quality', $this->dbObject('ProductQuality')->enumValues() ), $boxOrCaseField = DropdownField::create( 'IncludesBoxOrCase', 'Includes Box/Case', $this->dbObject('IncludesBoxOrCase')->enumValues() ), $originalManualField = CheckboxField::create('OriginalManual', 'Includes Original Manual'), $contentField = TextField::create('ShortDescription', 'Description'), $boughtDate = DateField::create('DateItemWasBought', 'Date this item was bought'), $soldDate = DateField::create('DateItemWasSold', 'Date this item was sold'), $mainImageField = UploadField::create('Image', 'Main Product Image'), $additionalImagesField = UploadField::create('AdditionalImages', 'More Images'), $metaFieldDesc = TextareaField::create('MetaDescription', 'Meta Description'), ] ); $soldDate->setDisabled(true); //set right titles and descriptions $featuredProductField->setDescription('If this box is ticked then this product will appear in the "Featured Products" box on the home page'); $allowPurchaseField->setDescription('This box must be ticked to allow a customer to purchase it'); $sellinOnBehalf->setDescription('This box must be ticked if this product is being sold on behalf'); $purchasePriceField->setDescription('Price paid for the product'); $salePriceField->setDescription('Selling price'); $soldPriceField->setDescription('The price that the product actually sold for'); $serialNumberField->setDescription('Enter the serial number of the product here'); $originalManualField->setDescription('Tick this box if the product includes the original manual, otherwise leave it empty'); $boxOrCaseField->setDescription('Does this product come with a box, case or both?'); $contentField->setDescription('Optional text only description, the maximum length of this description is 255 characters.'); $contentField->setMaxLength(255); $qualityFieldDescription = DBField::create_field( 'HTMLText', 'A <strong>Condition Rating Page</strong> has yet to be setup' ); $obj = EcommerceConfig::inst()->SecondHandExplanationPage(); if ($obj->exists()) { $qualityFieldDescription = 'An explanation of the ratings scale can be found by clicking this <a href="' . $obj->Link() . '">link</a>'; } $productQualityField->setDescription($qualityFieldDescription); $boughtDate->setDescription('Date Format (dd-mm-YYYY). Example: 3rd of May 1992 should be entered as 03-05-1992'); $mainImageField->setDescription( DBField::create_field( 'HTMLText', '<strong>Upload the main image for the product here.</strong><br> Recommended size: 810px wide x 418px high - but you can choose any width up to 810px, height must ALWAYS BE 418px. Should be provided to FTP data upload as productcode.jpg - e.g. 1003040.jpg. Images should be compressed up to 50%.' ) ); $additionalImagesField->setDescription( DBField::create_field( 'HTMLText', '<strong>Upload additional images here, you can upload as many as you want.</strong><br> Recommended size: 810px wide x 418px high - but you can choose any width up to 810px, height must ALWAYS BE 418px. Should be provided to FTP data upload as productcode.jpg - e.g. 1003040.jpg. Images should be compressed up to 50%.' ) ); //replace InternalItemID field with a read only field $fields->replaceField( 'InternalItemID', $fields->dataFieldByName('InternalItemID')->performReadonlyTransformation() ); $lastEditedItems = SecondHandProduct::get()->sort('Created', 'DESC')->limit(100); $lastItems = [ 0 => '--- not based on previous sale ---', ]; foreach ($lastEditedItems as $lastEditedItem) { $details = $lastEditedItem->getSellerSummary(); if ($details) { $lastItems[$lastEditedItem->ID] = $details; } } $fields->addFieldsToTab( 'Root.SellersDetails', [ HeaderField::create('SellersDetails', 'Enter the details of the person who the product was purchased from'), DropdownField::create( 'BasedOnID', 'Autocomplete from saved items', $lastItems ), TextField::create('SellersName', 'Name'), TextField::create('SellersPhone', 'Phone'), TextField::create('SellersEmail', 'Email Address'), DropdownField::create( 'SellersIDType', 'ID Type', $this->dbObject('SellersIDType')->enumValues() ), TextField::create('SellersIDNumber', 'ID Number'), DateField::create('SellersDateOfBirth', 'Date of Birth'), DateField::create('SellersIDExpiryDate', 'ID Expiry Date'), CheckboxField::create('SellersIDPhotocopy', 'ID Photocopy'), ] ); if (class_exists(GoogleAddressField::class)) { $mappingArray = $this->Config()->get('fields_to_google_geocode_conversion'); if (is_array($mappingArray) && count($mappingArray)) { $fields->addFieldToTab( 'Root.SellersDetails', $geocodingField = new GoogleAddressField( 'SellersAddressGeocodingField', _t('OrderAddress.FIND_ADDRESS', 'Find address'), Controller::curr()->getRequest()->getSession()->get('SellersAddressGeocodingFieldValue') ) ); $geocodingField->setFieldMap($mappingArray); $country_code = Config::inst()->get(SecondHandProduct::class, 'country_code'); if ($country_code) { $geocodingField->setRestrictToCountryCode($country_code); } //$geocodingField->setAlwaysShowFields(true); } } $fields->addFieldsToTab( 'Root.SellersDetails', [ TextField::create('SellersAddress', 'Address'), TextField::create('SellersAddress2', 'Suburb'), TextField::create('SellersCity', 'City/Town'), TextField::create('SellersPostalCode', 'Postal Code'), TextField::create('SellersRegionCode', 'Region Code'), TextField::create('SellersCountry', 'Country'), ] ); //add all fields to the main tab $fields->addFieldToTab( 'Root.SellersDetails', EcommerceCMSButtonField::create( 'PrintView', $this->getPrintLink(), 'Print Details', $newWindow = true ) ); if ($this->BasedOnID) { $list = Config::inst()->get(SecondHandProduct::class, 'seller_summary_detail_fields'); foreach ($list as $listField) { $fields->replaceField( $listField, ReadonlyField::create( $listField, $fields->dataFieldByName($listField)->Title() ) ); } $fields->removeByName('SellersAddressGeocodingField'); } $fields->addFieldToTab( 'Root.Categorisation', $categoriesTable = $this->getProductGroupsTableField() ); // If the product has been sold all fields should be disabled // Only the shop administrator is allowed to undo this. if ($this->HasBeenSold()) { $classURLSegment = ClassHelpers::sanitise_class_name(SecondHandProduct::class); $fields->insertAfter( 'AllowPurchase', EcommerceCMSButtonField::create( 'ArchiveButton', '/admin/secondhandproducts/' . $classURLSegment . '/archive/?productid=' . $this->ID, _t('SecondHandProduct.ARCHIVE_BUTTON', 'Archive Product') ) ); $fields = $fields->makeReadonly(); $fields->replaceField($categoriesTable->Name, $categoriesTable); $categoriesTable->setConfig(GridFieldConfig_RecordViewer::create()); } if ($this->canEdit()) { $fields->replaceField( 'AllowPurchase', CheckboxField::create( 'AllowPurchase', DBField::create_field( 'HTMLText', '<strong>Allow product to be purchased</strong>' ) ) ); $fields->replaceField('DateItemWasSold', DateField::create('DateItemWasSold', 'Date this item was sold')); } $fields->addFieldsToTab( 'Root.Status', [ ReadonlyField::create('isListedNice', 'Is listed', ($this->isListed() ? 'YES' : 'NO')), ReadonlyField::create('isUnderEmbargoNice', 'Is under embargo', ($this->isUnderEmbargo() ? 'YES' : 'NO')), ReadonlyField::create('HasBeenSoldNice', 'Has been sold', ($this->HasBeenSold() ? 'YES' : 'NO')), ] ); return $fields; } public function getPrintLink() { return $this->link('printview'); } public function CMSEditLink() { $sanitisedClassName = ClassHelpers::sanitise_class_name($this->ClassName); return Controller::join_links( singleton(SecondHandProductAdmin::class)->Link(), $sanitisedClassName, 'EditForm', 'field', $sanitisedClassName, 'item', $this->ID, 'edit' ); } public function getSettingsFields() { $fields = parent::getSettingsFields(); $fields->removeByName('ParentID'); return $fields; } public static function get_treshold_sql(): string { if ('' === self::$treshold_sql_cache) { $stage = self::get_stage(); $daysMin = intval(Config::inst()->get(SecondHandProduct::class, 'embargo_number_of_days')); $minThreshold = date( 'Y-m-d H:i:s', strtotime('-' . $daysMin . ' days', DBDatetime::now()->getTimestamp()) ); $daysMax = intval(Config::inst()->get(SecondHandProduct::class, 'max_number_of_days_for_sale')); $maxThreshold = date( 'Y-m-d H:i:s', strtotime('-' . $daysMax . ' days', DBDatetime::now()->getTimestamp()) ); self::$treshold_sql_cache = ' ( "SiteTree' . $stage . '"."Created" > \'' . $maxThreshold . '\' OR ( SecondHandProduct' . $stage . '.DateItemWasBought IS NOT NULL AND SecondHandProduct' . $stage . '.DateItemWasBought > \'' . $minThreshold . '\' AND SecondHandProduct' . $stage . '.DateItemWasBought < \'' . $maxThreshold . '\' ) ) '; } return self::$treshold_sql_cache; } public function canPurchase(Member $member = null, $checkPrice = true) { if ($this->HasBeenSold()) { return false; } if ($this->isUnderEmbargo()) { return false; } if (! $this->isListed()) { return false; } return parent::canPurchase($member, $checkPrice); } public function IsSecondHandProduct(): bool { return true; } public function isListed(): bool { return SecondHandProduct::get() ->where(self::get_treshold_sql()) ->byId($this->ID) ? true : false; } public function isUnderEmbargo(): bool { $embargoDays = Config::inst()->get(SecondHandProduct::class, 'embargo_number_of_days'); if (intval($embargoDays) > 0) { if ($this->DateItemWasBought) { $date = $this->DateItemWasBought; } else { $date = $this->Created; } $createdDate = strtotime($date); $daysOld = (time() - $createdDate) / (60 * 60 * 24); if ($daysOld <= $embargoDays) { return true; } } return false; } public function didNotSell(): bool { $embargoDays = Config::inst()->get(SecondHandProduct::class, 'embargo_number_of_days'); if (intval($embargoDays) > 0) { if ($this->DateItemWasBought) { $date = $this->DateItemWasBought; } else { $date = $this->Created; } $createdDate = strtotime($date); $daysOld = (time() - $createdDate) / (60 * 60 * 24); if ($daysOld <= $embargoDays) { return true; } } return false; } public function HasBeenSold(): bool { if (parent::HasBeenSold()) { return true; } if ($this->DateItemWasSold) { return true; } return false; } public function onBeforeWrite() { if ($this->BasedOnID) { $basedOn = $this->BasedOn(); if ($basedOn && $basedOn->exists()) { $list = Config::inst()->get(SecondHandProduct::class, 'seller_summary_detail_fields'); foreach ($list as $field) { $this->{$field} = $basedOn->{$field}; } } } //set the IternatlItemID if it doesn't already exist if (! $this->InternalItemID) { //todo - this may need improvement $this->InternalItemID = 'S-H-' . strtoupper(substr(md5(microtime()), rand(0, 26), 5)); } $this->URLSegment = $this->generateURLSegment($this->Title . '-' . $this->InternalItemID); if ($this->Title && strlen($this->MetaDescription) < 30) { $this->MetaDescription = 'Second Hand Product: ' . $this->Title; } // Save the date when the product was sold. if ($this->HasBeenSold()) { if (! $this->DateItemWasSold) { $this->DateItemWasSold = DBDatetime::now()->Rfc2822(); } } parent::onBeforeWrite(); } public function SecondHandProductQualityPercentage() { return $this->ProductQuality * 10; } public function InternalItemIDNice() { return $this->InternalItemID; } public function providePermissions() { $perms[EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_code')] = [ 'name' => EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_title'), 'category' => 'E-commerce', 'help' => EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_help'), 'sort' => 250, ]; return $perms; } public function requireDefaultRecords() { parent::requireDefaultRecords(); return PermissionProviderFactory::inst() ->setEmail(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_user_email')) ->setFirstName(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_user_firstname')) ->setSurname(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_user_surname')) ->setPassword(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_user_password')) ->setGroupName(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_group_name')) ->setCode(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_group_code')) ->setPermissionCode(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_code')) ->setRoleTitle(EcommerceConfig::get(SecondHandProduct::class, 'second_hand_admin_permission_title')) ->setPermissionArray( [ 'SITETREE_VIEW_ALL', 'CMS_ACCESS_SecondHandProductAdmin', ] ) ->CreateGroupAndMember() ; } public function onAferSubmit($order) { DB::query('Update \"Product\" SET AllowPurchase = 0 WHERE \"Product\".\"ID\" = ' . $this->ID); DB::query('Update \"Product_Live\" SET AllowPurchase = 0 WHERE \"Product_Live\".\"ID\" = ' . $this->ID); $this->writeToStage('Stage'); $this->publishRecursive(); } public function exportFields() { $fields = $this->summaryFields(); unset($fields['Image.CMSThumbnail']); return $fields; } public function populateDefaults() { if (! $this->DateItemWasBought) { $this->DateItemWasBought = DBDatetime::now()->Rfc2822(); } return parent::populateDefaults(); } public function getCreatedNice() { if ($this->DateItemWasBought) { $date = $this->DateItemWasBought; } else { $date = $this->Created; } return $date . ' = ' . DBField::create_field(DBDate::class, $date)->Ago(); } /** * By default we search for products that are allowed to be purchased only * standard SS method. * * @param null|mixed $_params * * @return \SilverStripe\Forms\FieldList */ public function scaffoldSearchFields($_params = null) { $fields = parent::scaffoldSearchFields($_params); $fields->fieldByName('AllowPurchase')->setValue(''); return $fields; } /** * @return string */ protected static function get_stage() { $stage = ''; if ('Live' === Versioned::get_stage()) { $stage = '_Live'; } return $stage; } } |