/**
 * Creates nodes that can be used to display the service configuration tree using the CTree class.
 *
 * @see CTree
 *
 * @param array $services
 * @param array $parentService
 * @param array $service
 * @param array $dependency
 * @param array $tree
 */
function createServiceConfigurationTree(array $services, &$tree, array $parentService = array(), array $service = array(), array $dependency = array())
{
    if (!$service) {
        $caption = new CLink(_('root'), '#');
        $caption->setMenuPopup(CMenuPopupHelper::getServiceConfiguration(null, _('root'), false));
        $serviceNode = array('id' => 0, 'parentid' => 0, 'caption' => $caption, 'trigger' => array(), 'algorithm' => SPACE, 'description' => SPACE);
        $service = $serviceNode;
        $service['serviceid'] = 0;
        $service['dependencies'] = array();
        $service['trigger'] = array();
        // add all top level services as children of "root"
        foreach ($services as $topService) {
            if (!$topService['parent']) {
                $service['dependencies'][] = array('servicedownid' => $topService['serviceid'], 'soft' => 0, 'linkid' => 0);
            }
        }
        $tree = array($serviceNode);
    } else {
        // caption
        $caption = new CLink($service['name'], '#');
        // service is deletable only if it has no hard dependency
        $deletable = true;
        foreach ($service['dependencies'] as $dep) {
            if ($dep['soft'] == 0) {
                $deletable = false;
                break;
            }
        }
        $caption->setMenuPopup(CMenuPopupHelper::getServiceConfiguration($service['serviceid'], $service['name'], $deletable));
        $serviceNode = array('id' => $service['serviceid'], 'caption' => $caption, 'description' => $service['trigger'] ? $service['trigger']['description'] : '-', 'parentid' => $parentService ? $parentService['serviceid'] : 0, 'algorithm' => serviceAlgorythm($service['algorithm']));
    }
    if (!$dependency || !$dependency['soft']) {
        $tree[$serviceNode['id']] = $serviceNode;
        foreach ($service['dependencies'] as $dependency) {
            $childService = $services[$dependency['servicedownid']];
            createServiceConfigurationTree($services, $tree, $service, $childService, $dependency);
        }
    } else {
        $serviceNode['caption'] = new CSpan($serviceNode['caption'], 'service-caption-soft');
        $tree[$serviceNode['id'] . '.' . $dependency['linkid']] = $serviceNode;
    }
}
// create table
$servicesParentTable = new CTableInfo();
$servicesParentTable->setHeader(array(_('Service'), _('Status calculation'), _('Trigger')));
$prefix = null;
// root
$description = new CLink(_('root'), '#', null, 'javascript:
	jQuery(\'#parent_name\', window.opener.document).val(' . zbx_jsvalue(_('root')) . ');
	jQuery(\'#parentname\', window.opener.document).val(' . zbx_jsvalue(_('root')) . ');
	jQuery(\'#parentid\', window.opener.document).val(' . zbx_jsvalue(0) . ');
	self.close();
	return false;');
$servicesParentTable->addRow(array(array($prefix, $description), _('Note'), '-'));
// others
foreach ($this->data['db_pservices'] as $db_service) {
    $description = new CSpan($db_service['name'], 'link');
    $description->setAttribute('onclick', 'javascript:
		jQuery(\'#parent_name\', window.opener.document).val(' . zbx_jsvalue($db_service['name']) . ');
		jQuery(\'#parentname\', window.opener.document).val(' . zbx_jsvalue($db_service['name']) . ');
		jQuery(\'#parentid\', window.opener.document).val(' . zbx_jsvalue($db_service['serviceid']) . ');
		self.close();
		return false;');
    $servicesParentTable->addRow(array(array($prefix, $description), serviceAlgorythm($db_service['algorithm']), $db_service['trigger']));
}
$column = new CCol(new CButton('cancel', _('Cancel'), 'javascript: self.close();'));
$column->setAttribute('style', 'text-align:right;');
$servicesParentTable->setFooter($column);
// append table to form
$servicesParentForm->addItem($servicesParentTable);
// append form to widget
$servicesParentWidget->addItem($servicesParentForm);
return $servicesParentWidget;
$servicesForm->addVar('parentid', $this->data['parentid']);
$servicesForm->addVar('parentname', $this->data['parentname']);
$servicesForm->addVar('triggerid', $this->data['triggerid']);
if (isset($this->data['service'])) {
    $servicesForm->addVar('serviceid', $this->data['service']['serviceid']);
}
// create form list
$servicesFormList = new CFormList('servicesFormList');
$nameTextBox = new CTextBox('name', $this->data['name'], ZBX_TEXTBOX_STANDARD_SIZE, false, 128);
$nameTextBox->attr('autofocus', 'autofocus');
$servicesFormList->addRow(_('Name'), $nameTextBox);
// append parent link to form list
$servicesFormList->addRow(_('Parent service'), array(new CTextBox('parent_name', $this->data['parentname'], ZBX_TEXTBOX_STANDARD_SIZE, true, 128), new CButton('select_parent', _('Change'), "javascript: openWinCentered('services.php?pservices=1" . url_param('serviceid') . "', 'ZBX_Services_List', 740, 420, 'scrollbars=1, toolbar=0, menubar=0, resizable=1, dialog=0');", 'formlist')));
// append algorithm to form list
$algorithmComboBox = new CComboBox('algorithm', $this->data['algorithm']);
$algorithmComboBox->addItems(serviceAlgorythm());
$servicesFormList->addRow(_('Status calculation algorithm'), $algorithmComboBox);
// append SLA to form list
$showslaCheckbox = new CCheckBox('showsla', $this->data['showsla'] == 0 ? 'no' : 'yes', null, 1);
$goodslaTextBox = new CTextBox('goodsla', $this->data['goodsla'], 6, false, 8);
if (!$this->data['showsla']) {
    $goodslaTextBox->setAttribute('disabled', 'disabled');
}
$servicesFormList->addRow(_('Calculate SLA, acceptable SLA (in %)'), array($showslaCheckbox, $goodslaTextBox));
// append trigger to form list
$servicesFormList->addRow(_('Trigger'), array(new CTextBox('trigger', $this->data['trigger'], ZBX_TEXTBOX_STANDARD_SIZE, true), new CButton('btn1', _('Select'), 'return PopUp("popup.php?' . 'dstfrm=' . $servicesForm->getName() . '&dstfld1=triggerid' . '&dstfld2=trigger' . '&srctbl=triggers' . '&srcfld1=triggerid' . '&srcfld2=description' . '&real_hosts=1' . '&with_triggers=1");', 'formlist')));
$servicesFormList->addRow(_('Sort order (0->999)'), new CTextBox('sortorder', $this->data['sortorder'], 3, false, 3));
/*
 * Dependencies tab
 */
$servicesChildTable = new CTable(_('No dependencies defined.'), 'formElementTable');
 /**
  * Validates the "algorithm" field. Assumes the "name" field is valid.
  *
  * @throws APIException if the name is missing or invalid
  *
  * @param array $service
  *
  * @return void
  */
 protected function checkAlgorithm(array $service)
 {
     if (!isset($service['algorithm']) || !serviceAlgorythm($service['algorithm'])) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect algorithm for service "%1$s".', $service['name']));
     }
 }
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/
include dirname(__FILE__) . '/js/configuration.services.child.list.js.php';
$servicesChildWidget = new CWidget();
$servicesChildWidget->addPageHeader(_('IT service dependencies'));
// create form
$servicesChildForm = new CForm();
$servicesChildForm->setName('servicesForm');
if (!empty($this->data['service'])) {
    $servicesChildForm->addVar('serviceid', $this->data['service']['serviceid']);
}
// create table
$servicesChildTable = new CTableInfo(_('No IT services found.'));
$servicesChildTable->setHeader(array(new CCheckBox('all_services', null, "javascript: checkAll('" . $servicesChildForm->getName() . "', 'all_services', 'services');"), _('Service'), _('Status calculation'), _('Trigger')));
$prefix = null;
foreach ($this->data['db_cservices'] as $service) {
    $description = new CLink($service['name'], '#', 'service-name');
    $description->setAttributes(array('id' => 'service-name-' . $service['serviceid'], 'data-name' => $service['name'], 'data-serviceid' => $service['serviceid'], 'data-trigger' => $service['trigger']));
    $cb = new CCheckBox('services[' . $service['serviceid'] . ']', null, null, $service['serviceid']);
    $cb->addClass('service-select');
    $servicesChildTable->addRow(array($cb, array($prefix, $description), serviceAlgorythm($service['algorithm']), $service['trigger']));
}
$servicesChildTable->setFooter(new CCol(new CButton('select', _('Select')), 'right'));
// append table to form
$servicesChildForm->addItem($servicesChildTable);
// append form to widget
$servicesChildWidget->addItem($servicesChildForm);
return $servicesChildWidget;