Skip to content

Instantly share code, notes, and snippets.

@danganf
Last active November 30, 2018 18:06
Show Gist options
  • Save danganf/0d07aa1476610e9a57fac718594f027d to your computer and use it in GitHub Desktop.
Save danganf/0d07aa1476610e9a57fac718594f027d to your computer and use it in GitHub Desktop.
<?php
namespace MagaliApi\MyClass\Requests\GatewayCommerce;
use MagaliApi\MyClass\Contracts\GatewayCommerce;
class Category extends GatewayCommerce {
CONST TABLE = 'category';
CONST REL_CAT_PRODUCT_CAT = 'category_product_by_category_id';
CONST REL_VIEW_CAT_PRODUCT_AVAIBLE = 'view_category_product_avaible_by_category_id';
public function getAvaible($parentId = null){
$querieParent = 'parent_id' . ( empty( $parentId ) ? ' is null' : '='.$parentId );
$relNameCatProdOk = $this::REL_VIEW_CAT_PRODUCT_AVAIBLE;
$this->setStatusTrue()
->setFilter($querieParent, 'and')
->setFilter('(store_id='.$this->request->get('store_id').') or (is_default=1)', 'and')
->setRelatedFields($relNameCatProdOk, 'product_id')
->setServiceName('_table/'.$this::TABLE)
->setMethod('GET');
$return = parent::request();
$dados = [];
if( !empty( $return ) ){
$id = 0;
foreach ( $return AS $row ){
# so retorna categoria com produto associado
if( count( $row[$relNameCatProdOk] ) > 0 ) {
unset($row[$relNameCatProdOk]);
$dados[] = $row;
if (empty($row['parent_id'])) {
$dados[$id]['submenu'] = $this->getAvaible($row['id']);
}
$id++;
}
}
}
return $dados;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment