public function edit()
 {
     $GLOBALS['phpgw_info']['flags']['app_header'] .= '::' . lang('edit');
     // Get the contract part id
     $arena_id = (int) phpgw::get_var('id');
     $cancel_link = self::link(array('menuaction' => 'activitycalendar.uiarena.index'));
     $buildings = activitycalendar_soarena::get_instance()->get_buildings();
     //var_dump($buildings);
     // Retrieve the arena object or create a new one
     if (isset($arena_id) && $arena_id > 0) {
         $arena = activitycalendar_soarena::get_instance()->get_single($arena_id);
     } else {
         $arena = new activitycalendar_arena();
     }
     if (isset($_POST['save_arena'])) {
         if (isset($arena)) {
             // ... set all parameters
             $arena->set_internal_arena_id(phpgw::get_var('internal_arena_id'));
             $arena->set_arena_name(phpgw::get_var('arena_name'));
             $arena->set_address(phpgw::get_var('address'));
             $arena->set_addressnumber(phpgw::get_var('address_no'));
             $arena->set_zip_code(phpgw::get_var('zip_code'));
             $arena->set_city(phpgw::get_var('city'));
             $arena->set_active(phpgw::get_var('arena_active') == 'yes' ? true : false);
             if (activitycalendar_soarena::get_instance()->store($arena)) {
                 $message = lang('messages_saved_form');
             } else {
                 $error = lang('messages_form_error');
             }
         }
         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'activitycalendar.uiarena.view', 'id' => $arena->get_id(), 'saved_ok' => 'yes'));
     }
     return $this->render('arena.php', array('arena' => $arena, 'buildings' => $buildings, 'editable' => true, 'cancel_link' => $cancel_link, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error')));
 }
 /**
  * Populates an arena-object
  * 
  *  @param int $arena_id
  *  @param activitycalendar_arena $arena
  *  @return activitycalendar_arena $arena
  */
 protected function populate(int $arena_id, &$arena)
 {
     if ($arena == null) {
         $arena = new activitycalendar_arena((int) $arena_id);
         $arena->set_arena_name($this->unmarshal($this->db->f('arena_name'), 'string'));
         $arena->set_address($this->unmarshal($this->db->f('address'), 'string'));
         $arena->set_addressnumber($this->unmarshal($this->db->f('addressnumber'), 'string'));
         $arena->set_zip_code($this->unmarshal($this->db->f('zip_code'), 'string'));
         $arena->set_city($this->unmarshal($this->db->f('city'), 'string'));
         $arena->set_internal_arena_id($this->unmarshal($this->db->f('internal_arena_id'), 'int'));
         $arena->set_active($this->unmarshal($this->db->f('active'), 'bool'));
     }
     return $arena;
 }
 /**
  * Public method. Add new activity.
  */
 public function add()
 {
     //$GLOBALS['phpgw']->redirect_link('/activitycalendarfrontend/index.php', array('menuaction' => 'activitycalendarfrontend.uiactivity.edit', 'action' => 'new_activity'));
     $GLOBALS['phpgw']->js->validate_file('json', 'json', 'phpgwapi');
     $c = createobject('phpgwapi.config', 'activitycalendarfrontend');
     $c->read();
     $config = $c->config_data;
     $ajaxUrl = $c->config_data['AJAXURL'];
     $categories = $this->so_activity->get_categories();
     $targets = $this->so_activity->get_targets();
     $offices = $this->so_activity->select_district_list();
     $districts = $this->so_activity->get_districts();
     $buildings = $this->so_arena->get_buildings();
     $arenas = $this->so_arena->get(null, null, 'arena.arena_name', true, null, null, null);
     $organizations = $this->so_organization->get(null, null, 'org.name', true, null, null, null);
     $activity = new activitycalendar_activity();
     $o_id = phpgw::get_var('organization_id');
     $o_id_new = phpgw::get_var('organization_id_hidden');
     if (isset($_POST['step_1'])) {
         //activity shall be registred on a new organization
         if ($o_id_new == "new_org") {
             //add new organization to internal activitycalendar organization register
             $org_homepage = phpgw::get_var('homepage');
             if ($org_homepage == 'http://') {
                 $org_homepage = "";
             }
             $org_info['name'] = phpgw::get_var('orgname');
             $org_info['orgnr'] = phpgw::get_var('orgno');
             $org_info['homepage'] = $org_homepage;
             $org_info['street'] = phpgw::get_var('address');
             $org_info['streetnumber'] = phpgw::get_var('number');
             $org_info['zip'] = phpgw::get_var('postzip');
             $org_info['postaddress'] = phpgw::get_var('postaddress');
             $org_info['status'] = "new";
             $o_id = $this->so_activity->add_organization_local($org_info);
             //add contact persons
             $contact1 = array();
             $contact1['name'] = phpgw::get_var('org_contact1_name');
             $contact1['phone'] = phpgw::get_var('org_contact1_phone');
             $contact1['mail'] = phpgw::get_var('org_contact1_mail');
             $contact1['org_id'] = $o_id;
             $contact1['group_id'] = 0;
             $this->so_activity->add_contact_person_local($contact1);
             $person_arr = $this->so_contact->get_local_contact_persons($o_id);
             foreach ($person_arr as $p) {
                 $persons[] = $p;
             }
             $person_ids = $this->so_organization->get_contacts_local($o_id);
             $desc = phpgw::get_var('org_description');
             $organization = $this->so_organization->get_organization_local($o_id);
             $new_org = true;
             $organization = $this->so_organization->get_organization_local($o_id);
             $person_arr = $this->so_organization->get_contacts_local_as_objects($o_id);
             foreach ($person_arr as $p) {
                 //var_dump($p);
                 $persons[] = $p;
             }
             $message = lang('organization_saved_form');
             $this->render('activity_new.php', array('activity' => $activity, 'new_organization' => true, 'organization' => $organization, 'contact1' => $persons[0], 'arenas' => $arenas, 'buildings' => $buildings, 'categories' => $categories, 'targets' => $targets, 'districts' => $districts, 'offices' => $offices, 'editable' => true, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error'), 'ajaxURL' => $ajaxUrl));
         } else {
             $new_org = false;
             $organization = $this->so_organization->get_single($o_id);
             $person_arr = $this->so_contact->get(null, null, null, null, null, null, array('organization_id' => $o_id));
             foreach ($person_arr as $p) {
                 //var_dump($p);
                 $persons[] = $p;
             }
             $activity->set_organization_id($o_id);
             $activity->set_description($organization->get_description());
             $activity->set_contact_persons($pers);
             $this->render('activity_new.php', array('activity' => $activity, 'new_organization' => false, 'organization' => $organization, 'contact1' => $persons[0], 'arenas' => $arenas, 'buildings' => $buildings, 'categories' => $categories, 'targets' => $targets, 'districts' => $districts, 'offices' => $offices, 'editable' => true, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error'), 'ajaxURL' => $ajaxUrl));
         }
     } else {
         if (isset($_POST['save_activity'])) {
             $get_org_from_local = false;
             $new_org_group = false;
             $new_org = phpgw::get_var('new_organization');
             if ($new_org != null && $new_org == 'yes') {
                 $get_org_from_local = true;
             }
             if ($get_org_from_local) {
                 $activity->set_new_org(true);
                 //$person_arr = $this->so_contact->get_local_contact_persons($o_id);
                 //foreach($person_arr as $p)
                 //{
                 //$persons[] = $p;
                 //}
                 //$person_ids = $this->so_organization->get_contacts_local($o_id);
                 //$desc = $this->so_organization->get_description_local($o_id);
                 $organization = $this->so_organization->get_organization_local($o_id);
                 $new_org = true;
                 //$new_org_group = true;
                 //Add new group for the activity
                 $group_info['name'] = phpgw::get_var('title');
                 $group_info['organization_id'] = $o_id;
                 $group_info['description'] = phpgw::get_var('description');
                 $group_info['status'] = "new";
                 $g_id = $this->so_activity->add_group_local($group_info);
                 //add contact persons
                 $contact1 = array();
                 $contact1['name'] = phpgw::get_var('contact_name');
                 $contact1['phone'] = phpgw::get_var('contact_phone');
                 $contact1['mail'] = phpgw::get_var('contact_mail');
                 $contact1['org_id'] = $o_id;
                 $contact1['group_id'] = $g_id;
                 $this->so_activity->add_contact_person_local($contact1);
                 $person_arr = $this->so_contact->get_local_contact_persons($g_id, true);
                 foreach ($person_arr as $p) {
                     $persons[] = $p;
                 }
                 $desc = phpgw::get_var('description');
                 $group = $this->so_group->get_group_local($g_id);
                 $person_ids = $this->so_group->get_contacts_local($g_id);
                 $new_group = true;
             } else {
                 if (is_numeric($o_id) && $o_id > 0) {
                     $group_info['name'] = phpgw::get_var('title');
                     $group_info['organization_id'] = $o_id;
                     $group_info['description'] = phpgw::get_var('description');
                     $group_info['status'] = "new";
                     $g_id = $this->so_activity->add_group_local($group_info);
                     //add contact persons
                     $contact1 = array();
                     $contact1['name'] = phpgw::get_var('contact_name');
                     $contact1['phone'] = phpgw::get_var('contact_phone');
                     $contact1['mail'] = phpgw::get_var('contact_mail');
                     $contact1['org_id'] = 0;
                     $contact1['group_id'] = $g_id;
                     $this->so_activity->add_contact_person_local($contact1);
                     $person_arr = $this->so_contact->get_local_contact_persons($g_id, true);
                     foreach ($person_arr as $p) {
                         $persons[] = $p;
                     }
                     $desc = phpgw::get_var('description');
                     $group = $this->so_group->get_group_local($g_id);
                     $person_ids = $this->so_group->get_contacts_local($g_id);
                     $organization = $this->so_organization->get_single($o_id);
                     $new_group = true;
                 }
             }
             if (strlen($desc) > 254) {
                 $desc = substr($desc, 0, 254);
             }
             $arena_id = phpgw::get_var('internal_arena_id');
             $new_arena = phpgw::get_var('new_arena_hidden');
             if ($new_arena != null && $new_arena == 'new_arena') {
                 $arena = new activitycalendar_arena();
                 $arena_name = phpgw::get_var('arena_name');
                 $arena_address = phpgw::get_var('arena_address');
                 $arena_addressnumber = phpgw::get_var('arena_number');
                 $arena_zip_code = phpgw::get_var('arena_zip_code');
                 $arena_city = phpgw::get_var('arena_city');
                 $arena->set_arena_name($arena_name);
                 $arena->set_address($arena_address);
                 $arena->set_addressnumber($arena_addressnumber);
                 $arena->set_zip_code($arena_zip_code);
                 $arena->set_city($arena_city);
                 $arena->set_active(true);
                 // All is good, store arena
                 if ($this->so_arena->store($arena)) {
                     $arena_id = $arena->get_id();
                     $activity->set_arena($arena_id);
                 }
             } else {
                 $arena_arr = explode("_", $arena_id);
                 if ($arena_arr[0] == 'i') {
                     $activity->set_internal_arena($arena_arr[1]);
                 } else {
                     $activity->set_arena($arena_arr[1]);
                 }
             }
             //... set all parameters
             $activity->set_title(phpgw::get_var('title'));
             $activity->set_organization_id($o_id);
             $activity->set_group_id($g_id);
             $activity->set_district(phpgw::get_var('district'));
             $activity->set_office(phpgw::get_var('office'));
             $activity->set_state(1);
             $activity->set_category(phpgw::get_var('category'));
             $target_array = phpgw::get_var('target');
             $activity->set_target(implode(",", $target_array));
             $activity->set_description($desc);
             $activity->set_time(phpgw::get_var('time'));
             $activity->set_contact_persons($persons);
             $activity->set_special_adaptation(phpgw::get_var('special_adaptation'));
             $activity->set_contact_person_2_address(phpgw::get_var('contact2_address') . ", " . phpgw::get_var('contact2_number'));
             $activity->set_contact_person_2_zip(phpgw::get_var('contact2_postaddress'));
             $activity->set_frontend(true);
             $activity->set_new_org($new_org);
             $activity->set_new_group($new_group);
             $target_ok = false;
             $district_ok = false;
             if ($get_org_from_local) {
                 //update new organization with district-id from activity.
                 $this->so_organization->update_org_district_local($organization->get_id(), $activity->get_district());
             }
             if ($activity->get_target() && $activity->get_target() != '') {
                 $target_ok = true;
             }
             if ($activity->get_district() && $activity->get_district() != '') {
                 $district_ok = true;
             }
             if ($target_ok && $district_ok) {
                 if ($this->so_activity->store($activity)) {
                     // ... and then try to store the object
                     $message = lang('messages_saved_form');
                 } else {
                     $error = lang('messages_form_error');
                 }
                 //$org_info_edit_url = self::link('/index.php' ,array('menuaction' => 'activitycalendarfrontend.uiactivity.edit_organization_values'));
                 $GLOBALS['phpgw_info']['flags']['noframework'] = true;
                 $this->render('activity.php', array('activity' => $activity, 'organization' => $organization, 'group' => $group, 'contact1' => $persons[0], 'arenas' => $arenas, 'buildings' => $buildings, 'categories' => $categories, 'targets' => $targets, 'districts' => $districts, 'offices' => $offices, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error'), 'ajaxURL' => $ajaxUrl));
             } else {
                 if (!$target_ok) {
                     $error .= "<br/>" . lang('target_not_selected');
                 }
                 if (!$district_ok) {
                     $error .= "<br/>" . lang('district_not_selected');
                 }
                 return $this->render('activity_new.php', array('activity' => $activity, 'organizations' => $organizations, 'organization' => $organization, 'contact1' => $persons[0], 'contact2' => $persons[1], 'org_name' => $org_name, 'new_org' => $new_org, 'groups' => $groups, 'arenas' => $arenas, 'buildings' => $buildings, 'categories' => $categories, 'targets' => $targets, 'districts' => $districts, 'offices' => $offices, 'editable' => true, 'cancel_link' => $cancel_link, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error'), 'ajaxURL' => $ajaxUrl));
             }
         } else {
             return $this->render('activity_new_step_1.php', array('organizations' => $organizations, 'ajaxURL' => $ajaxUrl));
         }
     }
 }