1010 */
1111defined ('_JEXEC ' ) or die ();
1212use Joomla \CMS \Factory ;
13+ use Joomla \CMS \Form \Form ;
14+ use Joomla \CMS \Language \Text ;
1315use Joomla \Database \DatabaseInterface ;
16+ use Joomla \Utilities \ArrayHelper ;
17+ use Phoca \PhocaCart \Container \Container ;
1418use Phoca \PhocaCart \ContentType \ContentTypeHelper ;
1519use Phoca \PhocaCart \I18n \I18nHelper ;
1620
@@ -19,7 +23,7 @@ class PhocacartRelated
1923 public static function storeRelatedItemsById ($ relatedString , $ productId )
2024 {
2125 if ((int )$ productId > 0 ) {
22- $ db =Factory:: getDBO ();
26+ $ db = Container:: getDbo ();
2327 $ query = ' DELETE '
2428 .' FROM #__phocacart_product_related '
2529 . ' WHERE product_a = ' . (int )$ productId ;
@@ -50,8 +54,7 @@ public static function storeRelatedItemsById($relatedString, $productId)
5054
5155 public static function copyRelatedItems (int $ sourceProductId , int $ destProductId ): void
5256 {
53- /** @var DatabaseInterface $db */
54- $ db = Factory::getContainer ()->get (DatabaseInterface::class);
57+ $ db = Container::getDbo ();
5558 $ query = 'INSERT INTO #__phocacart_product_related(related_type, product_a, product_b) '
5659 . 'SELECT related_type, ' . $ destProductId . ', product_b FROM #__phocacart_product_related WHERE product_a = ' . $ sourceProductId ;
5760 $ db ->setQuery ($ query );
@@ -64,8 +67,7 @@ public static function storeRelatedItems(int $productId, ?array $related): void
6467 return ;
6568 }
6669
67- /** @var DatabaseInterface $db */
68- $ db = Factory::getContainer ()->get (DatabaseInterface::class);
70+ $ db = Container::getDbo ();
6971
7072 $ query = 'DELETE FROM #__phocacart_product_related WHERE product_a = ' . $ productId ;
7173 $ db ->setQuery ($ query );
@@ -77,6 +79,10 @@ public static function storeRelatedItems(int $productId, ?array $related): void
7779 $ related = array_values ($ related );
7880 $ related = array_unique ($ related , SORT_REGULAR );
7981 foreach ($ related as $ index => $ relatedItem ) {
82+ if ($ relatedItem ['id ' ] == $ productId ) {
83+ continue ;
84+ }
85+
8086 $ values [] = '( ' . $ productId . ', ' . $ relatedItem ['id ' ] . ', ' . $ relatedItem ['related_type ' ] . ', ' . $ index . ') ' ;
8187 }
8288
@@ -87,6 +93,58 @@ public static function storeRelatedItems(int $productId, ?array $related): void
8793 }
8894 }
8995
96+ public static function addRelatedItems (int $ productId , int $ relatedType , array $ related ): void
97+ {
98+ if (!$ productId ) {
99+ return ;
100+ }
101+
102+ if (!$ related ) {
103+ return ;
104+ }
105+ ArrayHelper::toInteger ($ related );
106+ $ related = array_unique ($ related , SORT_REGULAR );
107+
108+ $ db = Container::getDbo ();
109+
110+ $ newRelated = $ db ->setQuery ('SELECT product_b AS id, related_type FROM #__phocacart_product_related WHERE product_a = ' . $ productId . ' ORDER BY ordering ' )->loadAssocList ('id ' );
111+ foreach ($ related as $ relatedItem ) {
112+ if ($ relatedItem == $ productId ) {
113+ continue ;
114+ }
115+
116+ if (array_key_exists ($ relatedItem , $ newRelated )) {
117+ $ newRelated [$ relatedItem ]['related_type ' ] = $ relatedType ;
118+ } else {
119+ $ newRelated [] = ['id ' => $ relatedItem , 'related_type ' => $ relatedType ];
120+ }
121+ }
122+
123+ self ::storeRelatedItems ($ productId , $ newRelated );
124+ }
125+
126+ public static function removeRelatedItems (int $ productId , int $ relatedType , array $ related ): void
127+ {
128+ if (!$ productId ) {
129+ return ;
130+ }
131+
132+ if (!$ related ) {
133+ return ;
134+ }
135+ ArrayHelper::toInteger ($ related );
136+
137+ $ db = Container::getDbo ();
138+
139+ $ related = array_unique ($ related , SORT_REGULAR );
140+
141+ $ query = 'DELETE FROM #__phocacart_product_related WHERE product_a = ' . $ productId .
142+ ' AND related_type = ' . $ relatedType .
143+ ' AND product_b IN ( ' . implode (', ' , $ related ) . ') ' ;
144+ $ db ->setQuery ($ query );
145+ $ db ->execute ();
146+ }
147+
90148 /*
91149 * Try to find the best menu link so we search for category which we are located
92150 * if we find the category, we use this, if not we use another if accessible, etc.
@@ -276,4 +334,47 @@ public static function correctProductId($productIdChange) {
276334 }
277335 return true ;
278336 }
337+
338+ public static function prepareBatchForm (Form $ form )
339+ {
340+ $ relatedTypes = ContentTypeHelper::getContentTypes (ContentTypeHelper::ProductRelated);
341+
342+ if (!$ relatedTypes ) {
343+ return true ;
344+ }
345+
346+ // Creating the dom
347+ $ xml = new \DOMDocument ('1.0 ' , 'UTF-8 ' );
348+ $ fieldsNode = $ xml ->appendChild (new \DOMElement ('form ' ))->appendChild (new \DOMElement ('fields ' ));
349+ $ fieldsNode ->setAttribute ('name ' , 'batch ' );
350+ $ fieldsNode = $ fieldsNode ->appendChild (new \DOMElement ('fields ' ));
351+ $ fieldsNode ->setAttribute ('name ' , 'related ' );
352+ $ fieldset = $ fieldsNode ->appendChild (new \DOMElement ('fieldset ' ));
353+ $ fieldset ->setAttribute ('name ' , 'params ' );
354+
355+ foreach ($ relatedTypes as $ relatedType ) {
356+ try {
357+ $ node = $ fieldset ->appendChild (new \DOMElement ('field ' ));
358+ $ node ->setAttribute ('name ' , 'add_ ' . $ relatedType ->id );
359+ $ node ->setAttribute ('type ' , 'PhocaCartItem ' );
360+ $ node ->setAttribute ('label ' , Text::sprintf ('COM_PHOCACART_BATCH_RELATED_ADD ' , Text::_ ($ relatedType ->title )));
361+ $ node ->setAttribute ('showon ' , '_related:1 ' );
362+ $ node ->setAttribute ('multiple ' , 'true ' );
363+
364+ $ node = $ fieldset ->appendChild (new \DOMElement ('field ' ));
365+ $ node ->setAttribute ('name ' , 'remove_ ' . $ relatedType ->id );
366+ $ node ->setAttribute ('type ' , 'PhocaCartItem ' );
367+ $ node ->setAttribute ('label ' , Text::sprintf ('COM_PHOCACART_BATCH_RELATED_REMOVE ' , Text::_ ($ relatedType ->title )));
368+ $ node ->setAttribute ('showon ' , '_related:1 ' );
369+ $ node ->setAttribute ('multiple ' , 'true ' );
370+ }
371+ catch (\Exception $ e ) {
372+ Factory::getApplication ()->enqueueMessage ($ e ->getMessage (), 'error ' );
373+ }
374+ }
375+
376+ $ form ->load ($ xml ->saveXML ());
377+
378+ return true ;
379+ }
279380}
0 commit comments