/** * 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']); }
/** * @param array $columns * @param string $query * @return string */ public function getScoreQuery($columns, $query) { $cases = []; $fullCases = []; $words = preg_split('#\\s#siu', $query, null, PREG_SPLIT_NO_EMPTY); foreach ($columns as $column) { $cases[5][] = $this->dbHelper->getCILike($column, ' ' . $query . ' '); } foreach ($words as $word) { foreach ($columns as $column) { $cases[3][] = $this->dbHelper->getCILike($column, ' ' . $word . ' ', ['position' => 'any']); $cases[2][] = $this->dbHelper->getCILike($column, $word, ['position' => 'any']); } } foreach ($words as $word) { foreach ($columns as $column) { $e = '(LENGTH(' . $column . ')'; $e .= '- LOCATE("' . addslashes($word) . '", ' . $column . ')) / LENGTH(' . $column . ')'; $locate = new \Zend_Db_Expr($e); $cases[$locate->__toString()][] = $locate; } } foreach ($cases as $weight => $conditions) { foreach ($conditions as $condition) { $fullCases[] = 'CASE WHEN ' . $condition . ' THEN ' . $weight . ' ELSE 0 END'; } } if (count($fullCases)) { $select = '(' . implode('+', $fullCases) . ')'; } else { $select = '0'; } return $select; }
/** * 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)); }
/** * Join table sales_order_address to select for billing and shipping order addresses. * Create correlation map * * @return $this */ protected function _addAddressFields() { $billingAliasName = 'billing_o_a'; $shippingAliasName = 'shipping_o_a'; $joinTable = $this->getTable('sales_order_address'); $this->addFilterToMap('billing_firstname', $billingAliasName . '.firstname')->addFilterToMap('billing_lastname', $billingAliasName . '.lastname')->addFilterToMap('billing_telephone', $billingAliasName . '.telephone')->addFilterToMap('billing_postcode', $billingAliasName . '.postcode')->addFilterToMap('shipping_firstname', $shippingAliasName . '.firstname')->addFilterToMap('shipping_lastname', $shippingAliasName . '.lastname')->addFilterToMap('shipping_telephone', $shippingAliasName . '.telephone')->addFilterToMap('shipping_postcode', $shippingAliasName . '.postcode'); $this->getSelect()->joinLeft([$billingAliasName => $joinTable], "(main_table.entity_id = {$billingAliasName}.parent_id" . " AND {$billingAliasName}.address_type = 'billing')", [$billingAliasName . '.firstname', $billingAliasName . '.lastname', $billingAliasName . '.telephone', $billingAliasName . '.postcode'])->joinLeft([$shippingAliasName => $joinTable], "(main_table.entity_id = {$shippingAliasName}.parent_id" . " AND {$shippingAliasName}.address_type = 'shipping')", [$shippingAliasName . '.firstname', $shippingAliasName . '.lastname', $shippingAliasName . '.telephone', $shippingAliasName . '.postcode']); $this->_coreResourceHelper->prepareColumnsList($this->getSelect()); return $this; }
/** * Join product and type data * * @return $this */ protected function _joinTables() { $entityType = $this->_eavConfig->getEntityType('catalog_product'); $attribute = $this->_eavConfig->getAttribute($entityType->getEntityTypeId(), 'name'); $joinConditionDefault = sprintf("p_d.attribute_id=%d AND p_d.store_id='0' AND main_table.product_id=p_d.entity_id", $attribute->getAttributeId()); $joinCondition = sprintf("p.attribute_id=%d AND p.store_id=main_table.store_id AND main_table.product_id=p.entity_id", $attribute->getAttributeId()); $this->getSelect()->joinLeft(array('p_d' => $attribute->getBackend()->getTable()), $joinConditionDefault, array()); $this->getSelect()->joinLeft(array('p' => $attribute->getBackend()->getTable()), $joinCondition, array('name' => $this->getConnection()->getIfNullSql('p.value', 'p_d.value'))); $this->getSelect()->joinLeft(array('types' => $this->getTable('googleshopping_types')), 'main_table.type_id=types.type_id'); $this->_resourceHelper->prepareColumnsList($this->getSelect()); // avoid column name collision return $this; }
/** * 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]; }
/** * Construct * * @param \Magento\Framework\App\ResourceConnection $resource * @param string $modulePrefix * @codeCoverageIgnore */ public function __construct(\Magento\Framework\App\ResourceConnection $resource, $modulePrefix = 'Magento_Eav') { parent::__construct($resource, $modulePrefix); }
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]; }
/** * @param \Magento\Framework\App\ResourceConnection $resource * @param \Magento\Reports\Model\ResourceModel\Helper $reportsResourceHelper * @param string $modulePrefix */ public function __construct(\Magento\Framework\App\ResourceConnection $resource, \Magento\Reports\Model\ResourceModel\Helper $reportsResourceHelper, $modulePrefix = 'sales') { parent::__construct($resource, $modulePrefix); $this->_reportsResourceHelper = $reportsResourceHelper; }
/** * @param \Magento\Framework\App\Resource $resource * @param string $modulePrefix * @param \Magento\Framework\Stdlib\DateTime\DateTime $coreDate */ public function __construct(\Magento\Framework\App\Resource $resource, $modulePrefix, \Magento\Framework\Stdlib\DateTime\DateTime $coreDate) { parent::__construct($resource, $modulePrefix); $this->_coreDate = $coreDate; }
/** * @param \Magento\Framework\App\Resource $resource * @param string $modulePrefix */ public function __construct(\Magento\Framework\App\Resource $resource, $modulePrefix = 'importexport') { parent::__construct($resource, $modulePrefix); }
/** * @param \Magento\Framework\App\Resource $resource * @param string $modulePrefix */ public function __construct(\Magento\Framework\App\Resource $resource, $modulePrefix = 'Magento_Search') { parent::__construct($resource, $modulePrefix); }