diff --git a/database/factories/ProcessCategoryFactory.php b/database/factories/ProcessCategoryFactory.php index 0f8f19776..374951bfa 100644 --- a/database/factories/ProcessCategoryFactory.php +++ b/database/factories/ProcessCategoryFactory.php @@ -1,12 +1,15 @@ define(\ProcessMaker\Model\ProcessCategory::class, function (Faker $faker) { return [ 'CATEGORY_UID' => G::generateUniqueID(), + 'CATEGORY_ID' => $faker->randomNumber(8), 'CATEGORY_PARENT' => '', 'CATEGORY_NAME' => $faker->sentence(5), 'CATEGORY_ICON' => '', diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessCategoryTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessCategoryTest.php new file mode 100644 index 000000000..2ec55b34d --- /dev/null +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/ProcessCategoryTest.php @@ -0,0 +1,115 @@ +delete(); + } + + /** + * Tests the getProcessCategories method without paremeters + * + * @test + */ + public function it_tests_get_process_categories_method_without_paremeters() + { + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 1 + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 2 + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 3 + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 4 + ]); + $result = ProcessCategory::getProcessCategories(); + + $this->assertCount(4, $result); + } + + /** + * Tests the getProcessCategories method filtered by name + * + * @test + */ + public function it_tests_get_process_categories_method_filter_by_name() + { + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 1, + 'CATEGORY_NAME' => 'Category1' + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 2, + 'CATEGORY_NAME' => 'Category2' + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 3, + 'CATEGORY_NAME' => 'Category3' + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 4, + 'CATEGORY_NAME' => 'Category4' + ]); + $result = ProcessCategory::getProcessCategories('1'); + + $this->assertCount(1, $result); + } + + /** + * Tests the getProcessCategories method with start and limit parameters + * + * @test + */ + public function it_tests_get_process_categories_method_with_start_limit() + { + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 1, + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 2, + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 3, + ]); + factory(ProcessCategory::class)->create([ + 'CATEGORY_ID' => 4, + ]); + $result = ProcessCategory::getProcessCategories(null, 1, 3); + + $this->assertCount(3, $result); + } + + /** + * Tests the getCategoryId method + * + * @test + */ + public function it_tests_get_category_id_method() + { + $processCategory = factory(ProcessCategory::class)->create(); + $result = ProcessCategory::getCategoryId($processCategory->CATEGORY_UID); + + $this->assertEquals($processCategory->CATEGORY_ID, $result); + } +} diff --git a/workflow/engine/classes/model/Process.php b/workflow/engine/classes/model/Process.php index 17ea8a301..0084e7401 100644 --- a/workflow/engine/classes/model/Process.php +++ b/workflow/engine/classes/model/Process.php @@ -143,6 +143,7 @@ class Process extends BaseProcess $this->setProShowDelegate(''); $this->setProShowDynaform(''); $this->setProCategory((isset($aData["PRO_CATEGORY"]))? $aData["PRO_CATEGORY"]: ""); + $this->setCategoryId((isset($aData["CATEGORY_ID"])) ? $aData["CATEGORY_ID"]: 0); $this->setProSubCategory(''); $this->setProIndustry(''); $this->setProCreateDate(date("Y-m-d H:i:s")); diff --git a/workflow/engine/src/ProcessMaker/Model/ProcessCategory.php b/workflow/engine/src/ProcessMaker/Model/ProcessCategory.php index b5e772719..3e96081c2 100644 --- a/workflow/engine/src/ProcessMaker/Model/ProcessCategory.php +++ b/workflow/engine/src/ProcessMaker/Model/ProcessCategory.php @@ -17,6 +17,18 @@ class ProcessCategory extends Model public $timestamps = false; + /** + * Scope a query to specific category name + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $name + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeCategoryName($query, $name) + { + return $query->where('CATEGORY_NAME', 'LIKE', "%{$name}%"); + } + /** * Get the categories * @@ -27,7 +39,7 @@ class ProcessCategory extends Model * @see ProcessProxy::categoriesList() * @link https://wiki.processmaker.com/3.0/Process_Categories */ - public static function getCategories( $dir = 'ASC') + public static function getCategories($dir = 'ASC') { $query = ProcessCategory::query() ->select([ @@ -38,4 +50,46 @@ class ProcessCategory extends Model return $query->get()->values()->toArray(); } + + /** + * Get the process categories + * + * @param string $name + * @param int $start + * @param int $limit + * + * @return array + * + * @see ProcessMaker\Services\Api\Home::getCategories() + */ + public static function getProcessCategories($name = null, $start = null, $limit = null) + { + $query = ProcessCategory::query()->select(['CATEGORY_ID', 'CATEGORY_NAME']); + + if (!is_null($name)) { + $query->categoryName($name); + } + + if (!is_null($start) && !is_null($limit)) { + $query->offset($start)->limit($limit); + } + + return $query->get()->toArray(); + } + + /** + * Get category Id + * + * @param string $categoryUid + * + * @return int + */ + public static function getCategoryId($categoryUid) + { + $query = ProcessCategory::query()->select(['CATEGORY_ID']); + $query->where('CATEGORY_UID', $categoryUid); + if ($query->first()) { + return $query->first()->CATEGORY_ID; + } + } } diff --git a/workflow/engine/src/ProcessMaker/Project/Workflow.php b/workflow/engine/src/ProcessMaker/Project/Workflow.php index 06d136332..27e0a8dc9 100644 --- a/workflow/engine/src/ProcessMaker/Project/Workflow.php +++ b/workflow/engine/src/ProcessMaker/Project/Workflow.php @@ -11,6 +11,7 @@ use Task as ClassesTask; use Route; use RoutePeer; +use ProcessMaker\Model\ProcessCategory; use ProcessMaker\Util\Common; use ProcessMaker\Exception; use ProcessMaker\Util; @@ -62,6 +63,8 @@ class Workflow extends Handler $data['USR_UID'] = array_key_exists('PRO_CREATE_USER', $data) ? $data['PRO_CREATE_USER'] : null; $data['PRO_TITLE'] = array_key_exists('PRO_TITLE', $data) ? trim($data['PRO_TITLE']) : ""; $data['PRO_CATEGORY'] = array_key_exists('PRO_CATEGORY', $data) ? $data['PRO_CATEGORY'] : ""; + $categoryId = ProcessCategory::getCategoryId($data['PRO_CATEGORY']); + $data['CATEGORY_ID'] = !is_null($categoryId) ? $categoryId : 0; try { diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Home.php b/workflow/engine/src/ProcessMaker/Services/Api/Home.php index da6a1a77f..55322be93 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Home.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Home.php @@ -16,6 +16,7 @@ use ProcessMaker\BusinessModel\Cases\Supervising; use ProcessMaker\BusinessModel\Cases\Unassigned; use ProcessMaker\Model\Delegation; use ProcessMaker\Model\Process; +use ProcessMaker\Model\ProcessCategory; use ProcessMaker\Model\User; use ProcessMaker\Model\UserConfig; use ProcessMaker\Model\Task; @@ -811,4 +812,30 @@ class Home extends Api { return UserConfig::deleteSetting($id, $name); } + + /** + * Get all process categories + * + * @url GET /categories + * + * @param string $name + * @param int $start + * @param int $limit + * + * @return array + * + * @throws RestException + * + * @access protected + * @class AccessControl {@permission PM_CASES} + */ + public function getCategories($name = null, $start = null, $limit = null) + { + try { + $categories = ProcessCategory::getProcessCategories($name, $start, $limit); + return $categories; + } catch (Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } }