Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions code/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* This class is only used if the CMS is installed, it ensures that the catalog
* URL is mapped via a page in the CMS
*/
class Catalog extends Page {
class Catalog extends Page
{
public static $icon = "commerce/images/product.png";

private static $db = array(
Expand All @@ -15,11 +16,13 @@ class Catalog extends Page {
'Category' => 'ProductCategory'
);

public function CommerceChildren() {
public function CommerceChildren()
{
return ProductCategory::get();
}

public function getCMSFields() {
public function getCMSFields()
{
$fields = parent::getCMSFields();

$display_types = array(
Expand All @@ -30,7 +33,7 @@ public function getCMSFields() {

$fields->addFieldToTab('Root.Main', DropDownField::create('Display', 'What will this catalog display?', $display_types), 'Content');

if($this->Display == 'Category') {
if ($this->Display == 'Category') {
$fields->addFieldToTab(
'Root.Main',
TreeDropdownField::create(
Expand All @@ -47,10 +50,11 @@ public function getCMSFields() {
return $fields;
}

public function requireDefaultRecords() {
public function requireDefaultRecords()
{
parent::requireDefaultRecords();

if(!Catalog::get()->first()) {
if (!Catalog::get()->first()) {
$catalog = new Catalog();
$catalog->Title = "Product Catalog";
$catalog->URLSegment = "catalog";
Expand All @@ -63,34 +67,40 @@ public function requireDefaultRecords() {
}
}

class Catalog_Controller extends Page_Controller {
class Catalog_Controller extends Page_Controller
{
public static $url_slug = 'catalog';

public static $allowed_actions = array(
'AddItemForm'
);

public function init() {
public function init()
{
parent::init();

Requirements::themedCSS("Commerce","commerce");
Requirements::themedCSS("Commerce", "commerce");
}

public function getRootCategories() {
public function getRootCategories()
{
return ProductCategory::get()->filter('ParentID', 0);
}

public function getAllProducts() {
public function getAllProducts()
{
return Product::get();
}

public function getCategoryChildren() {
public function getCategoryChildren()
{
$category = ProductCategory::get()->filter('ID', $this->CategoryID)->first();

if($category && $category->Children()->exists())
if ($category && $category->Children()->exists()) {
return $category->Children();
else
} else {
return $category->Products();
}
}
/*
public function index() {
Expand All @@ -100,35 +110,41 @@ public function index() {
return $this->renderWith(array('Categorys', 'Page'));
}*/

public function isProduct() {
if(Controller::curr()->request->Param('ProductID'))
public function isProduct()
{
if (Controller::curr()->request->Param('ProductID')) {
return true;
else
} else {
return false;
}
}

public function getCategory() {
public function getCategory()
{
return self::get_current_category();
}

public function getProduct() {
public function getProduct()
{
return self::get_current_product();
}

/**
* Create an array list of either current category children or products
*
*/
public function CategoriesOrProducts() {
public function CategoriesOrProducts()
{
$category = $this->getCategory();
$return = false;

if($category->Children()->exists())
if ($category->Children()->exists()) {
$return = $category->Children();
elseif($category->Products()->exists())
} elseif ($category->Products()->exists()) {
$return = $category->Products();
elseif($category->ID == 0)
} elseif ($category->ID == 0) {
$return = Product::get();
}

return $return;
}
Expand Down
78 changes: 47 additions & 31 deletions code/admin/CatalogueAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* @package Commerce
*/

class CatalogueAdmin extends ModelAdmin {
class CatalogueAdmin extends ModelAdmin
{
private static $url_segment = 'catalogue';

private static $menu_title = 'Catalogue';
Expand All @@ -24,7 +25,8 @@ class CatalogueAdmin extends ModelAdmin {

public $showImportForm = array('Product');

public function init() {
public function init()
{
parent::init();
}

Expand All @@ -33,32 +35,38 @@ public function init() {
*
* @return array
*/
public function getExportFields() {
if($this->modelClass == 'Product')
public function getExportFields()
{
if ($this->modelClass == 'Product') {
return Product::config()->export_fields;
else
} else {
return singleton($this->modelClass)->summaryFields();
}
}

public function getList() {
public function getList()
{
$list = parent::getList();

// Filter categories
if($this->modelClass == 'ProductCategory') {
if ($this->modelClass == 'ProductCategory') {
$parentID = $this->request->requestVar('ParentID');
if(!$parentID) $parentID = 0;
if (!$parentID) {
$parentID = 0;
}

$list = $list->filter('ParentID',$parentID);
$list = $list->filter('ParentID', $parentID);
}

return $list;
}

public function getEditForm($id = null, $fields = null) {
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);
$params = $this->request->requestVar('q');

if($this->modelClass == 'Product') {
if ($this->modelClass == 'Product') {
$gridField = $form->Fields()->fieldByName('Product');
$field_config = $gridField->getConfig();

Expand Down Expand Up @@ -101,7 +109,7 @@ public function getEditForm($id = null, $fields = null) {
);

// Update list of items for subsite (if used)
if(class_exists('Subsite')) {
if (class_exists('Subsite')) {
$list = $gridField
->getList()
->filter(array(
Expand All @@ -110,11 +118,10 @@ public function getEditForm($id = null, $fields = null) {

$gridField->setList($list);
}

}

// Alterations for Hiarachy on product cataloge
if($this->modelClass == 'ProductCategory') {
if ($this->modelClass == 'ProductCategory') {
$fields = $form->Fields();
$gridField = $fields->fieldByName('ProductCategory');

Expand Down Expand Up @@ -147,7 +154,7 @@ public function getEditForm($id = null, $fields = null) {
// Setup hierarchy view
$parentID = $this->request->requestVar('ParentID');

if($parentID){
if ($parentID) {
$field_config->addComponent(
GridFieldLevelup::create($parentID)
->setLinkSpec('?ParentID=%d')
Expand All @@ -166,7 +173,7 @@ public function getEditForm($id = null, $fields = null) {
'URLSegment' => 'URLSegement'
);

if(!$params) {
if (!$params) {
$fields = array_merge(array('listChildrenLink' => ''), $fields);
}

Expand All @@ -175,7 +182,7 @@ public function getEditForm($id = null, $fields = null) {

$controller = $this;
$columns->setFieldFormatting(array(
'listChildrenLink' => function($value, &$item) use($controller) {
'listChildrenLink' => function ($value, &$item) use ($controller) {
return sprintf(
'<a class="list-children-link" data-pjax-target="ListViewForm" href="%s?ParentID=%d">&#9658;</a>',
$controller->Link(),
Expand All @@ -185,7 +192,7 @@ public function getEditForm($id = null, $fields = null) {
));

// Update list of items for subsite (if used)
if(class_exists('Subsite')) {
if (class_exists('Subsite')) {
$list = $gridField
->getList()
->filter(array(
Expand All @@ -207,15 +214,18 @@ public function getEditForm($id = null, $fields = null) {
*
* @return string
*/
function SectionTitle() {
if($this->modelClass == 'ProductCategory')
public function SectionTitle()
{
if ($this->modelClass == 'ProductCategory') {
return 'Product Category';
else
} else {
return 'Product';
}
}
}

class ProductCategory_ItemRequest extends GridFieldDetailForm_ItemRequest {
class ProductCategory_ItemRequest extends GridFieldDetailForm_ItemRequest
{
private static $allowed_actions = array(
"ItemEditForm"
);
Expand All @@ -228,11 +238,13 @@ class ProductCategory_ItemRequest extends GridFieldDetailForm_ItemRequest {
* @param Controller $popupController
* @param string $popupFormName
*/
public function __construct($gridField, $component, $record, $popupController, $popupFormName) {
public function __construct($gridField, $component, $record, $popupController, $popupFormName)
{
parent::__construct($gridField, $component, $record, $popupController, $popupFormName);
}

public function Link($action = null) {
public function Link($action = null)
{
$parentParam = Controller::curr()->request->requestVar('ParentID');
$link = $parentParam ? parent::Link() . "?ParentID=$parentParam" : parent::Link();

Expand All @@ -247,17 +259,20 @@ public function Link($action = null) {
* @param boolean $unlinked
* @return ArrayData
*/
function Breadcrumbs($unlinked = false) {
if(!$this->popupController->hasMethod('Breadcrumbs')) return;
public function Breadcrumbs($unlinked = false)
{
if (!$this->popupController->hasMethod('Breadcrumbs')) {
return;
}

$items = $this->popupController->Breadcrumbs($unlinked);
if($this->record && $this->record->ID) {
if ($this->record && $this->record->ID) {
$ancestors = $this->record->getAncestors();
$ancestors = new ArrayList(array_reverse($ancestors->toArray()));
$ancestors->push($this->record);

// Push each ancestor to breadcrumbs
foreach($ancestors as $ancestor) {
foreach ($ancestors as $ancestor) {
$items->push(new ArrayData(array(
'Title' => $ancestor->Title,
'Link' => ($unlinked) ? false : $this->popupController->Link() . "?ParentID={$ancestor->ID}"
Expand All @@ -273,15 +288,16 @@ function Breadcrumbs($unlinked = false) {
return $items;
}

public function ItemEditForm() {
public function ItemEditForm()
{
$form = parent::ItemEditForm();

if($form) {
if ($form) {
// Update the default parent field
$parentParam = Controller::curr()->request->requestVar('ParentID');
$parent_field = $form->Fields()->dataFieldByName("ParentID");

if($parentParam && $parent_field) {
if ($parentParam && $parent_field) {
$parent_field->setValue($parentParam);
}

Expand Down
6 changes: 4 additions & 2 deletions code/admin/LocaliseAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*
* @package Commerce
*/
class LocaliseAdmin extends ModelAdmin {
class LocaliseAdmin extends ModelAdmin
{
private static $url_segment = 'localisation';
private static $menu_title = 'Localisation';
private static $menu_priority = -1;
Expand All @@ -14,7 +15,8 @@ class LocaliseAdmin extends ModelAdmin {
'ProductWeight',
);

public function getEditForm($id = null, $fields = null) {
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);

return $form;
Expand Down
Loading