/** * Retrieve list of categories with name containing $namePart and their parents * * @param string $namePart * @return string */ public function getSuggestedCategoriesJson($namePart) { $storeId = $this->getRequest()->getParam('store', $this->_getDefaultStoreId()); /* @var $collection Collection */ $collection = $this->_categoryFactory->create()->getCollection(); $matchingNamesCollection = clone $collection; $escapedNamePart = $this->_resourceHelper->addLikeEscape($namePart, ['position' => 'any']); $matchingNamesCollection->addAttributeToFilter('name', ['like' => $escapedNamePart])->addAttributeToFilter('entity_id', ['neq' => \Magento\Catalog\Model\Category::TREE_ROOT_ID])->addAttributeToSelect('path')->setStoreId($storeId); $shownCategoriesIds = []; foreach ($matchingNamesCollection as $category) { foreach (explode('/', $category->getPath()) as $parentId) { $shownCategoriesIds[$parentId] = 1; } } $collection->addAttributeToFilter('entity_id', ['in' => array_keys($shownCategoriesIds)])->addAttributeToSelect(['name', 'is_active', 'parent_id'])->setStoreId($storeId); $categoryById = [\Magento\Catalog\Model\Category::TREE_ROOT_ID => ['id' => \Magento\Catalog\Model\Category::TREE_ROOT_ID, 'children' => []]]; foreach ($collection as $category) { foreach ([$category->getId(), $category->getParentId()] as $categoryId) { if (!isset($categoryById[$categoryId])) { $categoryById[$categoryId] = ['id' => $categoryId, 'children' => []]; } } $categoryById[$category->getId()]['is_active'] = $category->getIsActive(); $categoryById[$category->getId()]['label'] = $category->getName(); $categoryById[$category->getParentId()]['children'][] =& $categoryById[$category->getId()]; } return $this->_jsonEncoder->encode($categoryById[\Magento\Catalog\Model\Category::TREE_ROOT_ID]['children']); }
/** * Retrieve list of attributes with admin store label containing $labelPart * * @param string $labelPart * @param int $templateId * @return \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ public function getSuggestedAttributes($labelPart, $templateId = null) { $escapedLabelPart = $this->_resourceHelper->addLikeEscape($labelPart, ['position' => 'any']); /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection */ $collection = $this->_collectionFactory->create()->addFieldToFilter('frontend_label', ['like' => $escapedLabelPart]); $collection->setExcludeSetFilter($templateId ?: $this->getRequest()->getParam('template_id'))->setPageSize(20); $result = []; foreach ($collection->getItems() as $attribute) { /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ $result[] = ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode()]; } return $result; }
/** * @param $namePart * @return string */ public function getSuggestedCategoriesJson($namePart) { /* @var $collection \Mageplaza\Blog\Model\ResourceModel\Category\Collection */ $collection = $this->categoryCollectionFactory->create(); /* @var $matchingNameCollection \Mageplaza\Blog\Model\ResourceModel\Category\Collection */ $matchingNameCollection = clone $collection; $escapedNamePart = $this->resourceHelper->addLikeEscape($namePart, ['position' => 'any']); $matchingNameCollection->addFieldToFilter('name', ['like' => $escapedNamePart])->addFieldToFilter('category_id', ['neq' => \Mageplaza\Blog\Model\Category::TREE_ROOT_ID]); $shownCategoriesIds = []; foreach ($matchingNameCollection as $category) { /** @var \Mageplaza\Blog\Model\Category $category */ foreach (explode('/', $category->getPath()) as $parentId) { $shownCategoriesIds[$parentId] = 1; } } $collection->addFieldToFilter('category_id', ['in' => array_keys($shownCategoriesIds)]); $categoriesById = [\Mageplaza\Blog\Model\Category::TREE_ROOT_ID => ['id' => \Mageplaza\Blog\Model\Category::TREE_ROOT_ID, 'children' => []]]; foreach ($collection as $category) { /** @var \Mageplaza\Blog\Model\Category $category */ foreach ([$category->getId(), $category->getParentId()] as $categoryId) { if (!isset($categoriesById[$categoryId])) { $categoriesById[$categoryId] = ['id' => $categoryId, 'children' => []]; } } $categoriesById[$category->getId()]['is_active'] = true; $categoriesById[$category->getId()]['label'] = $category->getName(); $categoriesById[$category->getParentId()]['children'][] =& $categoriesById[$category->getId()]; } return $this->jsonEncoder->encode($categoriesById[\Mageplaza\Blog\Model\Category::TREE_ROOT_ID]['children']); }
/** * Set search query text to filter * * @param string $query * @return $this */ public function setQueryFilter($query) { $this->getSelect()->reset(\Magento\Framework\DB\Select::FROM)->distinct(true)->from(['main_table' => $this->getTable('search_query')])->where('num_results > 0 AND display_in_terms = 1 AND query_text LIKE ?', $this->_resourceHelper->addLikeEscape($query, ['position' => 'start']))->order('popularity ' . \Magento\Framework\DB\Select::SQL_DESC); if ($this->getStoreId()) { $this->getSelect()->where('store_id = ?', (int) $this->getStoreId()); } return $this; }
/** * Delete files that starts with given $folderName * * @param string $folderName * @return void */ public function deleteFolder($folderName = '') { $folderName = rtrim($folderName, '/'); if (!strlen($folderName)) { return; } $likeExpression = $this->_resourceHelper->addLikeEscape($folderName . '/', ['position' => 'start']); $this->getConnection()->delete($this->getMainTable(), new \Zend_Db_Expr('filename LIKE ' . $likeExpression)); }
/** * Retrieve categories tree * * @param string|null $filter * @return array */ protected function getCategoriesTree($filter = null) { if (isset($this->categoriesTrees[$filter])) { return $this->categoriesTrees[$filter]; } $storeId = $this->locator->getStore()->getId(); /* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */ $matchingNamesCollection = $this->categoryCollectionFactory->create(); if ($filter !== null) { $matchingNamesCollection->addAttributeToFilter('name', ['like' => $this->dbHelper->addLikeEscape($filter, ['position' => 'any'])]); } $matchingNamesCollection->addAttributeToSelect('path')->addAttributeToFilter('entity_id', ['neq' => CategoryModel::TREE_ROOT_ID])->setStoreId($storeId); $shownCategoriesIds = []; /** @var \Magento\Catalog\Model\Category $category */ foreach ($matchingNamesCollection as $category) { foreach (explode('/', $category->getPath()) as $parentId) { $shownCategoriesIds[$parentId] = 1; } } /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */ $collection = $this->categoryCollectionFactory->create(); $collection->addAttributeToFilter('entity_id', ['in' => array_keys($shownCategoriesIds)])->addAttributeToSelect(['name', 'is_active', 'parent_id'])->setStoreId($storeId); $categoryById = [CategoryModel::TREE_ROOT_ID => ['value' => CategoryModel::TREE_ROOT_ID, 'optgroup' => null]]; foreach ($collection as $category) { foreach ([$category->getId(), $category->getParentId()] as $categoryId) { if (!isset($categoryById[$categoryId])) { $categoryById[$categoryId] = ['value' => $categoryId]; } } $categoryById[$category->getId()]['is_active'] = $category->getIsActive(); $categoryById[$category->getId()]['label'] = $category->getName(); $categoryById[$category->getParentId()]['optgroup'][] =& $categoryById[$category->getId()]; } $this->categoriesTrees[$filter] = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']; return $this->categoriesTrees[$filter]; }
public function testAddLikeEscape() { $value = $this->_model->addLikeEscape('test'); $this->assertInstanceOf('Zend_Db_Expr', $value); $this->assertContains('test', (string) $value); }
/** * Retrieve condition * * @return array */ public function getCondition() { $likeExpression = $this->_resourceHelper->addLikeEscape($this->getValue(), array('position' => 'any')); return array('like' => $likeExpression); }
/** * Retrieve condition * * @return array */ public function getCondition() { $likeExpression = $this->_resourceHelper->addLikeEscape($this->getValue(), ['position' => 'any']); return ['like' => $likeExpression]; }