/** * Renders grid column * * @param \Magento\Framework\Object $row * @return string */ public function render(\Magento\Framework\Object $row) { $line = parent::_getValue($row); $wrappedLine = ''; $lineLength = $this->getColumn()->getData('lineLength') ? $this->getColumn()->getData('lineLength') : $this->_defaultMaxLineLength; for ($i = 0, $n = floor($this->string->strlen($line) / $lineLength); $i <= $n; $i++) { $wrappedLine .= $this->string->substr($line, $lineLength * $i, $lineLength) . "<br />"; } return $wrappedLine; }
/** * Special processing before attribute save: * a) check some rules for password * b) transform temporary attribute 'password' into real attribute 'password_hash' * * @param \Magento\Framework\Object $object * @return void * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave($object) { $password = $object->getPassword(); $length = $this->string->strlen($password); if ($length > 0) { if ($length < self::MIN_PASSWORD_LENGTH) { throw new LocalizedException(__('The password must have at least %1 characters.', self::MIN_PASSWORD_LENGTH)); } if ($this->string->substr($password, 0, 1) == ' ' || $this->string->substr($password, $length - 1, 1) == ' ') { throw new LocalizedException(__('The password can not begin or end with a space.')); } $object->setPasswordHash($object->hashPassword($password)); } }
/** * Load search results * * @return $this */ public function load() { $result = array(); if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) { $this->setResults($result); return $this; } $collection = $this->_catalogSearchData->getQuery()->getSearchCollection()->addAttributeToSelect('name')->addAttributeToSelect('description')->addBackendSearchFilter($this->getQuery())->setCurPage($this->getStart())->setPageSize($this->getLimit())->load(); foreach ($collection as $product) { $description = strip_tags($product->getDescription()); $result[] = array('id' => 'product/1/' . $product->getId(), 'type' => __('Product'), 'name' => $product->getName(), 'description' => $this->string->substr($description, 0, 30), 'url' => $this->_adminhtmlData->getUrl('catalog/product/edit', array('id' => $product->getId()))); } $this->setResults($result); return $this; }
/** * Return formatted option value ready to edit, ready to parse * * @param string $optionValue Prepared for cart option value * @return string */ public function getEditableOptionValue($optionValue) { $option = $this->getOption(); $result = ''; if (!$this->_isSingleSelection()) { foreach (explode(',', $optionValue) as $_value) { $_result = $option->getValueById($_value); if ($_result) { $result .= $_result->getTitle() . ', '; } else { if ($this->getListener()) { $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage()); $result = ''; break; } } } $result = $this->string->substr($result, 0, -2); } elseif ($this->_isSingleSelection()) { $_result = $option->getValueById($optionValue); if ($_result) { $result = $_result->getTitle(); } else { if ($this->getListener()) { $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage()); } $result = ''; } } else { $result = $optionValue; } return $result; }
/** * Add meta information from product to head block * * @return \Magento\Catalog\Block\Product\View */ protected function _prepareLayout() { $this->getLayout()->createBlock('Magento\\Catalog\\Block\\Breadcrumbs'); $product = $this->getProduct(); if (!$product) { return parent::_prepareLayout(); } $title = $product->getMetaTitle(); if ($title) { $this->pageConfig->getTitle()->set($title); } $keyword = $product->getMetaKeyword(); $currentCategory = $this->_coreRegistry->registry('current_category'); if ($keyword) { $this->pageConfig->setKeywords($keyword); } elseif ($currentCategory) { $this->pageConfig->setKeywords($product->getName()); } $description = $product->getMetaDescription(); if ($description) { $this->pageConfig->setDescription($description); } else { $this->pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 255)); } if ($this->_productHelper->canUseCanonicalTag()) { $this->pageConfig->addRemotePageAsset($product->getUrlModel()->getUrl($product, ['_ignore_category' => true]), 'canonical', ['attributes' => ['rel' => 'canonical']]); } $pageMainTitle = $this->getLayout()->getBlock('page.main.title'); if ($pageMainTitle) { $pageMainTitle->setPageTitle($product->getName()); } return parent::_prepareLayout(); }
/** * @param string $queryText * @param int|string $maxQueryLength * @return string */ private function getPreparedQueryText($queryText, $maxQueryLength) { if ($this->isQueryTooLong($queryText, $maxQueryLength)) { $queryText = $this->string->substr($queryText, 0, $maxQueryLength); } return $queryText; }
/** * Filter value * * @param string $string * @return string */ public function filter($string) { $length = $this->length; $this->remainder = ''; if (0 == $length) { return ''; } $originalLength = $this->string->strlen($string); if ($originalLength > $length) { $length -= $this->string->strlen($this->etc); if ($length <= 0) { return ''; } $preparedString = $string; $preparedLength = $length; if (!$this->breakWords) { $preparedString = preg_replace('/\\s+?(\\S+)?$/u', '', $this->string->substr($string, 0, $length + 1)); $preparedLength = $this->string->strlen($preparedString); } $this->remainder = $this->string->substr($string, $preparedLength, $originalLength); return $this->string->substr($preparedString, 0, $length) . $this->etc; } return $string; }
/** * Saving visitor information * * @param \Magento\Log\Model\Visitor $visitor * @return \Magento\Log\Model\Resource\Visitor */ protected function _saveVisitorInfo($visitor) { $referer = $this->string->cleanString($visitor->getHttpReferer()); $referer = $this->string->substr($referer, 0, 255); $userAgent = $this->string->cleanString($visitor->getHttpUserAgent()); $userAgent = $this->string->substr($userAgent, 0, 255); $charset = $this->string->cleanString($visitor->getHttpAcceptCharset()); $charset = $this->string->substr($charset, 0, 255); $language = $this->string->cleanString($visitor->getHttpAcceptLanguage()); $language = $this->string->substr($language, 0, 255); $data = new \Magento\Framework\Object(array('visitor_id' => $visitor->getId(), 'http_referer' => $referer, 'http_user_agent' => $userAgent, 'http_accept_charset' => $charset, 'http_accept_language' => $language, 'server_addr' => $visitor->getServerAddr(), 'remote_addr' => $visitor->getRemoteAddr())); $bind = $this->_prepareDataForTable($data, $this->getTable('log_visitor_info')); $adapter = $this->_getWriteAdapter(); $adapter->insert($this->getTable('log_visitor_info'), $bind); return $this; }
/** * Add meta information from product to head block * * @return \Magento\Catalog\Block\Product\View */ protected function _prepareLayout() { $this->getLayout()->createBlock('Magento\\Catalog\\Block\\Breadcrumbs'); $product = $this->getProduct(); if (!$product) { return parent::_prepareLayout(); } $headBlock = $this->getLayout()->getBlock('head'); if ($headBlock) { $title = $product->getMetaTitle(); if ($title) { $headBlock->setTitle($title); } $keyword = $product->getMetaKeyword(); $currentCategory = $this->_coreRegistry->registry('current_category'); if ($keyword) { $headBlock->setKeywords($keyword); } elseif ($currentCategory) { $headBlock->setKeywords($product->getName()); } $description = $product->getMetaDescription(); if ($description) { $headBlock->setDescription($description); } else { $headBlock->setDescription($this->string->substr($product->getDescription(), 0, 255)); } //@todo: move canonical link to separate block $childBlockName = 'magento-page-head-product-canonical-link'; if ($this->_productHelper->canUseCanonicalTag() && !$headBlock->getChildBlock($childBlockName)) { $params = array('_ignore_category' => true); $headBlock->addChild($childBlockName, 'Magento\\Theme\\Block\\Html\\Head\\Link', array('url' => $product->getUrlModel()->getUrl($product, $params), 'properties' => array('attributes' => array('rel' => 'canonical')))); } } $pageMainTitle = $this->getLayout()->getBlock('page.main.title'); if ($pageMainTitle) { $pageMainTitle->setPageTitle($product->getName()); } return parent::_prepareLayout(); }
/** * Prepare and set request in property of current instance * * @param \Magento\Framework\Object $request * @return $this */ public function setRequest(\Magento\Framework\Object $request) { $this->_request = $request; $this->setStore($request->getStoreId()); $requestObject = new \Magento\Framework\Object(); $requestObject->setIsGenerateLabelReturn($request->getIsGenerateLabelReturn()); $requestObject->setStoreId($request->getStoreId()); if ($request->getLimitMethod()) { $requestObject->setService($request->getLimitMethod()); } $requestObject = $this->_addParams($requestObject); if ($request->getDestPostcode()) { $requestObject->setDestPostal($request->getDestPostcode()); } $requestObject->setOrigCountry($this->_getDefaultValue($request->getOrigCountry(), Shipment::XML_PATH_STORE_COUNTRY_ID))->setOrigCountryId($this->_getDefaultValue($request->getOrigCountryId(), Shipment::XML_PATH_STORE_COUNTRY_ID)); $shippingWeight = $request->getPackageWeight(); $requestObject->setValue(round($request->getPackageValue(), 2))->setValueWithDiscount($request->getPackageValueWithDiscount())->setCustomsValue($request->getPackageCustomsValue())->setDestStreet($this->string->substr(str_replace("\n", '', $request->getDestStreet()), 0, 35))->setDestStreetLine2($request->getDestStreetLine2())->setDestCity($request->getDestCity())->setOrigCompanyName($request->getOrigCompanyName())->setOrigCity($request->getOrigCity())->setOrigPhoneNumber($request->getOrigPhoneNumber())->setOrigPersonName($request->getOrigPersonName())->setOrigEmail($this->_scopeConfig->getValue('trans_email/ident_general/email', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $requestObject->getStoreId()))->setOrigCity($request->getOrigCity())->setOrigPostal($request->getOrigPostal())->setOrigStreetLine2($request->getOrigStreetLine2())->setDestPhoneNumber($request->getDestPhoneNumber())->setDestPersonName($request->getDestPersonName())->setDestCompanyName($request->getDestCompanyName()); $originStreet2 = $this->_scopeConfig->getValue(Shipment::XML_PATH_STORE_ADDRESS2, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $requestObject->getStoreId()); $requestObject->setOrigStreet($request->getOrigStreet() ? $request->getOrigStreet() : $originStreet2); if (is_numeric($request->getOrigState())) { $requestObject->setOrigState($this->_regionFactory->create()->load($request->getOrigState())->getCode()); } else { $requestObject->setOrigState($request->getOrigState()); } if ($request->getDestCountryId()) { $destCountry = $request->getDestCountryId(); } else { $destCountry = self::USA_COUNTRY_ID; } // for DHL, Puerto Rico state for US will assume as Puerto Rico country // for Puerto Rico, dhl will ship as international if ($destCountry == self::USA_COUNTRY_ID && ($request->getDestPostcode() == '00912' || $request->getDestRegionCode() == self::PUERTORICO_COUNTRY_ID)) { $destCountry = self::PUERTORICO_COUNTRY_ID; } $requestObject->setDestCountryId($destCountry)->setDestState($request->getDestRegionCode())->setWeight($shippingWeight)->setFreeMethodWeight($request->getFreeMethodWeight())->setOrderShipment($request->getOrderShipment()); if ($request->getPackageId()) { $requestObject->setPackageId($request->getPackageId()); } $requestObject->setBaseSubtotalInclTax($request->getBaseSubtotalInclTax()); $this->setRawRequest($requestObject); return $this; }
public function testSubstr() { $this->assertSame('tring', $this->_string->substr('string', 1)); }
/** * Remove characters and words not allowed by Google Content in title and content (description). * * To avoid "Expected response code 200, got 400. * Reason: There is a problem with the character encoding of this attribute" * * @param string $string * @return string */ public function cleanAtomAttribute($string) { return $this->string->substr(preg_replace('/[\\pC¢€•—™°½]|shipping/ui', '', $string), 0, 3500); }
/** * Get formatted order description * * @param \Magento\Sales\Model\Order $order * @return string */ protected function _getOrderDescription($order) { $invoiceDesc = ''; foreach ($order->getAllItems() as $item) { if ($item->getParentItem()) { continue; } //COM filed can only handle max 100 if ($this->string->strlen($invoiceDesc . $item->getName()) > 100) { break; } $invoiceDesc .= $item->getName() . ', '; } return $this->string->substr($invoiceDesc, 0, -2); }