D7¶
This section contains scripts with example of using Bitrix D7 core
ORM¶
Select with Query object¶
<?php
use \Bitrix\Main\Loader;
use \Bitrix\Main\Entity\Query;
use \Bitrix\Crm\DealTable;
Loader::includeModule('crm');
$query = new Query(DealTable::getEntity());
$query->setFilter(array(
'STAGE_ID' => 'WON',
));
$query->setSelect(array(
'ID',
'LEAD_DATE_CREATE' => 'LEAD_BY.DATE_CREATE',
'CONTACT_DATE_CREATE' => 'CONTACT_BY.DATE_CREATE',
'USER_ASSIGNED' => 'CONTACT_BY.ASSIGNED_BY_ID',
'CLOSEDATE' => 'CLOSEDATE',
'MONEY' => 'OPPORTUNITY',
'CURRENCY' => 'CURRENCY_ID'
));
$result = $query->exec();
$deals = array();
while ($arDeal = $result->fetch()) {
}
Select with Entity object¶
<?php
use \Bitrix\Main\Loader;
use \Bitrix\Crm\DealTable;
Loader::includeModule('crm');
$deals = DealTable::getList([
'filter' => [
'ID' => $dealId
]
])->fetch();
Join with ORM¶
<?php
use \Bitrix\Main\Loader;
use \Bitrix\Main\Entity\Query;
use \Bitrix\Crm\DealTable;
Loader::includeModule('crm');
$arResult = array();
$query = new Query(LeadTable::getEntity());
$query
//JOIN for b_crm_status table
->registerRuntimeField(
'STATUSES',
array(
'data_type' => '\\Bitrix\\Crm\\StatusTable',
'reference' => array(
'=this.STATUS_ID' => 'ref.STATUS_ID',
'=ref.ENTITY_ID' => new SqlExpression('"STATUS"')
),
)
)
->setSelect(array('STATUS_NAME' => 'STATUSES.NAME'))
->setFilter(
array(
'ID' => $dealId,
)
)
->setOrder(array('DATE_CREATE' => 'ASC'));
$result = $query->exec();
while ($arLead = $result->fetch()) {
$arResult[] = $arLead;
}
Bitrix\Main\Application¶
<?php
use Bitrix\Main\Application;
$application = Application::getInstance();
$docRoot = Application::getDocumentRoot();
$connection = Application::getConnection();
Bitrix\Main\Context¶
<?php
use Bitrix\Main\Application;
use Bitrix\Main\Context;
$context = Application::getInstance()->getContext();
//$context = Context::getCurrent();
$request = $context->getRequest();
$server = $context->getServer();
$siteId = $context->getSite();
$langId = $context->getLanguage();
Bitrix\Main\Request¶
<?php
use Bitrix\Main\Context;
use Bitrix\Main\Request;
$context = Context::getCurrent();
$request = $context->getRequest();
//$request = Context::getCurrent()->getRequest();
$value = $request->get("param");
$value = $request["param"];
$value = $request->getQuery("param");
$values = $request->getQueryList();
$value = $request->getPost("param");
$values = $request->getPostList();
$value = $request->getFile("param");
$values = $request->getFileList();
$value = $request->getCookie("param");
$values = $request->getCookieList();
$method = $request->getRequestMethod();
$flag = $request->isGet();
$flag = $request->isPost();
$flag = $request->isAjaxRequest();
$flag = $request->isHttps();
$flag = $request->isAdminSection();
$requestUri = $request->getRequestUri();
$requestPage = $request->getRequestedPage();
$rDir = $request->getRequestedPageDirectory();
Bitrix\Main\Server¶
<?php
use Bitrix\Main\Context;
use Bitrix\Main\Server;
$context = Context::getCurrent();
$server = $context->getServer();
//$server = Context::getCurrent()->getServer();
$server->getDocumentRoot();
$server->getPersonalRoot();
$server->getHttpHost();
$server->getServerName();
$server->getServerAddr();
$server->getServerPort();
$server->getRequestUri();
$server->getRequestMethod();
$server->getPhpSelf();
$server->getScriptName();
$server->get('HTTP_ACCEPT');
Basket¶
Bitrix\Sale\Basket¶
<?php
use Bitrix\Sale;
//cur user basket
$basket = Sale\Basket::loadItemsForFUser(Sale\Fuser::getId(), Bitrix\Main\Context::getCurrent()->getSite());
//order's basket
$basket = Sale\Order::load($orderId)->getBasket();
$price = $basket->getPrice();
$fullPrice = $basket->getBasePrice();
$weight = $basket->getWeight();
//add to basket
if ($item = $basket->getExistsItem('catalog', $productId)) {
$item->setField('QUANTITY', $item->getQuantity() + $quantity);
} else {
$item = $basket->createItem('catalog', $productId);
$item->setFields(array(
'QUANTITY' => $quantity,
'CURRENCY' => \Bitrix\Currency\CurrencyManager::getBaseCurrency(),
'LID' => \Bitrix\Main\Context::getCurrent()->getSite(),
'PRODUCT_PROVIDER_CLASS' => 'CCatalogProductProvider',
));
}
$basket->save();
//delete basket item
$basket->getItemById($id)->delete();
$basket->save();
//CAN_BUY=Y items
$orderBasket = $basket->getOrderableItems();
Bitrix\Sale\BasketItem¶
<?php
use Bitrix\Sale;
$basketItems = $basket->getBasketItems();
foreach ($basket as $basketItem) {
echo $basketItem->getField('NAME') . ' - ' . $basketItem->getQuantity() . '<br />';
}
$item = $basketItems[0];
$item->getId();
$item->getProductId();
$item->getPrice();
$item->getQuantity();
$item->getFinalPrice();
$item->getWeight();
$item->getField('NAME');
$item->canBuy();
$item->isDelay();
$item->getPropertyCollection();
$item->getCollection();
//operations
$item->setField('QUANTITY', $quantity);
$item->setFields(array(
'QUANTITY' => $quantity,
'CUSTOM_PRICE' => $customPrice,
));
$item->delete();
$item->save(); //or $basket->save();
Order¶
Bitrix\Sale\Order¶
<?php
use Bitrix\Sale;
$order = Sale\Order::load($orderId);
//fields
$order->getId();
$order->getSiteId();
$order->getDateInsert();
$order->getPersonTypeId();
$order->getUserId();
$order->getPrice();
$order->getDiscountPrice();
$order->getDeliveryPrice();
$order->getSumPaid();
$order->getCurrency();
$order->isPaid();
$order->isAllowDelivery();
$order->isShipped();
$order->isCanceled();
$allFields = $order->getAvailableFields();
$order->getField("ORDER_WEIGHT");
$order->getField('PRICE');
//change
$order->setField('USER_DESCRIPTION', 'Comment');
$order->save();
//payments and deliveries
$paymentIds = $order->getPaymentSystemId();
$deliveryIds = $order->getDeliverySystemId();
//discounts
$discountData = $order->getDiscount()->getApplyResult();
//basket
$order->setBasket($basket);
$basket = $order->getBasket();
Bitrix\Sale\PropertyValue¶
<?php
$propertyCollection = $order->getPropertyCollection();
$props = $propertyCollection->getArray();
$propGroups = $propertyCollection->getGroups();
$propsFromGroup = $propertyCollection->getGroupProperties($groupId);
$emailPropValue = $propertyCollection->getUserEmail();
$namePropValue = $propertyCollection->getPayerName();
$locPropValue = $propertyCollection->getDeliveryLocation();
$taxLocPropValue = $propertyCollection->getTaxLocation();
$profNamePropVal = $propertyCollection->getProfileName();
$zipPropValue = $propertyCollection->getDeliveryLocationZip();
$phonePropValue = $propertyCollection->getPhone();
$addressPropValue = $propertyCollection->getAddress();
$somePropValue = $propertyCollection->getItemByOrderPropertyId($orderPropertyId);
$somePropValue->getValue();
$somePropValue->getViewHtml();
$arProp = $somePropValue->getProperty();
$propId = $somePropValue->getPropertyId();
$propName = $somePropValue->getName();
$isRequired = $somePropValue->isRequired();
$propPerson = $somePropValue->getPersonTypeId();
$propGroup = $somePropValue->getGroupId();
//update
$somePropValue->setValue("value");
$order->save();
Files and directories operations¶
Bitrix\Main\IO\File¶
<?php
use Bitrix\Main\IO;
use Bitrix\Main\Application;
$file = new IO\File(Application::getDocumentRoot() . "/file.txt");
//file info
$isExist = $file->isExists();
$dir = $file->getDirectory();
$dir = $file->getDirectoryName();
$fileName = $file->getName();
$fileExt = $file->getExtension();
$fileSize = $file->getSize();
$contentType = $file->getContentType();
$createdAt = $file->getCreationTime();
$accessAt = $file->getLastAccessTime();
$modifiedAt = $file->getModificationTime();
$perms = $file->getPermissions();
$perms = substr(sprintf('%o', $file->getPermissions()), -3);
//file operations
$content = $file->getContents();
$file->putContents("data");
$file->putContents("data", IO\File::APPEND);
$file->readFile();
$file->rename(Application::getDocumentRoot() . "/new_file.txt");
$file->delete();
Bitrix\Main\IO\Directory¶
<?php
use Bitrix\Main\IO;
use Bitrix\Main\Application;
$dir = new IO\Directory(Application::getDocumentRoot() . "/test/");
$dir->create();
//dir info
$isExist = $dir->isExists();
$createdAt = $dir->getCreationTime();
$accessAt = $dir->getLastAccessTime();
$modifiedAt = $dir->getModificationTime();
$perms = $dir->getPermissions();
$perms = substr(sprintf('%o', $dir->getPermissions()), -3);
//dir operations
$childDir = $dir->createSubdirectory("child");
$dir->rename(Application::getDocumentRoot() . "/another_path/");
$dir->delete();
$files = $dir->getChildren();
Bitrix\Main\IO\Path¶
<?php
use Bitrix\Main\IO;
use Bitrix\Main\Application;
$path = Application::getDocumentRoot() . "/some_dir/some_file.ext";
$fileExt = IO\Path::getExtension($path);
$fileName = IO\Path::getName($path);
$fileDir = IO\Path::getDirectory($path);
HTTP¶
<?php
use \Bitrix\Main\Application;
use \Bitrix\Main\Web\Uri;
use \Bitrix\Main\Web\HttpClient;
//URI
$uri = new Uri("http://username:password@example.com/some/path/?param1=value#comments");
$uri->getLocator();
$uri->getUri();
$uri->getHost();
$uri->getUser();
$uri->getPass();
$uri->getPath();
$uri->getPathQuery();
$uri->getPort();
$uri->getQuery();
$uri->getScheme();
$uri->deleteParams(array("param1"));
$uri->getUri();
$uri->addParams(array("param2" => "value"));
$uri->getUri();
//HTTP Client
$options = array(
"redirect" => true,
"redirectMax" => 5,
"waitResponse" => true,
"socketTimeout" => 30,
"streamTimeout" => 60,
"version" => HttpClient::HTTP_1_0, // HttpClient::HTTP_1_0 or HttpClient::HTTP_1_1
"proxyHost" => "",
"proxyPort" => "",
"proxyUser" => "",
"proxyPassword" => "",
"compress" => false, // Accept-Encoding: gzip
"charset" => "", //request body charset
"disableSslVerification" => false,
);
$httpClient = new HttpClient($options);
//headers
$name = "User-Agent";
$value = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A";
$httpClient->setHeader($name, $value, true);
//cookies
$cookies = array(
"name" => "value",
);
$httpClient->setCookies($cookies);
//basic auth
$httpClient->setAuthorization($user, $pass);
//send request
//methods - HttpClient::HTTP_GET, HttpClient::HTTP_POST, HttpClient::HTTP_PUT, HttpClient::HTTP_HEAD и HttpClient::HTTP_PATCH.
$httpClient->query($method, $url, $entityBody = null);
//result
$httpClient->getResult();
$httpClient->getStatus();
$httpClient->getContentType();
$httpClient->getEffectiveUrl();
$httpClient->getCookies();
$httpClient->getHeaders();
$httpClient->getError();
//download file
$httpClient->download('http://example.com/file.pdf', Application::getDocumentRoot() . '/upload/example/document.pdf');