/**
  * 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));
         }
     }
 }
 public function edit()
 {
     $GLOBALS['phpgw_info']['flags']['app_header'] .= '::' . lang('edit');
     // Get the contract part id
     $activity_id = (int) phpgw::get_var('id');
     $cancel_link = self::link(array('menuaction' => 'activitycalendar.uiactivities.index'));
     $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();
     // Retrieve the activity object or create a new one
     if (isset($activity_id) && $activity_id > 0) {
         $activity = $this->so_activity->get_single($activity_id);
     } else {
         $activity = new activitycalendar_activity();
     }
     $new_group = $activity->get_new_group();
     $g_id = phpgw::get_var('group_id');
     $o_id = phpgw::get_var('organization_id');
     if ($new_group) {
         $persons = $this->so_group->get_contacts_local($activity->get_group_id());
         $desc = $this->so_group->get_description_local($activity->get_group_id());
     } else {
         if (isset($g_id) && $g_id > 0) {
             $persons = $this->so_group->get_contacts($g_id);
             $desc = $this->so_group->get_description($g_id);
         } else {
             if (isset($o_id) && $o_id > 0) {
                 $persons = $this->so_org->get_contacts($o_id);
                 $desc = $this->so_org->get_description($o_id);
             }
         }
     }
     if (strlen($desc) > 254) {
         $desc = substr($desc, 0, 254);
     }
     $arenas = $this->so_arena->get(null, null, 'arena.arena_name', true, null, null, null);
     if ($activity->get_new_org()) {
         $org_name = $this->so_org->get_organization_name_local($activity->get_organization_id());
     } else {
         $organizations = $this->so_org->get(null, null, 'org.name', true, null, null, null);
     }
     if ($new_group) {
         $group_array = $this->so_group->get(null, null, null, null, null, null, array('group_id' => $activity->get_group_id(), 'new_groups' => 'true'));
         //var_dump($group_array);
         if (count($group_array) > 0) {
             $keys = array_keys($group_array);
             $local_group = $group_array[$keys[0]];
             //$group_name = $local_group->get_name();
         }
     } else {
         $groups = $this->so_group->get(null, null, null, null, null, null, null);
     }
     if (isset($_POST['save_activity'])) {
         if (isset($activity)) {
             $old_state = $activity->get_state();
             $new_state = phpgw::get_var('state');
             // ... set all parameters
             $activity->set_title(phpgw::get_var('title'));
             $activity->set_organization_id(phpgw::get_var('organization_id'));
             $activity->set_group_id(phpgw::get_var('group_id'));
             $internal_arena = phpgw::get_var('internal_arena_id');
             if (isset($internal_arena) && $internal_arena > 0) {
                 $activity->set_arena(0);
                 $activity->set_internal_arena($internal_arena);
             } else {
                 $activity->set_arena(phpgw::get_var('arena_id'));
                 $activity->set_internal_arena(0);
             }
             $district_array = phpgw::get_var('district');
             $activity->set_district(implode(",", $district_array));
             $activity->set_office(phpgw::get_var('office'));
             $activity->set_state($new_state);
             $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_contact_person_2_address(phpgw::get_var('contact_person_2_address'));
             $activity->set_contact_person_2_zip(phpgw::get_var('contact_person_2_zip'));
             $activity->set_special_adaptation(phpgw::get_var('special_adaptation'));
             $target_ok = false;
             $district_ok = false;
             if ($new_state != 5) {
                 $target_ok = true;
                 $district_ok = true;
             } else {
                 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)) {
                     if ($new_group) {
                         //transfer group to booking
                         $group_array = $this->so_group->get(null, null, null, null, null, null, array('group_id' => $activity->get_group_id(), 'new_groups' => 'true'));
                         if (count($group_array) > 0) {
                             $keys = array_keys($group_array);
                             $group = $group_array[$keys[0]];
                         }
                         $group_info = array();
                         $group_info['name'] = $group->get_name();
                         //new
                         $group_info['organization_id'] = $activity->get_organization_id();
                         $group_info['description'] = $group->get_description();
                         $contacts = $this->so_contact->get_local_contact_persons($group->get_id(), true);
                         $contact_1 = $contacts[0];
                         $new_group_id = $this->so_group->transfer_group($group_info);
                         if ($new_group_id) {
                             //update activity with new org id
                             //add contact persons to booking
                             $contact1 = array();
                             $contact1['name'] = $contact_1->get_name();
                             $contact1['phone'] = $contact_1->get_phone();
                             $contact1['mail'] = $contact_1->get_email();
                             $contact1['group_id'] = $new_group_id;
                             $this->so_activity->add_contact_person_group($contact1);
                             $message = lang('messages_saved_form');
                             //get organization_id for the group:
                             $group_org_id = $this->so_group->get_orgid_from_group($new_group_id);
                             //get affected activities and update with new org id
                             $update_activities = $this->so_activity->get_activities_for_update($group->get_id(), true);
                             foreach ($update_activities as $act_id) {
                                 $act = $this->so_activity->get_single($act_id);
                                 $act->set_organization_id($group_org_id);
                                 $act->set_group_id($new_group_id);
                                 $act->set_new_org(false);
                                 $act->set_new_group(false);
                                 $this->so_activity->store($act);
                             }
                             //set local group as stored
                             $group->set_change_type('added');
                             $group->set_transferred(true);
                             $this->so_group->update_local($group);
                             $message = lang('messages_saved_form');
                             //var_dump($new_group_id);
                             $contact_persons = $this->so_contact->get_booking_contact_persons($new_group_id, true);
                             //var_dump(2);
                             $cp1 = $contact_persons[0];
                         }
                     }
                     $message = lang('messages_saved_form');
                 } else {
                     $error = lang('messages_form_error');
                 }
                 if (isset($activity_id) && $activity_id > 0) {
                     $activity = $this->so_activity->get_single($activity_id);
                 }
                 if ($old_state != $new_state && ($new_state == 3 || $new_state == 5)) {
                     $kontor = $this->so_activity->get_office_name($activity->get_office());
                     $subject = lang('mail_subject_update');
                     $body = lang('mail_body_state_' . $new_state, $activity->get_title(), $kontor);
                     if ($activity->get_group_id() && $activity->get_group_id() > 0) {
                         $activity->set_contact_persons(activitycalendar_socontactperson::get_instance()->get_booking_contact_persons($activity->get_group_id(), true));
                         activitycalendar_uiactivities::send_mailnotification_to_group($activity->get_contact_person_1(), $subject, $body);
                     } else {
                         if ($activity->get_organization_id() && $activity->get_organization_id() > 0) {
                             $activity->set_contact_persons(activitycalendar_socontactperson::get_instance()->get_booking_contact_persons($activity->get_organization_id()));
                             activitycalendar_uiactivities::send_mailnotification_to_organization($activity->get_contact_person_1(), $subject, $body);
                         }
                     }
                 }
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'activitycalendar.uiactivities.view', 'id' => $activity->get_id(), 'saved_ok' => 'yes'));
             } else {
                 if (!$target_ok) {
                     $error .= "<br/>" . lang('target_not_selected');
                 }
                 if (!$district_ok) {
                     $error .= "<br/>" . lang('district_not_selected');
                 }
                 return $this->render('activity.php', array('activity' => $activity, 'organizations' => $organizations, 'org_name' => $org_name, 'groups' => $groups, 'local_group' => $local_group, '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')));
             }
         }
     }
     $editable = true;
     if ($activity->get_new_org()) {
         $error = lang('org_not_transferred');
         $editable = false;
     }
     //var_dump($local_group);
     return $this->render('activity.php', array('activity' => $activity, 'organizations' => $organizations, 'org_name' => $org_name, 'groups' => $groups, 'local_group' => $local_group, 'arenas' => $arenas, 'buildings' => $buildings, 'categories' => $categories, 'targets' => $targets, 'districts' => $districts, 'offices' => $offices, 'editable' => $editable, 'cancel_link' => $cancel_link, 'message' => isset($message) ? $message : phpgw::get_var('message'), 'error' => isset($error) ? $error : phpgw::get_var('error')));
 }