| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dynamic\FoxyStripe\Page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\GridField\GridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\ORM\DataList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\ORM\PaginatedList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\SiteConfig\SiteConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symbiote\GridFieldExtensions\GridFieldOrderableRows; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class ProductHolder extends \Page | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     private static $singular_name = 'Product Group'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     private static $plural_name = 'Product Groups'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     private static $description = 'Display a list of related products'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     private static $many_many = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         'Products' => ProductPage::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     private static $many_many_extraFields = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         'Products' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             'SortOrder' => 'Int', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         ), | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     /* | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     private static $allowed_children = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         ProductHolder::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ProductPage::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     private static $table_name = 'FS_ProductHolder'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @return FieldList | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |     public function getCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |         $fields = parent::getCMSFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         if (SiteConfig::current_site_config()->MultiGroup) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $config = GridFieldConfig_RelationEditor::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $config->addComponent(new GridFieldOrderableRows('SortOrder')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $config->addComponent(new GridFieldAddExistingSearchButton()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $fields->addFieldToTab( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 'Root.Products', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 GridField::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     'Products', | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     _t('ProductHolder.Products', 'Products'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     $this->Products(), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 | 1 |  |         return $fields; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @return DataList | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     public function Products() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 | 1 |  |         return $this->getManyManyComponents('Products')->sort('SortOrder'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * loadDescendantProductGroupIDListInto function. | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param mixed &$idList | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function loadDescendantProductGroupIDListInto(&$idList) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if ($children = $this->AllChildren()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             foreach ($children as $child) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 if (in_array($child->ID, $idList)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 if ($child instanceof self) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                     $idList[] = $child->ID; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |                     $child->loadDescendantProductGroupIDListInto($idList); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |      * ProductGroupIDs function. | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     public function ProductGroupIDs() | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         $holderIDs = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         $this->loadDescendantProductGroupIDListInto($holderIDs); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         return $holderIDs; | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * @param int $limit | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * @return PaginatedList | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function ProductList($limit = 10) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $config = SiteConfig::current_site_config(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         if ($config->ProductLimit > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             $limit = $config->ProductLimit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if ($config->MultiGroup) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $entries = $this->Products()->sort('SortOrder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             $filter = '"ParentID" = '.$this->ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             // Build a list of all IDs for ProductGroups that are children | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             $holderIDs = $this->ProductGroupIDs(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             // If no ProductHolders, no ProductPages. So return false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             if ($holderIDs) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 // Otherwise, do the actual query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 if ($filter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     $filter .= ' OR '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 $filter .= '"ParentID" IN ('.implode(',', $holderIDs).')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $order = '"SiteTree"."Title" ASC'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $entries = ProductPage::get()->where($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $list = new PaginatedList($entries, Controller::curr()->request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $list->setPageLength($limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 172 |  |  |         return $list; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |