<commit_before><?php

/**
 * Handles database operations for Product Category
 *
 * @author <NAME> (<EMAIL>)
 */
class CategoryRepository extends BaseRepository
{
	private $category_images_folder = './files/attach/images/shop/%d/product-categories/';

	/**
	 * Insert a new Product category; returns the ID of the newly created record.
	 *
	 * @param Category $category Category to inserted
	 *
	 * @throws Exception DatabaseError.
	 * @return category_srl int
	 */
	public function insertCategory(Category $category)
	{
		$category->category_srl = getNextSequence();
		$output = executeQuery('shop.insertCategory', $category);
		if(!$output->toBool())
		{
			throw new Exception($output->getMessage(), $output->getError());
		}
		return $category->category_srl;
	}

	/**
	 * Deletes a product category by $category_srl or $module_srl
	 *
	 * @param stdClass $args Can have the following properties: category_srl or module_srl
	 *
	 * @throws Exception
	 * @return bool
	 */
	public function deleteCategory($args)
	{
		if(!isset($args->category_srl) && !isset($args->module_srl))
		{
			throw new Exception("Missing arguments for Product category delete: please provide [category_srl] or [module_srl]");
		}

		// Get category info before deleting it, so we can also delete category image
		if($args->category_srl)
		{
			$category = $this->getCategory($args->category_srl);
		}

		$output = executeQuery('shop.deleteCategory', $args);
		if(!$output->toBool())
		{
			throw new Exception($output->getMessage(), $output->getError());
		}

		$output = executeQuery('shop.deleteAttributesScope', $args);
		if(!$output->toBool())
		{
			throw new Exception($output->getMessage(), $output->getError());
		}

		$output = executeQuery('shop.deleteProductCategories', $args);
		if(!$output->toBool())
		{
			throw new Exception($output->getMessage(), $output->getError());
		}

		if($args->category_srl)
		{
			$output = $this->deleteCategoryImage($category->filename);
			if(!$output)
			{
				throw new Exception("Could not delete category image");
			}
		}
		else
		{
			$this->deleteCategoriesImages($args->module_srl);
		}

		return TRUE;
	}

	/**
	 * Retrieve a Category object from the database given a srl
	 *
	 * @param int $category_srl by which to select the Category
	 *
	 * @throws Exception
	 * @return Category
	 */
	public function getCategory($category_srl)
	{
		$args = new stdClass();
		$args->category_srl = $category_srl;<commit_msg>Issue 52:	When trying to delete product category from backend sometimes error occurs<commit_after><?php

/**
 * Handles database operations for Product Category
 *
 * @author <NAME> (<EMAIL>)
 */
class CategoryRepository extends BaseRepository
{
	private $category_images_folder = './files/attach/images/shop/%d/product-categories/';

	/**
	 * Insert a new Product category; returns the ID of the newly created record.
	 *
	 * @param Category $category Category to inserted
	 *
	 * @throws Exception DatabaseError.
	 * @return category_srl int
	 */
	public function insertCategory(Category $category)
	{
		$category->category_srl = getNextSequence();
		$output = executeQuery('shop.insertCategory', $category);
		if(!$output->toBool())
		{
			throw new Exception($output->getMessage(), $output->getError());
		}
		return $category->category_srl;
	}

	/**
	 * Deletes a product category by $category_srl or $module_srl
	 *
	 * @param stdClass $args Can have the following properties: category_srl or module_srl
	 *
	 * @throws Exception
	 * @return bool
	 */
	public function deleteCategory($args)
	{
		if(!isset($args->category_srl) && !isset($args->module_srl))
		{
			throw new Exception("Missing arguments for Product category delete: please provide [category_srl] or [module_srl]");
		}

		// Get category info before deleting it, so we can also delete category image
		if($args->category_srl)
		{
			$category = $this->getCategory($args->category_srl);
		}

        $db = DB::getInstance();
        $db->begin();

		$output = executeQuery('shop.deleteCategory', $args);
		if(!$output->toBool())
		{
            $db->rollback();
			throw new Exception($output->getMessage(), $output->getError());
		}

		$output = executeQuery('shop.deleteAttributesScope', $args);
		if(!$output->toBool())
		{
            $db->rollback();
			throw new Exception($output->getMessage(), $output->getError());
		}

		$output = executeQuery('shop.deleteProductCategories', $args);
		if(!$output->toBool())
		{
            $db->rollback();
			throw new Exception($output->getMessage(), $output->getError());
		}

		if($args->category_srl)
		{
			$this->deleteCategoryImage($category->filename);
		}
		else
		{
			$this->deleteCategoriesImages($args->module_srl);
		}

        $db->commit();

		return TRUE;
	}

	/**
	 * Retrieve a Category object from the database given a srl
	 *
	 * @param int $category_srl by which to select the Category
	 *
	 * @throws Exception
	 * @return Category
	 */
	public function getCategory($category_srl)
	{
		$args = new stdClass();
		$args->category_srl = $category_srl;
{'fraction_non_alphanumeric': 0.1436233611442193, 'fraction_numerical': 0.0003972983710766786, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '118663', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1490, 'n_words': 379}