diff --git a/src/app/basket/controllers/BasketController.php b/src/app/basket/controllers/BasketController.php new file mode 100644 index 0000000000000000000000000000000000000000..baa597ccac6524d7ba2ecb2f6b264e665707988f --- /dev/null +++ b/src/app/basket/controllers/BasketController.php @@ -0,0 +1,402 @@ +<?php + +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. +* +*/ + +/** +* @brief Basket Controller +* @author dev@maarch.org +*/ + +namespace Basket\controllers; + +use Basket\models\BasketModel; +use Core\Models\ActionModel; +use Core\Models\GroupModel; +use Core\Models\ServiceModel; +use Core\Models\ValidatorModel; +use Respect\Validation\Validator; +use Slim\Http\Request; +use Slim\Http\Response; + +class BasketController +{ + public function get(Request $request, Response $response) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + return $response->withJson(['baskets' => BasketModel::get()]); + } + + public function getById(Request $request, Response $response, $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id']]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + return $response->withJson(['basket' => $basket]); + } + + public function create(Request $request, Response $response) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $data = $request->getParams(); + + $check = Validator::stringType()::notEmpty()->validate($data['id']); + $check = $check && Validator::stringType()::notEmpty()->validate($data['name']); + $check = $check && Validator::stringType()::notEmpty()->validate($data['description']); + $check = $check && Validator::stringType()::notEmpty()->validate($data['clause']); + if (!$check) { + return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); + } + + $existingBasket = BasketModel::getById(['id' => $data['id'], 'select' => ['1']]); + if (!empty($existingBasket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket already exists']); + } + + $data['isVisible'] = empty($data['isSearchBasket']) ? 'Y' : 'N'; + $data['isFolderBasket'] = empty($data['isFolderBasket']) ? 'N' : 'Y'; + $data['flagNotif'] = empty($data['flagNotif']) ? 'N' : 'Y'; + BasketModel::create($data); + + return $response->withJson(['basket' => $data['id']]); + } + + public function update(Request $request, Response $response, $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id'], 'select' => [1]]); + + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + $data = $request->getParams(); + + $check = Validator::stringType()::notEmpty()->validate($data['name']); + $check = $check && Validator::stringType()::notEmpty()->validate($data['description']); + $check = $check && Validator::stringType()::notEmpty()->validate($data['clause']); + if (!$check) { + return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); + } + + $data['isVisible'] = empty($data['isSearchBasket']) ? 'Y' : 'N'; + $data['isFolderBasket'] = empty($data['isFolderBasket']) ? 'N' : 'Y'; + $data['flagNotif'] = empty($data['flagNotif']) ? 'N' : 'Y'; + $data['id'] = $aArgs['id']; + BasketModel::update($data); + + return $response->withJson(['success' => 'success']); + } + + public function delete(Request $request, Response $response, $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id'], 'select' => [1]]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket does not exist']); + } + + BasketModel::delete(['id' => $aArgs['id']]); + + return $response->withJson(['baskets' => BasketModel::get()]); + } + + public function getGroups(Request $request, Response $response, $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id'], 'select' => [1]]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + $groups = BasketModel::getGroups(['id' => $aArgs['id']]); + + foreach ($groups as $key => $group) { + $actions = BasketModel::getActionsForGroupById([ + 'id' => $aArgs['id'], + 'groupId' => $group['group_id'], + 'select' => ['id_action', 'where_clause', 'used_in_basketlist', 'used_in_action_page', 'default_action_list'] + ]); + $actionIds = []; + foreach ($actions as $action) { + $actionIds[] = $action['id_action']; + } + $statuses = []; + $redirects = []; + if (!empty($actionIds)) { + $statuses = BasketModel::getGroupBasketStatusesIn([ + 'id' => $aArgs['id'], + 'groupId' => $group['group_id'], + 'actionIds' => $actionIds, + 'select' => ['status_id', 'action_id'] + ]); + $redirects = BasketModel::getGroupBasketRedirectIn([ + 'id' => $aArgs['id'], + 'groupId' => $group['group_id'], + 'actionIds' => $actionIds, + 'select' => ['entity_id', 'action_id', 'keyword', 'redirect_mode'] + ]); + } + foreach ($actions as $actionKey => $action) { + $actions[$actionKey]['statuses'] = []; + $actions[$actionKey]['redirects'] = []; + foreach ($statuses as $status) { + if ($status['action_id'] == $action['id_action']) { + $actions[$actionKey]['statuses'][] = $status['status_id']; + } + } + foreach ($redirects as $redirect) { + if ($redirect['action_id'] == $action['id_action']) { + $actions[$actionKey]['redirects'][] = $redirect; + } + } + } + + $groups[$key]['groupActions'] = $actions; + } + + return $response->withJson(['groups' => $groups]); + } + + public function createGroup(Request $request, Response $response, array $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id'], 'select' => [1]]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + $data = $request->getParams(); + + $check = Validator::stringType()::notEmpty()->validate($data['group_id']); + $check = $check && Validator::stringType()::notEmpty()->validate($data['result_page']); + $check = $check && Validator::arrayType()::notEmpty()->validate($data['groupActions']); + if (!$check) { + return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); + } + $data['groupActions'] = BasketController::checkGroupActions(['groupActions' => $data['groupActions']]); + if (!empty($data['groupActions']['errors'])) { + return $response->withStatus(400)->withJson(['errors' => $data['groupActions']['errors']]); + } + + if (BasketModel::hasGroup(['id' => $aArgs['id'], 'groupId' => $data['group_id']])) { + return $response->withStatus(400)->withJson(['errors' => 'Group already exist for this basket']); + } + + BasketModel::createGroup(['id' => $aArgs['id'], 'groupId' => $data['group_id'], 'resultPage' => $data['result_page']]); + foreach ($data['groupActions'] as $groupAction) { + BasketModel::createGroupAction([ + 'id' => $aArgs['id'], + 'groupId' => $data['group_id'], + 'actionId' => $groupAction['id_action'], + 'whereClause' => $groupAction['where_clause'], + 'usedInBasketlist' => $groupAction['used_in_basketlist'], + 'usedInActionPage' => $groupAction['used_in_action_page'], + 'defaultActionList' => $groupAction['default_action_list'] + ]); + + if (!empty($groupAction['statuses'])) { + foreach ($groupAction['statuses'] as $status) { + BasketModel::createGroupActionStatus([ + 'id' => $aArgs['id'], + 'groupId' => $data['group_id'], + 'actionId' => $groupAction['id_action'], + 'statusId' => $status + ]); + } + } + if (!empty($groupAction['redirects'])) { + foreach ($groupAction['redirects'] as $redirect) { + BasketModel::createGroupActionRedirect([ + 'id' => $aArgs['id'], + 'groupId' => $data['group_id'], + 'actionId' => $groupAction['id_action'], + 'entityId' => $redirect['entity_id'], + 'keyword' => $redirect['keyword'], + 'redirectMode' => $redirect['redirect_mode'] + ]); + } + } + } + + return $response->withJson(['success' => 'success']); + } + + public function updateGroup(Request $request, Response $response, array $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id'], 'select' => [1]]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + $data = $request->getParams(); + + $check = Validator::stringType()::notEmpty()->validate($data['result_page']); + $check = $check && Validator::arrayType()::notEmpty()->validate($data['groupActions']); + if (!$check) { + return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); + } + $data['groupActions'] = BasketController::checkGroupActions(['groupActions' => $data['groupActions']]); + if (!empty($data['groupActions']['errors'])) { + return $response->withStatus(400)->withJson(['errors' => $data['groupActions']['errors']]); + } + + if (!BasketModel::hasGroup(['id' => $aArgs['id'], 'groupId' => $aArgs['groupId']])) { + return $response->withStatus(400)->withJson(['errors' => 'Group does not exist for this basket']); + } + + BasketModel::deleteGroup(['id' => $aArgs['id'], 'groupId' => $aArgs['groupId']]); + + BasketModel::createGroup(['id' => $aArgs['id'], 'groupId' => $aArgs['groupId'], 'resultPage' => $data['result_page']]); + foreach ($data['groupActions'] as $groupAction) { + BasketModel::createGroupAction([ + 'id' => $aArgs['id'], + 'groupId' => $aArgs['groupId'], + 'actionId' => $groupAction['id_action'], + 'whereClause' => $groupAction['where_clause'], + 'usedInBasketlist' => $groupAction['used_in_basketlist'], + 'usedInActionPage' => $groupAction['used_in_action_page'], + 'defaultActionList' => $groupAction['default_action_list'] + ]); + + if (!empty($groupAction['statuses'])) { + foreach ($groupAction['statuses'] as $status) { + BasketModel::createGroupActionStatus([ + 'id' => $aArgs['id'], + 'groupId' => $aArgs['groupId'], + 'actionId' => $groupAction['id_action'], + 'statusId' => $status + ]); + } + } + if (!empty($groupAction['redirects'])) { + foreach ($groupAction['redirects'] as $redirect) { + BasketModel::createGroupActionRedirect([ + 'id' => $aArgs['id'], + 'groupId' => $aArgs['groupId'], + 'actionId' => $groupAction['id_action'], + 'entityId' => $redirect['entity_id'], + 'keyword' => $redirect['keyword'], + 'redirectMode' => $redirect['redirect_mode'] + ]); + } + } + } + + return $response->withJson(['success' => 'success']); + } + + public function getDataForGroupById(Request $request, Response $response, $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id']]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + $basketGroups = BasketModel::getGroups(['id' => $aArgs['id']]); + $groups = GroupModel::get(['select' => ['group_id', 'group_desc']]); + + foreach ($groups as $key => $group) { + $found = false; + foreach ($basketGroups as $basketGroup) { + if ($basketGroup['group_id'] == $group['group_id']) { + $found = true; + break; + } + } + if ($found) { + unset($groups[$key]); + } + } + $groups = array_values($groups); + + $basketPages = BasketModel::getBasketPages(); + $actions = ActionModel::get(); + + return $response->withJson(['groups' => $groups, 'pages' => $basketPages, 'actions' => $actions]); + } + + public function deleteGroup(Request $request, Response $response, $aArgs) + { + if (!ServiceModel::hasService(['id' => 'admin_baskets', 'userId' => $GLOBALS['userId'], 'location' => 'basket', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + $basket = BasketModel::getById(['id' => $aArgs['id']]); + if (empty($basket)) { + return $response->withStatus(400)->withJson(['errors' => 'Basket not found']); + } + + BasketModel::deleteGroup(['id' => $aArgs['id'], 'groupId' => $aArgs['groupId']]); + + return $response->withJson(['success' => 'success']); + } + + private static function checkGroupActions(array $aArgs) { + ValidatorModel::notEmpty($aArgs, ['groupActions']); + ValidatorModel::arrayType($aArgs, ['groupActions']); + + $defaultAction = false; + $actions = ActionModel::get(['select' => ['id']]); + + foreach ($aArgs['groupActions'] as $key => $groupAction) { + $actionExists = false; + foreach ($actions as $action) { + if ($action['id'] == $groupAction['id_action']) { + $actionExists = true; + } + } + if (!$actionExists) { + return ['errors' => 'Action does not exist']; + } + if ($groupAction['default_action_list'] === true) { + $defaultAction = true; + } + $aArgs['groupActions'][$key]['where_clause'] = empty($groupAction['where_clause']) ? '' : $groupAction['where_clause']; + $aArgs['groupActions'][$key]['used_in_basketlist'] = empty($groupAction['used_in_basketlist']) ? 'N' : 'Y'; + $aArgs['groupActions'][$key]['used_in_action_page'] = empty($groupAction['used_in_action_page']) ? 'N' : 'Y'; + $aArgs['groupActions'][$key]['default_action_list'] = empty($groupAction['default_action_list']) ? 'N' : 'Y'; + } + if (!$defaultAction) { + return ['errors' => 'Default action needed']; + } + + return $aArgs['groupActions']; + } +} diff --git a/modules/basket/Models/BasketModel.php b/src/app/basket/models/BasketModel.php similarity index 96% rename from modules/basket/Models/BasketModel.php rename to src/app/basket/models/BasketModel.php index 8e9b64be37a2f7a341c740bbe0a5124625061767..02775ad8fe150a8d9031a347ad0ffd0428e13ca1 100644 --- a/modules/basket/Models/BasketModel.php +++ b/src/app/basket/models/BasketModel.php @@ -19,7 +19,7 @@ * along with Maarch Framework. If not, see <http://www.gnu.org/licenses/>. */ -namespace Baskets\Models; +namespace Basket\models; class BasketModel extends BasketModelAbstract { // Do your stuff in this class diff --git a/modules/basket/Models/BasketModelAbstract.php b/src/app/basket/models/BasketModelAbstract.php similarity index 51% rename from modules/basket/Models/BasketModelAbstract.php rename to src/app/basket/models/BasketModelAbstract.php index 3b80548b9141b30e9244bbebabbdab4c274de0ed..30fe7ca6de0a05bce39297e08d4963eaea64d1da 100644 --- a/modules/basket/Models/BasketModelAbstract.php +++ b/src/app/basket/models/BasketModelAbstract.php @@ -10,11 +10,11 @@ /** * @brief BasketModelAbstract * @author <dev@maarch.org> -* @ingroup basket */ -namespace Baskets\Models; +namespace Basket\models; +use Core\Models\CoreConfigModel; use Core\Models\DatabaseModel; use Core\Models\UserModel; use Core\Models\ValidatorModel; @@ -24,68 +24,332 @@ require_once 'core/class/SecurityControler.php'; class BasketModelAbstract { + public static function get(array $aArgs = []) + { + ValidatorModel::arrayType($aArgs, ['select']); - public static function getResListById(array $aArgs = []) + $aBaskets = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['baskets'] + ]); + + return $aBaskets; + } + + public static function getById(array $aArgs) { - ValidatorModel::notEmpty($aArgs, ['basketId']); - ValidatorModel::stringType($aArgs, ['basketId']); + ValidatorModel::notEmpty($aArgs, ['id']); + ValidatorModel::stringType($aArgs, ['id']); + ValidatorModel::arrayType($aArgs, ['select']); $aBasket = DatabaseModel::select([ - 'select' => ['basket_clause', 'basket_res_order'], + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], 'table' => ['baskets'], 'where' => ['basket_id = ?'], - 'data' => [$aArgs['basketId']] + 'data' => [$aArgs['id']] ]); - if (empty($aBasket[0]) || empty($aBasket[0]['basket_clause'])) { - return []; - } + return $aBasket[0]; + } - $sec = new \SecurityControler(); - $where = $sec->process_security_where_clause($aBasket[0]['basket_clause'], $_SESSION['user']['UserId'], false); + public static function create(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'name', 'description', 'clause', 'isVisible', 'isFolderBasket', 'flagNotif']); + ValidatorModel::stringType($aArgs, ['id', 'name', 'color', 'description', 'clause', 'isVisible', 'isFolderBasket', 'flagNotif']); + + DatabaseModel::insert([ + 'table' => 'baskets', + 'columnsValues' => [ + 'basket_id' => $aArgs['id'], + 'basket_name' => $aArgs['name'], + 'basket_desc' => $aArgs['description'], + 'basket_clause' => $aArgs['clause'], + 'is_visible' => $aArgs['isVisible'], + 'is_folder_basket' => $aArgs['isFolderBasket'], + 'flag_notif' => $aArgs['flagNotif'], + 'color' => $aArgs['color'], + 'coll_id' => 'letterbox_coll', + ] + ]); - $aResList = DatabaseModel::select([ + return true; + } + + public static function update(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'name', 'description', 'clause', 'isVisible', 'isFolderBasket', 'flagNotif']); + ValidatorModel::stringType($aArgs, ['id', 'name', 'color', 'description', 'clause', 'isVisible', 'isFolderBasket', 'flagNotif']); + + DatabaseModel::update([ + 'table' => 'baskets', + 'set' => [ + 'basket_name' => $aArgs['name'], + 'basket_desc' => $aArgs['description'], + 'basket_clause' => $aArgs['clause'], + 'is_visible' => $aArgs['isVisible'], + 'is_folder_basket' => $aArgs['isFolderBasket'], + 'flag_notif' => $aArgs['flagNotif'], + 'color' => $aArgs['color'], + 'coll_id' => 'letterbox_coll', + ], + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + + return true; + } + + public static function delete(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id']); + ValidatorModel::stringType($aArgs, ['id']); + + DatabaseModel::delete([ + 'table' => 'baskets', + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + DatabaseModel::delete([ + 'table' => 'groupbasket', + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + DatabaseModel::delete([ + 'table' => 'groupbasket_redirect', + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + DatabaseModel::delete([ + 'table' => 'groupbasket_status', + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + DatabaseModel::delete([ + 'table' => 'actions_groupbaskets', + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + DatabaseModel::delete([ + 'table' => 'user_baskets_secondary', + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] + ]); + + return true; + } + + public static function getGroups(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id']); + ValidatorModel::stringType($aArgs, ['id']); + ValidatorModel::arrayType($aArgs, ['select']); + + $aGroups = DatabaseModel::select([ 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], - 'table' => ['res_view_letterbox'], - 'where' => [$where], - 'order_by' => empty($aBasket[0]['basket_res_order']) ? ['creation_date DESC'] : [$aBasket[0]['basket_res_order']], + 'table' => ['groupbasket'], + 'where' => ['basket_id = ?'], + 'data' => [$aArgs['id']] ]); - return $aResList; + return $aGroups; + } + + public static function createGroup(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'resultPage']); + ValidatorModel::stringType($aArgs, ['id', 'groupId', 'resultPage']); + + DatabaseModel::insert([ + 'table' => 'groupbasket', + 'columnsValues' => [ + 'basket_id' => $aArgs['id'], + 'group_id' => $aArgs['groupId'], + 'result_page' => $aArgs['resultPage'] + ] + ]); + + return true; + } + + public static function createGroupAction(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'actionId', 'usedInBasketlist', 'usedInActionPage', 'defaultActionList']); + ValidatorModel::stringType($aArgs, ['id', 'groupId', 'actionId', 'whereClause', 'usedInBasketlist', 'usedInActionPage', 'defaultActionList']); + + DatabaseModel::insert([ + 'table' => 'actions_groupbaskets', + 'columnsValues' => [ + 'id_action' => $aArgs['actionId'], + 'where_clause' => $aArgs['whereClause'], + 'group_id' => $aArgs['groupId'], + 'basket_id' => $aArgs['id'], + 'used_in_basketlist' => $aArgs['usedInBasketlist'], + 'used_in_action_page' => $aArgs['usedInActionPage'], + 'default_action_list' => $aArgs['defaultActionList'], + ] + ]); + + return true; } - public static function getActionByActionId(array $aArgs = []) + public static function createGroupActionRedirect(array $aArgs) { - ValidatorModel::notEmpty($aArgs, ['actionId']); - ValidatorModel::intVal($aArgs, ['actionId']); + ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'actionId', 'redirectMode']); + ValidatorModel::stringType($aArgs, ['id', 'groupId', 'actionId', 'entityId', 'keyword', 'redirectMode']); + + DatabaseModel::insert([ + 'table' => 'groupbasket_redirect', + 'columnsValues' => [ + 'action_id' => $aArgs['actionId'], + 'group_id' => $aArgs['groupId'], + 'basket_id' => $aArgs['id'], + 'entity_id' => $aArgs['entityId'], + 'keyword' => $aArgs['keyword'], + 'redirect_mode' => $aArgs['redirectMode'] + ] + ]); + + return true; + } + + public static function createGroupActionStatus(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'actionId', 'statusId']); + ValidatorModel::stringType($aArgs, ['id', 'groupId', 'actionId', 'statusId']); + + DatabaseModel::insert([ + 'table' => 'groupbasket_status', + 'columnsValues' => [ + 'action_id' => $aArgs['actionId'], + 'group_id' => $aArgs['groupId'], + 'basket_id' => $aArgs['id'], + 'status_id' => $aArgs['statusId'] + ] + ]); + + return true; + } - $aAction = DatabaseModel::select([ + public static function deleteGroup(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId']); + ValidatorModel::stringType($aArgs, ['id', 'groupId']); + + DatabaseModel::delete([ + 'table' => 'groupbasket', + 'where' => ['basket_id = ?', 'group_id = ?'], + 'data' => [$aArgs['id'], $aArgs['groupId']] + ]); + DatabaseModel::delete([ + 'table' => 'actions_groupbaskets', + 'where' => ['basket_id = ?', 'group_id = ?'], + 'data' => [$aArgs['id'], $aArgs['groupId']] + ]); + DatabaseModel::delete([ + 'table' => 'groupbasket_redirect', + 'where' => ['basket_id = ?', 'group_id = ?'], + 'data' => [$aArgs['id'], $aArgs['groupId']] + ]); + DatabaseModel::delete([ + 'table' => 'groupbasket_status', + 'where' => ['basket_id = ?', 'group_id = ?'], + 'data' => [$aArgs['id'], $aArgs['groupId']] + ]); + + return true; + } + + public static function getActionsForGroupById(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId']); + ValidatorModel::stringType($aArgs, ['id', 'groupId']); + ValidatorModel::arrayType($aArgs, ['select']); + + $aGroups = DatabaseModel::select([ 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], - 'table' => ['actions'], - 'where' => ['id = ?'], - 'data' => [$aArgs['actionId']] + 'table' => ['actions_groupbaskets'], + 'where' => ['basket_id = ?', 'group_id = ?'], + 'data' => [$aArgs['id'], $aArgs['groupId']] ]); - return $aAction[0]; + return $aGroups; } - public static function getActionIdById(array $aArgs = []) + public static function getGroupBasketStatusesIn(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'actionIds']); + ValidatorModel::stringType($aArgs, ['id', 'groupId']); + ValidatorModel::arrayType($aArgs, ['select', 'actionIds']); + + $aStatuses = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['groupbasket_status'], + 'where' => ['basket_id = ?', 'group_id = ?', 'action_id in (?)'], + 'data' => [$aArgs['id'], $aArgs['groupId'], $aArgs['actionIds']] + ]); + + return $aStatuses; + } + + public static function getGroupBasketRedirectIn(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'actionIds']); + ValidatorModel::stringType($aArgs, ['id', 'groupId']); + ValidatorModel::arrayType($aArgs, ['select', 'actionIds']); + + $aRedirects = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['groupbasket_redirect'], + 'where' => ['basket_id = ?', 'group_id = ?', 'action_id in (?)'], + 'data' => [$aArgs['id'], $aArgs['groupId'], $aArgs['actionIds']] + ]); + + return $aRedirects; + } + + public static function hasGroup(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id', 'groupId']); + ValidatorModel::stringType($aArgs, ['id', 'groupId']); + + $groups = BasketModel::getGroups(['id' => $aArgs['id'], 'select' => ['group_id']]); + + foreach ($groups as $group) { + if ($group['group_id'] == $aArgs['groupId']) { + return true; + } + } + + return false; + } + + public static function getResListById(array $aArgs = []) { ValidatorModel::notEmpty($aArgs, ['basketId']); ValidatorModel::stringType($aArgs, ['basketId']); - $aAction = DatabaseModel::select([ - 'select' => ['id_action'], - 'table' => ['actions_groupbaskets'], + $aBasket = DatabaseModel::select([ + 'select' => ['basket_clause', 'basket_res_order'], + 'table' => ['baskets'], 'where' => ['basket_id = ?'], 'data' => [$aArgs['basketId']] ]); - if (empty($aAction[0])) { - return ''; + if (empty($aBasket[0]) || empty($aBasket[0]['basket_clause'])) { + return []; } - return $aAction[0]['id_action']; + $sec = new \SecurityControler(); + $where = $sec->process_security_where_clause($aBasket[0]['basket_clause'], $_SESSION['user']['UserId'], false); + + $aResList = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['res_view_letterbox'], + 'where' => [$where], + 'order_by' => empty($aBasket[0]['basket_res_order']) ? ['creation_date DESC'] : [$aBasket[0]['basket_res_order']], + ]); + + return $aResList; } public static function getBasketsByUserId(array $aArgs) @@ -293,6 +557,32 @@ class BasketModelAbstract return $coloredBaskets; } + public static function getBasketPages() + { + $customId = CoreConfigModel::getCustomId(); + if (file_exists("custom/{$customId}/modules/basket/xml/basketpage.xml")) { + $path = "custom/{$customId}/modules/basket/xml/basketpage.xml"; + } else { + $path = 'modules/basket/xml/basketpage.xml'; + } + + $basketPages = []; + if (file_exists($path)) { + $loadedXml = simplexml_load_file($path); + if ($loadedXml) { + foreach ($loadedXml->BASKETPAGE as $value) { + $basketPages[] = [ + 'id' => (string)$value->ID, + 'label' => (string)$value->LABEL, + 'name' => (string)$value->NAME + ]; + } + } + } + + return $basketPages; + } + // TODO In Progress public static function getPreparedClauseById(array $aArgs) {