public function index()
 {
     $config = CreateObject('phpgwapi.config', 'rental');
     $config->read();
     $use_fellesdata = $config->config_data['use_fellesdata'];
     if (isset($_POST['search'])) {
         $username = phpgw::get_var('username');
         if (!isset($username)) {
             $msglog['error'][] = array('msg' => lang('lacking_username'));
         } else {
             if ($username == $GLOBALS['phpgw_info']['user']['account_lid']) {
                 $msglog['error'][] = array('msg' => lang('searching_for_self'));
             } else {
                 $account_id = frontend_bofrontend::delegate_exist($username);
                 if ($account_id) {
                     $search = frontend_bofrontend::get_account_info($account_id);
                     $msglog['message'][] = array('msg' => lang('user_found_in_PE'));
                 } else {
                     if ($use_fellesdata) {
                         $fellesdata_user = frontend_bofellesdata::get_instance()->get_user($username);
                         if ($fellesdata_user) {
                             $search = $fellesdata_user;
                             $msglog['message'][] = array('msg' => lang('user_found_in_Fellesdata'));
                         }
                     } else {
                         $msglog['error'][] = array('msg' => lang('no_hits'));
                     }
                 }
             }
         }
     } else {
         if (isset($_POST['add'])) {
             $account_id = phpgw::get_var('account_id');
             $success = false;
             if ($use_fellesdata) {
                 $org_units = frontend_bofellesdata::get_instance()->get_result_units($GLOBALS['phpgw_info']['user']['account_lid']);
                 //Parameter to delegate access to only a single organisational unit
                 $org_unit_id = $this->header_state['selected_org_unit'];
                 $success = true;
                 foreach ($org_units as $org_unit) {
                     if ($org_unit_id == 'all' || $org_unit['ORG_UNIT_ID'] == $org_unit_id) {
                         //$curr_success = true;
                         $res = $this->add_delegate($account_id, $org_unit['ORG_UNIT_ID'], $org_unit['ORG_NAME']);
                         if ($res) {
                             //$mail_contents[] = $res;
                             $org_unit_names[] = $org_unit['ORG_NAME'];
                         } else {
                             $msglog['error'][] = array('msg' => lang('error_delegating_unit', $org_unit['ORG_NAME']));
                         }
                         $success = $success && $res;
                     }
                 }
             }
             if ($success) {
                 //Retrieve the usernames
                 $user_account = $GLOBALS['phpgw']->accounts->get($account_id);
                 $owner_account = $GLOBALS['phpgw']->accounts->get($GLOBALS['phpgw_info']['user']['account_id']);
                 $user_name = $user_account->__get('lid');
                 $owner_name = $owner_account->__get('lid');
                 $org_name_string = implode(',', $org_unit_names);
                 //If the usernames are set retrieve account data from Fellesdata
                 if (isset($user_name) && $user_name != '' && $owner_name && $owner_name != '') {
                     $fellesdata_user = frontend_bofellesdata::get_instance()->get_user($user_name);
                     $fellesdata_owner = frontend_bofellesdata::get_instance()->get_user($owner_name);
                     if ($fellesdata_user && $fellesdata_owner) {
                         //Send email notification to delegate
                         $email = $fellesdata_user['email'];
                         if (isset($email) && $email != '') {
                             $title = lang('email_add_delegate_title');
                             $message = lang('email_add_delegate_message', $fellesdata_user['firstname'], $fellesdata_user['lastname'], $fellesdata_owner['firstname'], $fellesdata_owner['lastname'], $org_name_string);
                             frontend_bofrontend::send_system_message($email, $title, $message);
                         }
                     }
                 }
                 $msglog['message'][] = array('msg' => lang('delegation_successful'));
                 /*					
                 					//send e-mail to user
                 					$user_account = $GLOBALS['phpgw']->accounts->get($account_id);
                 					$user_name = $user_account->__get('lid');
                 					$fellesdata_user = frontend_bofellesdata::get_instance()->get_user($user_name);
                 					if($fellesdata_user)
                 					{
                 						$email = $fellesdata_user['email'];
                 						if(isset($email) && $email != '')
                 						{
                 							$title = lang('email_add_delegate_title');
                 							$mail_content = implode(',',$mail_contents);
                 							frontend_bofrontend::send_system_message($email,$title,$mail_content);
                 						}
                 					}*/
             } else {
                 $msglog['error'][] = array('msg' => lang('delegation_error'));
             }
         } else {
             if (isset($_POST['remove'])) {
                 $account_id = phpgw::get_var('account_id');
                 $result = frontend_bofrontend::remove_delegate($account_id, null, null);
                 if ($result) {
                     $msglog['message'][] = array('msg' => lang('remove_delegate_successful'));
                 } else {
                     $msglog['error'][] = array('msg' => lang('remove_delegate_error'));
                 }
             } else {
                 if (isset($_POST['remove_specific'])) {
                     $account_id = phpgw::get_var('account_id');
                     //Parameter to delegate access to only a single organisational unit
                     $org_unit_id = $this->header_state['selected_org_unit'];
                     $result = frontend_bofrontend::remove_delegate($account_id, null, $org_unit_id);
                     if ($result) {
                         $msglog['message'][] = array('msg' => lang('remove_delegate_successful'));
                     } else {
                         $msglog['error'][] = array('msg' => lang('remove_delegate_error'));
                     }
                 }
             }
         }
     }
     $form_action = $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'frontend.uidelegates.index'));
     $delegates_per_org_unit = frontend_bofrontend::get_delegates($this->header_state['selected_org_unit']);
     $delegates_per_user = frontend_bofrontend::get_delegates(null, true);
     $number_of_delegates = count($delegates_per_org_unit);
     $number_of_user_delegates = count($delegates_per_user);
     $config = CreateObject('phpgwapi.config', 'frontend');
     $config->read();
     $delegateLimit = $config->config_data['delegate_limit'];
     if (!is_numeric($delegateLimit)) {
         $delegateLimit = 3;
     }
     $error_message = lang('max_x_delegates', $delegateLimit);
     $data = array('header' => $this->header_state, 'tabs' => $this->tabs, 'delegate_data' => array('form_action' => $form_action, 'delegate' => $delegates_per_org_unit, 'user_delegate' => $delegates_per_user, 'number_of_delegates' => isset($number_of_delegates) ? $number_of_delegates : 0, 'number_of_user_delegates' => isset($number_of_user_delegates) ? $number_of_user_delegates : 0, 'search' => isset($search) ? $search : array(), 'msgbox_data' => $GLOBALS['phpgw']->common->msgbox($GLOBALS['phpgw']->common->msgbox_data($msglog)), 'delegate_limit' => $delegateLimit, 'error_message' => $error_message));
     $GLOBALS['phpgw']->xslttpl->set_var('phpgw', array('app_data' => $data));
     $GLOBALS['phpgw']->xslttpl->add_file(array('frontend', 'delegate'));
 }
 public function edit()
 {
     $GLOBALS['phpgw_info']['flags']['app_header'] .= '::' . lang('edit');
     $config = CreateObject('phpgwapi.config', 'rental');
     $config->read();
     $use_fellesdata = $config->config_data['use_fellesdata'];
     $unit_id = (int) phpgw::get_var('id');
     $unit_level = (int) phpgw::get_var('level');
     if (isset($unit_id) && $unit_id > 0 && $use_fellesdata) {
         $msglog['error']['msg'] = phpgw::get_var('error');
         $msglog['message']['msg'] = phpgw::get_var('message');
         if (isset($_POST['search'])) {
             $username = phpgw::get_var('username');
             if (!isset($username)) {
                 $msglog['error']['msg'] = lang('lacking_username');
             } else {
                 $account_id = frontend_bofrontend::delegate_exist($username);
                 if ($account_id) {
                     $search_result = frontend_bofrontend::get_account_info($account_id);
                     $msglog['message']['msg'] = lang('user_found_in_PE');
                 } else {
                     $fellesdata_user = frontend_bofellesdata::get_instance()->get_user($username);
                     if ($fellesdata_user) {
                         $search_result = $fellesdata_user;
                         $msglog['message']['msg'] = lang('user_found_in_Fellesdata');
                     } else {
                         $msglog['error']['msg'] = lang('no_hits');
                     }
                 }
             }
         } else {
             if (isset($_POST['add'])) {
                 $account_id = phpgw::get_var('account_id');
                 //var_dump($account_id);
                 $bofelles = rental_bofellesdata::get_instance();
                 $unit = $bofelles->get_result_unit($unit_id, $unit_level);
                 //var_dump($unit);
                 if ($account_id) {
                     $res = $this->add_delegate($account_id, $unit['ORG_UNIT_ID'], $unit['ORG_NAME']);
                     //var_dump($res);
                     if (!$res) {
                         $msglog['error']['msg'] = lang('delegation_error');
                     } else {
                         $msglog['message']['msg'] = lang('delegation_successful');
                     }
                 } else {
                     $msglog['error']['msg'] = lang('unknown_user');
                 }
             }
         }
         $bofelles = rental_bofellesdata::get_instance();
         $unit = $bofelles->get_result_unit_with_leader($unit_id, $unit_level);
         $delegates_per_org_unit = frontend_bofrontend::get_delegates($unit_id);
         $unit['UNIT_NO_OF_DELEGATES'] = count($delegates_per_org_unit);
         $form_action = $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'rental.uiresultunit.edit', 'id' => $unit_id, 'level' => $unit_level));
         $this->render('resultunit.php', array('unit' => $unit, 'form_action' => $form_action, 'search_result' => isset($search_result) ? $search_result : array(), 'msglog' => $msglog, 'cancel_link' => self::link(array('menuaction' => 'rental.uiresultunit.index', 'populate_form' => 'yes'))));
     }
 }