Skip to content

Commit

Permalink
REFACTOR ProductObject (#37)
Browse files Browse the repository at this point in the history
Move product info to a dataobject. Product page has_one Product object. Allows products to be attached to multiple Product pages across categories.
  • Loading branch information
jsirish authored Oct 31, 2023
1 parent 8d6aadc commit 015058d
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 149 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"bummzack/sortablefile": "^2.0",
"dynamic/silverstripe-collection": "^3.0",
"silverstripe/recipe-cms": "^5",
"silvershop/silverstripe-hasonefield":"^4.0",
"symbiote/silverstripe-gridfieldextensions": "^4.0"
},
"require-dev": {
Expand Down
8 changes: 5 additions & 3 deletions src/Admin/ProductFileAdmin.php → src/Admin/ProductAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
namespace Dynamic\Products\Admin;

use Dynamic\Products\Model\Brochure;
use Dynamic\Products\Model\ProductObject;
use SilverStripe\Admin\ModelAdmin;

/**
* Class \Dynamic\Products\Admin\ProductFileAdmin
*
*/
class ProductFileAdmin extends ModelAdmin
class ProductAdmin extends ModelAdmin
{
/**
* @var array
*/
private static $managed_models = array(
ProductObject::class,
Brochure::class,
);

/**
* @var string
*/
private static $url_segment = 'product-files';
private static $url_segment = 'products';

/**
* @var string
*/
private static $menu_title = 'Product Files';
private static $menu_title = 'Products';
}
6 changes: 3 additions & 3 deletions src/Extension/ProductFileDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Dynamic\Products\Extension;

use Dynamic\Products\Model\ProductFile;
use Dynamic\Products\Model\ProductObject;
use Dynamic\Products\Page\Product;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Dev\Debug;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
Expand All @@ -19,15 +19,15 @@
* Class \Dynamic\Products\Extension\ProductFileDataExtension
*
* @property Brochure|ProductFileDataExtension $owner
* @method ManyManyList|Product[] Products()
* @method ManyManyList|ProductObject[] Products()
*/
class ProductFileDataExtension extends DataExtension
{
/**
* @var array
*/
private static $belongs_many_many = array(
'Products' => Product::class,
'Products' => ProductObject::class,
);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Brochure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Class \Dynamic\Products\Model\Brochure
*
* @method ManyManyList|Product[] Products()
* @method ManyManyList|ProductObject[] Products()
* @mixin ProductFileDataExtension
*/
class Brochure extends ProductFile
Expand Down
209 changes: 209 additions & 0 deletions src/Model/ProductObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?php

namespace Dynamic\Products\Model;

use SilverStripe\Assets\File;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use Dynamic\Products\Page\Product;
use SilverStripe\Forms\GridField\GridField;
use Bummzack\SortableFile\Forms\SortableUploadField;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;

/**
* Class \Dynamic\Products\Model\ProductObject
*
* @property string $Title
* @property string $Content
* @property string $SKU
* @method DataList|Product[] ProductPages()
* @method ManyManyList|File[] Images()
* @method ManyManyList|Brochure[] Brochures()
*/
class ProductObject extends DataObject
{
/**
* @var
*/
private $image;

/**
* @var
*/
private $has_images;

/**
*
* @var string
*/
private static $table_name = 'ProductObject';

/**
* @var string
*/
private static $singular_name = 'Product';

/**
* @var string
*/
private static $plural_name = 'Products';

/**
* @var array
*/
private static $db = [
'Title' => 'Varchar(255)',
'Content' => 'HTMLText',
'SKU' => 'Varchar(100)',
];

/**
* @var array
*/
private static $has_many = [
'ProductPages' => Product::class,
];

/**
* @var array
*/
private static $many_many = [
'Images' => File::class,
'Brochures' => Brochure::class,
];

/**
* @var array
*/
private static $many_many_extraFields = [
'Images' => [
'SortOrder' => 'Int',
],
'Brochures' => [
'SortOrder' => 'Int',
],
];

/**
*
*/
private static $owns = [
'Images',
];

/**
* The relation name was established before requests for videos.
* The relation has subsequently been updated from Image::class to File::class
* to allow for additional file types such as mp4
*
* @var array
*/
private static $allowed_images_extensions = [
'gif',
'jpeg',
'jpg',
'png',
'bmp',
'ico',
'mp4',
];

/**
*
* @return void
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->insertBefore(
'Content',
TextField::create('SKU', 'Product SKU')
);

// Images tab
$images = SortableUploadField::create('Images')
->setSortColumn('SortOrder')
->setIsMultiUpload(true)
->setAllowedExtensions($this->config()->get('allowed_images_extensions'))
->setFolderName('Uploads/Products/Images');

$fields->addFieldsToTab('Root.Images', [
$images,
]);

if ($this->ID) {
// Brochures
$brochures = $fields->dataFieldByName('Brochures');
$config = $brochures->getConfig();
$config->addComponents([
new GridFieldOrderableRows('SortOrder'),
new GridFieldAddExistingSearchButton(),
])
->removeComponentsByType([
GridFieldAddExistingAutocompleter::class,
]);
}
});

return parent::getCMSFields();
}

/**
* @return bool
*/
public function setImage()
{
if ($this->getHasImages()) {
$this->image = $this->Images()->sort('SortOrder')->first();
}
return $this;
}

/**
* @return mixed
*/
public function getImage()
{
if (!$this->image) {
$this->setImage();
}
return $this->image;
}

/**
* @return mixed
*/
public function getThumbnail()
{
if ($image = $this->getImage()) {
if ($thumb = Image::get()->byID($image->ID)) {
return $thumb->CMSThumbnail();
}
}

return false;
}

/**
* @return $this
*/
public function setHasImages()
{
$this->has_images = $this->Images()->exists();
return $this;
}

/**
* @return mixed
*/
public function getHasImages()
{
if (!$this->has_images) {
$this->setHasImages();
}
return $this->has_images;
}
}
Loading

0 comments on commit 015058d

Please sign in to comment.