Bitrix Site Manager

Main file of component

In this section

What is main file of component?

Main file of component is a PHP file implementing the component logic. In its turn, the main file can use language files to translate messages into various languages.

How to include

The component is attached by inclusion of the main file using the function CMain::IncludeFile:

Example of inclusion of component
<?
// component displaying the information block element details
$APPLICATION->IncludeFile(
    "iblock/catalog/element.php",              // path to the main file of component
    Array(                                     // array of component parameters

        "IBLOCK_TYPE"       => "catalog",      // information block type
        "IBLOCK_ID"         => "21",           // information block
        "ELEMENT_ID"        => $_REQUEST["ID"],// element ID
        "SECTION_URL"       => "section.php",  // URL of the page with section content
        "arrFIELD_CODE"     => Array(          // fields
             "DETAIL_TEXT",
             "DETAIL_PICTURE"),
        "arrPROPERTY_CODE"  => Array(          // properties
             "YEAR",
             "STANDBY_TIME",
             "TALKTIME",
             "WEIGHT",
             "STANDART",
             "SIZE",
             "BATTERY",
             "SCREEN",
             "WAP",
             "VIBRO",
             "VOICE",
             "PC",
             "MORE_PHOTO",
             "MANUAL"),
        "CACHE_TIME"        => "3600",         // cache time (in seconds)

        )
    );
?>

How to create

When creating the main file of component, each and all system functions can be used. But it should be considered that the main file is included using the function CMain::IncludeFile, which has its own scope of variables. Hence, there are only two ways to pass the parameters to component:

  1. The first way is to use the second parameter of the function CMain::IncludeFile. You can pass an associated array in this parameter, which will be further available as $arParams in the main file of component. Besides, the array $arParams will be transformed into a set of variables with the same names as the array keys and the same values as those of the corresponding array keys (for details, see description of the standard PHP function extract).
  2. The second way is to pass the parameters to component via the global arrays of variables. This way of parameter is quite the same as passing parameters to a common PHP function. You may use the standard PHP arrays:
    • $GLOBALS
    • $_REQUEST
    • $_GET
    • $_POST
    • $_SERVER
    • etc.
    as well as your own global arrays defined prior to including component.

    The following variables are defined as global in the function CMain::IncludeFile:

The variable scope confinement within the main component file stipulates special conditions of using certain functions. It refers to those functions which initialise the global variables. If these global variables will be further used within the main file of component, you have to address them via the standard PHP array $GLOBALS, or preliminary define these variables as global using the standard PHP directive global. Mainly, it refers to the following functions:

Example of the main file of component
<?
/**************************************************************************
                component displaying the information block element
                iblock/catalog/element.php
***************************************************************************/

if (CModule::IncludeModule("iblock")):
    
    IncludeTemplateLangFile(__FILE__);

    /*************************************************************************
                Initialise the original parameters of the component 
    *************************************************************************/

    /*
    $IBLOCK_TYPE        = $arParams["IBLOCK_TYPE"];       // information block type
    $IBLOCK_ID          = $arParams["IBLOCK_ID"];         // information block ID
    $ELEMENT_ID         = $arParams["ELEMENT_ID"];        // element ID
    $SECTION_URL        = $arParams["SECTION_URL"];       // URL of the page with section content
    $arrFIELD_CODE      = $arParams["arrFIELD_CODE"];     // an array of field ID's
    $arrPROPERTY_CODE   = $arParams["arrPROPERTY_CODE"];  // an array of property ID's
    $CACHE_TIME         = $arParams["CACHE_TIME"];        // cache time (in seconds)
    */

    $arrFIELD_CODE = (is_array($arrFIELD_CODE)) ? $arrFIELD_CODE : array();
    $arrPROPERTY_CODE = (is_array($arrPROPERTY_CODE)) ? $arrPROPERTY_CODE : array();
    $ELEMENT_ID = intval($ELEMENT_ID);

    /*************************************************************************
                                Using cache
    *************************************************************************/

    $obCache = new CPHPCache;
    $CACHE_ID = __FILE__.md5(serialize($arParams).$USER->GetGroups());
    if($obCache->InitCache($CACHE_TIME, $CACHE_ID, "/"))
    {
        $arVars = $obCache->GetVars();
        $ELEMENT_NAME  = $arVars["ELEMENT_NAME"];
        $IBLOCK_ID     = $arVars["IBLOCK_ID"];
        $SECTION_ID    = $arVars["SECTION_ID"];
        $SECTION_NAME  = $arVars["SECTION_NAME"];
        $KEYWORDS      = $arVars["KEYWORDS"];
        $DESCRIPTION   = $arVars["DESCRIPTION"];
        $arrPath       = $arVars["arrPath"];
        $found = "Y";
    }
    else
    {
        if ($ELEMENT_ID>0)
        {
            $arSelect = array(
                "ID",
                "NAME",
                "IBLOCK_ID",
                "IBLOCK_SECTION_ID"
                );
            if ($rsElement = GetIBlockElementListEx($IBLOCK_TYPE, 
                                                    false, false, array(), 
                                                    false, 
                                                    array("ID" => $ELEMENT_ID), 
                                                    $arSelect))
            {
                if ($obElement = $rsElement->GetNextElement()) 
                {
                    $found = "Y";
                    $arElement = $obElement->GetFields();            
                    $arProperty = $obElement->GetProperties();
                    $KEYWORDS = $arProperty["KEYWORDS"]["VALUE"];
                    $DESCRIPTION = $arProperty["DESCRIPTION"]["VALUE"];
                    $ELEMENT_NAME = $arElement["NAME"];
                    $IBLOCK_ID = $arElement["IBLOCK_ID"];
                    $SECTION_ID = $arElement["IBLOCK_SECTION_ID"];
                    if ($rsSection = CIBlockSection::GetByID($SECTION_ID))
                    {
                        $arSection = $rsSection->Fetch();
                        $SECTION_NAME = $arSection["NAME"];
                    }
                }
            }

            $arrPath = array();
            $rsPath = GetIBlockSectionPath($IBLOCK_ID, $SECTION_ID);
            while($arPath=$rsPath->GetNext()) 
                $arrPath[] = array("ID" => $arPath["ID"], 
                                   "NAME" => $arPath["NAME"]);
        }
    }

    // if the element is found
    if ($found == "Y"):

        $APPLICATION->SetPageProperty("keywords", $KEYWORDS);
        $APPLICATION->SetPageProperty("description", $DESCRIPTION);

        CIBlock::ShowPanel($IBLOCK_ID, $ELEMENT_ID, $SECTION_ID, $IBLOCK_TYPE);
        $APPLICATION->SetTitle($ELEMENT_NAME);
        if (is_array($arrPath))
        {
            while(list($key, $arS) = each($arrPath))
            {
                if ($SECTION_ID==$arS["ID"]) $SECTION_NAME = $arS["NAME"];
                $APPLICATION->AddChainItem($arS["NAME"], 
                                           $SECTION_URL."SECTION_ID=".$arS["ID"]);
            }
        }

        CIBlockElement::CounterInc($ELEMENT_ID);

        if($obCache->StartDataCache()):

            $arSelect = array(
                "ID",
                "IBLOCK_ID",
                "IBLOCK_SECTION_ID",
                "DETAIL_TEXT_TYPE",
                "PREVIEW_TEXT_TYPE",
                "DETAIL_PICTURE",
                "PREVIEW_PICTURE",
                );
            $arSelect = array_merge($arSelect, $arrFIELD_CODE);
            if ($rsElement = GetIBlockElementListEx($IBLOCK_TYPE, false, 
                                                    false, array(), false, 
                                                    array("ID" => $ELEMENT_ID), 
                                                    $arSelect)) :

                if ($obElement = $rsElement->GetNextElement()) :

                    $arElement = $obElement->GetFields();
                    $arProperty = $obElement->GetProperties();

                    /****************************************************************
                                            HTML form
                    ****************************************************************/
                    ?>
                    <table width="70%" border="0" cellspacing="0" cellpadding="7">
                        <tr>
                            <?
                            if (in_array("PREVIEW_PICTURE", $arrFIELD_CODE))
                            {
                                $image = intval($arElement["PREVIEW_PICTURE"])>0 ? 
                                    $arElement["PREVIEW_PICTURE"] : 
                                    $arElement["DETAIL_PICTURE"];
                            }
                            if (in_array("DETAIL_PICTURE", $arrFIELD_CODE))
                            {
                                $image = intval($arElement["DETAIL_PICTURE"])>0 ? 
                                    $arElement["DETAIL_PICTURE"] : 
                                    $arElement["PREVIEW_PICTURE"];
                            }
                            ?>
                        </tr>
                    </table>

                    <?if (strlen($arElement["PREVIEW_TEXT"])>0):?>
                    <font class="text"><p><?=$arElement["PREVIEW_TEXT"]?></p></font>
                    <?endif;?>

                    <?if (strlen($arElement["DETAIL_TEXT"])>0):?>
                    <font class="text"><p><?
                    echo ($arElement["DETAIL_TEXT_TYPE"]=="html") ? 
                              $arElement["DETAIL_TEXT"] : 
                              TxtToHTML($arElement["~DETAIL_TEXT"]);
                    ?></p></font>
                    <?endif;

                endif;
            endif;

            $obCache->EndDataCache(array(
                "ELEMENT_NAME"    => $ELEMENT_NAME,
                "IBLOCK_ID"        => $IBLOCK_ID,
                "SECTION_ID"    => $SECTION_ID,
                "SECTION_NAME"    => $SECTION_NAME,
                "KEYWORDS"        => $KEYWORDS,
                "DESCRIPTION"    => $DESCRIPTION,
                "arrPath"        => $arrPath
                ));
        endif;
    else:
            ShowError(GetMessage("CATALOG_ELEMENT_NOT_FOUND"));
    endif;
endif;
?>

Additional information