Documentation

Example: creating a custom list of elements

A detailed description of creating a Control Panel report page can be found here.

Example

<?php
use Bitrix\Main\Loader;
use Bitrix\Main\Localization\Loc;


// include all necessary files:
// the first common prologue require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php"); // initialise the module require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/subscribe/include.php"); // the module prologue require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/subscribe/prolog.php");

// include a language file IncludeModuleLangFile(__FILE__); // obtain the current user access level for the module $POST_RIGHT = $APPLICATION->GetGroupRight("subscribe"); // if access if denied, direct the user to the autorisation form
if ($POST_RIGHT == "D")     $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED")); $sTableID = "tbl_rubric"; // the table ID $oSort = new CAdminSorting($sTableID, "ID", "desc"); // the object to be sorted $lAdmin = new CAdminList($sTableID, $oSort); // the main list object // ******************************************************************** // // FILTER // // ******************************************************************** // // *********************** CheckFilter ******************************** // // move filter validation to a separate function function CheckFilter() { global $FilterArr, $lAdmin; foreach ($FilterArr as $f) global $$f; // In this case, no check is required. // In a common case, the value of $find_name is be checked // and if an error occurs, it should be passed to a handler // using $lAdmin->AddFilterError('error_text').   // return false if an error occurs;
return count($lAdmin->arFilterErrors)==0; } // *********************** /CheckFilter ******************************* // // describe filter elements $FilterArr = Array( "find", "find_type", "find_id", "find_lid", "find_active", "find_visible", "find_auto", ); // initialise the filter $lAdmin->InitFilter($FilterArr); // if all filter values are correct, process it
if (CheckFilter()) { // create a filter array to invoke CRubric::GetList() $arFilter = Array( "ID" => ($find!="" && $find_type == "id"? $find:$find_id), "LID" => $find_lid, "ACTIVE" => $find_active, "VISIBLE" => $find_visible, "AUTO" => $find_auto, ); } // ******************************************************************** // // HANDLE LIST ELEMENTS ACTIONS // ******************************************************************** // // save edited elements if($lAdmin->EditAction() && $POST_RIGHT=="W") { // walk the list of elements foreach($FIELDS as $ID=>$arFields) { if(!$lAdmin->IsUpdated($ID)) continue; // save changes of each element $DB->StartTransaction(); $ID = IntVal($ID); $cData = new CRubric; if(($rsData = $cData->GetByID($ID)) && ($arData = $rsData->Fetch())) { foreach($arFields as $key=>$value) $arData[$key]=$value; if(!$cData->Update($ID, $arData)) { $lAdmin->AddGroupError(GetMessage("rub_save_error")." ".$cData->LAST_ERROR, $ID); $DB->Rollback(); } } else { $lAdmin->AddGroupError(GetMessage("rub_save_error")." ".GetMessage("rub_no_rubric"), $ID); $DB->Rollback(); } $DB->Commit(); } } // handle single and group actions if(($arID = $lAdmin->GroupAction()) && $POST_RIGHT=="W") { // if "For all elements" is checked if($_REQUEST['action_target']=='selected') { $cData = new CRubric; $rsData = $cData->GetList(array($by=>$order), $arFilter); while($arRes = $rsData->Fetch()) $arID[] = $arRes['ID']; } // walk the list of elements foreach($arID as $ID) { if(strlen($ID)<=0) continue; $ID = IntVal($ID); // perform the required action for each element switch($_REQUEST['action']) { // delete case "delete": @set_time_limit(0); $DB->StartTransaction(); if(!CRubric::Delete($ID)) { $DB->Rollback(); $lAdmin->AddGroupError(GetMessage("rub_del_err"), $ID); } $DB->Commit(); break; // activate/deactivate case "activate": case "deactivate": $cData = new CRubric; if(($rsData = $cData->GetByID($ID)) && ($arFields = $rsData->Fetch())) { $arFields["ACTIVE"]=($_REQUEST['action']=="activate"?"Y":"N"); if(!$cData->Update($ID, $arFields)) $lAdmin->AddGroupError(GetMessage("rub_save_error").$cData->LAST_ERROR, $ID); } else $lAdmin->AddGroupError(GetMessage("rub_save_error")." ".GetMessage("rub_no_rubric"), $ID); break; } } } // ******************************************************************** // // EXTRACT LIST ELEMENTS
// ******************************************************************** // // get the list of mail lists $cData = new CRubric; $rsData = $cData->GetList(array($by=>$order), $arFilter); // create an instance of CAdminResult from the list $rsData = new CAdminResult($rsData, $sTableID); // initialise pagewise navigation similar to CDBResult. $rsData->NavStart(); // print the page switcher to $lAdmin $lAdmin->NavText($rsData->GetNavPrint(GetMessage("rub_nav"))); // ******************************************************************** // // PREPARE LIST FOR OUTPUT // ******************************************************************** // $lAdmin->AddHeaders(array( array( "id" =>"ID", "content" =>"ID", "sort" =>"id", "align" =>"right", "default" =>true, ), array( "id" =>"NAME", "content" =>GetMessage("rub_name"), "sort" =>"name", "default" =>true, ), array( "id" =>"LID", "content" =>GetMessage("rub_site"), "sort" =>"lid", "default" =>true, ), array( "id" =>"SORT", "content" =>GetMessage("rub_sort"), "sort" =>"sort", "align" =>"right", "default" =>true, ), array( "id" =>"ACTIVE", "content" =>GetMessage("rub_act"), "sort" =>"act", "default" =>true, ), array( "id" =>"VISIBLE", "content" =>GetMessage("rub_visible"), "sort" =>"visible", "default" =>true, ), array( "id" =>"AUTO", "content" =>GetMessage("rub_auto"), "sort" =>"auto", "default" =>true, ), array( "id" =>"LAST_EXECUTED", "content" =>GetMessage("rub_last_exec"), "sort" =>"last_executed", "default" =>true, ), )); while($arRes = $rsData->NavNext(true, "f_")): // create a row. the result is an instance of CAdminListRow $row =& $lAdmin->AddRow($f_ID, $arRes); // next, configure the value display format for viewing and editing // the NAME parameter is edited as text and displayed as link $row->AddInputField("NAME", array("size"=>20)); $row->AddViewField("NAME", '<a href="rubric_edit.php?id='.$f_id.'&lang='.lang.'">'.$f_name.'</a>'); // the LID parameter is edited as a drop down list $row->AddEditField("LID", CLang::SelectBox("LID", $f_LID)); // the SORT parameter is edited as text $row->AddInputField("SORT", array("size"=>20)); // the ACTIVE and VISIBLE flags are edited as checkboxes $row->AddCheckField("ACTIVE"); $row->AddCheckField("VISIBLE"); // the AUTO parameter is displayed as Yes or No text in bold in editing mode $row->AddViewField("AUTO", $f_AUTO=="Y"?GetMessage("POST_U_YES"):GetMessage("POST_U_NO")); $row->AddEditField("AUTO", "<b>".($f_AUTO=="Y"?GetMessage("POST_U_YES"):GetMessage("POST_U_NO"))."</b>"); // build the context menu $arActions = Array(); // edit the element $arActions[] = array( "ICON"=>"edit", "DEFAULT"=>true, "TEXT"=>GetMessage("rub_edit"), "ACTION"=>$lAdmin->ActionRedirect("rubric_edit.php?ID=".$f_ID) ); // delete the element if ($POST_RIGHT>="W") $arActions[] = array( "ICON"=>"delete", "TEXT"=>GetMessage("rub_del"), "ACTION"=>"if(confirm('".GetMessage('rub_del_conf')."')) ".$lAdmin->ActionDoGroup($f_ID, "delete") ); // insert a separator $arActions[] = array("SEPARATOR"=>true); // check the automated mail lists if (strlen($f_TEMPLATE)>0 && $f_AUTO=="Y") $arActions[] = array( "ICON"=>"", "TEXT"=>GetMessage("rub_check"), "ACTION"=>$lAdmin->ActionRedirect("template_test.php?ID=".$f_ID) ); // if the separator comes last, remove it if(is_set($arActions[count($arActions)-1], "SEPARATOR")) unset($arActions[count($arActions)-1]); // apply the context menu to the row $row->AddActions($arActions); endwhile; // the table footer $lAdmin->AddFooter( array( array("title"=>GetMessage("MAIN_ADMIN_LIST_SELECTED"),
          "value"=>$rsData->SelectedRowsCount()), // number of elements array("counter"=>true,
          "title"=>GetMessage("MAIN_ADMIN_LIST_CHECKED"),
          "value"=>"0"), // counter of selected elements ) ); // group actions
$lAdmin->AddGroupActionTable(Array( "delete"=>GetMessage("MAIN_ADMIN_LIST_DELETE"), // delete selected elements "activate"=>GetMessage("MAIN_ADMIN_LIST_ACTIVATE"), // activate selected elements "deactivate"=>GetMessage("MAIN_ADMIN_LIST_DEACTIVATE"), // deactivate selected elements )); // ******************************************************************** // // CONTROL PANEL MENU
// ******************************************************************** // // create a menu item containing only one item: Create Newsletter $aContext = array( array( "TEXT"=>GetMessage("POST_ADD"), "LINK"=>"rubric_edit.php?lang=".LANG, "TITLE"=>GetMessage("POST_ADD_TITLE"), "ICON"=>"btn_new", ), ); // and attach it to the list $lAdmin->AddAdminContextMenu($aContext); // ******************************************************************** // // OUTPUT // ******************************************************************** // // enable export $lAdmin->CheckListMode(); // specify page title $APPLICATION->SetTitle(GetMessage("rub_title")); // remember to separate data preparation from display require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php"); // ******************************************************************** // // FILTER OUTPUT
// ******************************************************************** // // create a filter object $oFilter = new CAdminFilter( $sTableID."_filter", array( "ID", GetMessage("rub_f_site"), GetMessage("rub_f_active"), GetMessage("rub_f_public"), GetMessage("rub_f_auto"), ) ); ?> <form name="find_form" method="get" action="<?echo $APPLICATION->GetCurPage();?>"> <?$oFilter->Begin();?> <tr> <td><b><?=GetMessage("rub_f_find")?>:</b></td> <td> <input type="text" size="25" name="find" value="<?echo htmlspecialchars($find)?>" title="<?=GetMessage("rub_f_find_title")?>"> <? $arr = array( "reference" => array( "ID", ), "reference_id" => array( "id", ) ); echo SelectBoxFromArray("find_type", $arr, $find_type, "", ""); ?> </td> </tr> <tr> <td><?="ID"?>:</td> <td> <input type="text" name="find_id" size="47" value="<?echo htmlspecialchars($find_id)?>"> </td> </tr> <tr> <td><?=GetMessage("rub_f_site").":"?></td> <td><input type="text" name="find_lid" size="47" value="<?echo htmlspecialchars($find_lid)?>"></td> </tr> <tr> <td><?=GetMessage("rub_f_active")?>:</td> <td> <? $arr = array( "reference" => array( GetMessage("POST_YES"), GetMessage("POST_NO"), ), "reference_id" => array( "Y", "N", ) ); echo SelectBoxFromArray("find_active", $arr, $find_active, GetMessage("POST_ALL"), ""); ?> </td> </tr> <tr> <td><?=GetMessage("rub_f_public")?>:</td> <td><?echo SelectBoxFromArray("find_visible", $arr, $find_visible, GetMessage("POST_ALL"), "");?></td> </tr> <tr> <td><?=GetMessage("rub_f_auto")?>:</td> <td><?echo SelectBoxFromArray("find_auto", $arr, $find_auto, GetMessage("POST_ALL"), "");?></td> </tr> <? $oFilter->Buttons(array("table_id"=>$sTableID,"url"=>$APPLICATION->GetCurPage(),"form"=>"find_form")); $oFilter->End(); ?> </form> <? // display the table $lAdmin->DisplayList(); ?> <? // close the table require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_admin.php"); ?>
© «Bitrix24», 2001-2024