Source of file ShopifyCollection.php
Size: 6,495 Bytes - Last Modified: 2021-12-24T06:46:30+00:00
/var/www/docs.ssmods.com/process/src/src/Page/ShopifyCollection.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 | <?php namespace Dynamic\Shopify\Page; use Dynamic\Shopify\Model\ShopifyFile; use Dynamic\Shopify\Task\ShopifyImportTask; use SilverStripe\Admin\LeftAndMain; use SilverStripe\AssetAdmin\Forms\UploadField; use SilverStripe\Assets\Image; use SilverStripe\CMS\Model\RedirectorPage; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\VirtualPage; use SilverStripe\Control\Controller; use SilverStripe\Core\ClassInfo; use SilverStripe\Dev\Debug; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer; use SilverStripe\Forms\NumericField; use SilverStripe\Forms\ReadonlyField; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Security; /** * Class ShopifyCollection * @package Dynamic\Shopify\Page * * @property string ShopifyID * @property int ProductsCt * @property string SortOrder * @property bool CollectionActive * @property int ProductsPerPage * * @property int FileID * @method ShopifyFile File */ class ShopifyCollection extends \Page { /** * @var string */ private static $table_name = 'ShopifyCollection'; /** * @var string[] */ private static $db = [ 'ShopifyID' => 'Varchar', 'ProductsCt' => 'Int', 'SortOrder' => 'Varchar(20)', 'CollectionActive' => 'Boolean', 'ProductsPerPage' => 'Int', ]; /** * @var string[] */ private static $data_map = [ 'id' => 'ShopifyID', 'title' => 'Title', 'handle' => 'URLSegment', 'descriptionHtml' => 'Content', 'productsCount' => 'ProductsCt', 'createdAt' => 'Created', 'updatedAt' => 'LastEdited', 'sortOrder' => 'SortOrder', 'publishedOnCurrentPublication' => 'CollectionActive', ]; /** * @var string[] */ private static $has_one = [ 'File' => ShopifyFile::class ]; /** * @var bool[] */ private static $indexes = [ 'ShopifyID' => true, ]; /** * @var array */ private static $defaults = [ 'ProductsPerPage' => 12, ]; /** * @var string[] */ private static $summary_fields = [ 'File.CMSThumbnail' => 'Image', 'Title', 'ShopifyID', 'ProductsCt' => 'Products', ]; /** * @var string[] */ private static $allowed_children = [ ShopifyCollection::class, ShopifyProduct::class, VirtualPage::class, RedirectorPage::class, ]; /** * @return FieldList */ public function getCMSFields() { $self =& $this; $this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { $fields->dataFieldByName('Title') ->setReadonly(true); $fields->dataFieldByName('URLSegment') ->setReadonly(true); $fields->replaceField( 'Content', ReadonlyField::create('Content', 'Description') ); $fields->addFieldsToTab('Root.Details', [ ReadonlyField::create('ShopifyID'), ReadonlyField::create('Image') ->setTitle('Image') ->setValue($this->File()->getThumbnail(100, 100)), ReadonlyField::create('CollectionActive'), ReadonlyField::create('PublishedAt'), ]); $fields->addFieldsToTab( 'Root.Display', [ NumericField::create('ProductsPerPage') ->setTitle(_t(__CLASS__ . '.ProductsPerPage', 'Products Per Page')), ], 'Content' ); }); return parent::getCMSFields(); } /** * @inheritDoc */ public function getCMSActions() { $actions = parent::getCMSActions(); if (!$this->ShopifyID || !$this->canEdit()) { return $actions; } $controller = Controller::curr(); if ($controller instanceof LeftAndMain) { /** @var FormAction $action */ $action = FormAction::create('shopifyCollectionFetch', 'Re-fetch Shopify') ->addExtraClass('btn-primary font-icon-sync') ->setUseButtonTag(true); $actions->push($action); } return $actions; } /** * @return mixed */ public function getProductList() { $id = $this->CopyContentFromID ?: $this->ID; $categories = ShopifyCollection::get()->filter('ParentID', $id)->column('ID'); $categories[] = $id; $classes = array_merge( [ VirtualPage::class, RedirectorPage::class, ], ClassInfo::subclassesFor(ShopifyProduct::class) ); $products = SiteTree::get() ->filter('ClassName', $classes) ->filterAny([ 'ParentID' => $categories, ]); $this->extend('updateProductList', $products, $categories); $products = $products->filterByCallback(function ($page) { return $page->canView(Security::getCurrentUser()); }); return $products; } /** * @param $shopifyCollection * @return ProductCollection|ShopifyCollection * @throws \SilverStripe\ORM\ValidationException */ public static function findOrMakeFromShopifyData($shopifyCollection) { if (!$collection = self::getByShopifyID($shopifyCollection->id)) { $collection = self::create(); } $map = self::config()->get('data_map'); ShopifyImportTask::loop_map($map, $collection, $shopifyCollection); if ($collection->isChanged()) { $collection->write(); if ($collection->isPublished()) { $collection->publishRecursive(); } } return $collection; } /** * @param $shopifyId * @return ProductCollection */ public static function getByShopifyID($shopifyId) { return DataObject::get_one(self::class, ['ShopifyID' => $shopifyId]); } /** * @param $urlSegment * @return DataObject|null */ public static function getByURLSegment($urlSegment) { return DataObject::get_one(self::class, ['URLSegment' => $urlSegment]); } } |