/**
  * Execute search
  *
  * @param void
  * @return null
  */
 function search()
 {
     ajx_set_panel("search");
     $timeBegin = microtime(true);
     if (active_project() && !logged_user()->isProjectUser(active_project())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $search_for = array_var($_GET, 'search_for');
     $page = (int) array_var($_GET, 'page', 1);
     if ($page < 1) {
         $page = 1;
     }
     if (trim($search_for) == '') {
         $search_results = null;
         $pagination = null;
     } else {
         if (active_project()) {
             $projects = active_project()->getId();
         } else {
             $projects = null;
         }
         list($search_results, $pagination) = SearchableObjects::searchPaginated($search_for, $projects, logged_user()->isMemberOfOwnerCompany());
     }
     // if
     $timeEnd = microtime(true);
     tpl_assign('search_string', $search_for);
     tpl_assign('current_page', $page);
     tpl_assign('search_results', $search_results);
     tpl_assign('pagination', $pagination);
     tpl_assign('time', $timeEnd - $timeBegin);
 }
 /**
  * Construct the AccountController
  *
  * @access public
  * @param void
  * @return AccountController
  */
 function __construct()
 {
     parent::__construct();
     prepare_company_website_controller($this, 'website');
     if (array_var($_GET, 'current') != 'administration') {
         ajx_set_panel("account");
     }
 }
	function __construct() {
		parent::__construct();
		prepare_company_website_controller($this, 'website');
		ajx_set_panel("administration");

		// Access permissios
		if(!logged_user()->isCompanyAdmin(owner_company())) {
			flash_error(lang('no access permissions'));
			ajx_current("empty");
		} // if
	}
 function help_options()
 {
     $show_context_help = user_config_option('show_context_help', 'until_close', logged_user()->getId());
     $show = true;
     if ($show_context_help == 'never') {
         $show = false;
     }
     tpl_assign('show_help', $show);
     ajx_set_panel('help');
     ajx_replace(true);
 }
 /**
  * Construct the AdministrationController
  *
  * @access public
  * @param void
  * @return AdministrationController
  */
 function __construct()
 {
     parent::__construct();
     prepare_company_website_controller($this, 'website');
     ajx_set_panel("administration");
     // Access permissions
     if (!logged_user()->isExecutiveGroup()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
     }
     // if
     //Autentify password
     if (config_option('ask_administration_autentification')) {
         $last_login = array_var($_SESSION, 'admin_login', 0);
         if ($last_login < time() - ADMIN_SESSION_TIMEOUT) {
             if (array_var($_GET, 'a') != 'password_autentify') {
                 $ref_controller = null;
                 $ref_action = null;
                 $ref_params = array();
                 foreach ($_GET as $k => $v) {
                     $ref_var_name = $k;
                     switch ($ref_var_name) {
                         case 'c':
                             $ref_controller = $v;
                             break;
                         case 'a':
                             $ref_action = $v;
                             break;
                         default:
                             $ref_params[$ref_var_name] = $v;
                     }
                     // switch
                 }
                 $url = get_url($ref_controller, $ref_action, $ref_params);
                 $this->redirectTo('administration', 'password_autentify', array('url' => $url));
             }
         } else {
             $_SESSION['admin_login'] = time();
         }
     }
     //if
 }
 /**
  * Edit logged user profile. 
  * Called with different POST format from "administration/users/edit user profile " and from "profile/edit my profile" 
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_profile()
 {
     ajx_set_panel("");
     $user = Contacts::findById(get_id());
     if (!($user instanceof Contact && $user->isUser()) || $user->getDisabled()) {
         flash_error(lang('user dnx'));
         ajx_current("empty");
         return;
     }
     // if
     $company = $user->getCompany();
     /*if(!($company instanceof Contact)) {
     			flash_error(lang('company dnx'));
     			ajx_current("empty");
     			return;
     		} // if
     		*/
     if (!$user->canUpdateProfile(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $redirect_to = array_var($_GET, 'redirect_to');
     if (trim($redirect_to) == '' || !is_valid_url($redirect_to)) {
         $redirect_to = $user->getCardUserUrl();
     }
     // if
     tpl_assign('redirect_to', null);
     $user_data = array_var($_POST, 'user');
     if (!is_array($user_data)) {
         $user_data = array('username' => $user->getUsername(), 'email' => $user->getEmailAddress(), 'display_name' => $user->getObjectName(), 'timezone' => $user->getTimezone(), 'company_id' => $user->getCompanyId(), 'is_admin' => $user->isAdministrator(), 'type' => $user->getUserType());
         // array
     }
     // if
     tpl_assign('user', $user);
     tpl_assign('company', $company);
     tpl_assign('user_data', $user_data);
     tpl_assign('billing_categories', BillingCategories::findAll());
     // Permission Groups
     $groups = PermissionGroups::getNonPersonalSameLevelPermissionsGroups('`parent_id`,`id` ASC');
     tpl_assign('groups', $groups);
     $roles = SystemPermissions::getAllRolesPermissions();
     tpl_assign('roles', $roles);
     $tabs = TabPanelPermissions::getAllRolesModules();
     tpl_assign('tabs_allowed', $tabs);
     // Submit user
     if (is_array(array_var($_POST, 'user'))) {
         $company_id = array_var($user_data, 'company_id');
         if ($company_id && !Contacts::findById($company_id) instanceof Contact) {
             ajx_current("empty");
             flash_error(lang("company dnx"));
             return;
         }
         try {
             DB::beginWork();
             $user->setUserType(array_var($user_data, 'type'));
             $user->setTimezone(array_var($user_data, 'timezone'));
             $user->setDefaultBillingId(array_var($user_data, 'default_billing_id'));
             $user->setUpdatedOn(DateTimeValueLib::now());
             if (logged_user()->isAdministrator()) {
                 //if ($user->getId() != 2) { // System admin cannot change it's company (from Feng 2.0 onwards administrador has id = 2)
                 //	$user->setCompanyId(array_var($user_data,'company_id'));
                 //}
                 $user->setUsername(array_var($user_data, 'username'));
             } else {
                 $user->setCompanyId(array_var($user_data, 'company_id'));
             }
             if (!isset($_POST['sys_perm'])) {
                 $rol_permissions = SystemPermissions::getRolePermissions(array_var($user_data, 'type'));
                 $_POST['sys_perm'] = array();
                 $not_rol_permissions = SystemPermissions::getNotRolePermissions(array_var($user_data, 'type'));
                 foreach ($not_rol_permissions as $npr) {
                     $_POST['sys_perm'][$npr] = 0;
                 }
                 foreach ($rol_permissions as $pr) {
                     $_POST['sys_perm'][$pr] = 1;
                 }
             }
             if (!isset($_POST['mod_perm'])) {
                 $tabs_permissions = TabPanelPermissions::getRoleModules(array_var($user_data, 'type'));
                 $_POST['mod_perm'] = array();
                 foreach ($tabs_permissions as $pr) {
                     $_POST['mod_perm'][$pr] = 1;
                 }
             }
             $user->save();
             $autotimezone = array_var($user_data, 'autodetect_time_zone', null);
             if ($autotimezone !== null) {
                 set_user_config_option('autodetect_time_zone', $autotimezone, $user->getId());
             }
             $object_controller = new ObjectController();
             $object_controller->add_custom_properties($user);
             $ret = null;
             Hook::fire('after_edit_profile', $user, $ret);
             $pg_id = $user->getPermissionGroupId();
             save_permissions($pg_id, $user->isGuest());
             DB::commit();
             flash_success(lang('success update profile'));
             ajx_current("back");
             ajx_add("overview-panel", "reload");
         } catch (Exception $e) {
             DB::rollback();
             ajx_current("empty");
             flash_error($e->getMessage());
         }
         // try
     }
     // if
 }
 function restore_user()
 {
     ajx_set_panel(array_var($_REQUEST, "current"));
     $user = Contacts::findById(get_id());
     if (!($user instanceof Contact && $user->isUser())) {
         flash_error(lang('user dnx'));
         ajx_current("empty");
         return;
     }
     if (!$user->canDelete(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     try {
         DB::beginWork();
         $user->setDisabled(false);
         $user->unarchive();
         $ret = null;
         Hook::fire("user_restored", $user, $ret);
         DB::commit();
         ApplicationLogs::createLog($user, ApplicationLogs::ACTION_UNTRASH);
         flash_success(lang('success restore user', $user->getObjectName()));
         ajx_current("reload");
     } catch (Exception $e) {
         flash_error($e->getMessage());
         DB::rollback();
         ajx_current("empty");
     }
 }
 /**
  * Edit task
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_task()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_task');
     $task = ProjectTasks::findById(get_id());
     if (!$task instanceof ProjectTask) {
         flash_error(lang('task list dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$task->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $task_data = array_var($_POST, 'task');
     if (!is_array($task_data)) {
         $this->getRepeatOptions($task, $occ, $rsel1, $rsel2, $rsel3, $rnum, $rend, $rjump);
         $tag_names = $task->getTagNames();
         $task_data = array('title' => array_var($_POST, 'title', $task->getTitle()), 'text' => $task->getText(), 'milestone_id' => array_var($_POST, 'milestone_id', $task->getMilestoneId()), 'due_date' => getDateValue(array_var($_POST, 'task_due_date'), $task->getDueDate()), 'start_date' => getDateValue(array_var($_POST, 'task_start_date', $task->getStartDate())), 'parent_id' => $task->getParentId(), 'project_id' => array_var($_POST, 'project_id', $task->getProjectId()), 'tags' => is_array($tag_names) && count($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $task->isPrivate(), 'assigned_to' => array_var($_POST, 'assigned_to', $task->getAssignedToCompanyId() . ':' . $task->getAssignedToUserId()), 'priority' => array_var($_POST, 'priority', $task->getPriority()), 'send_notification' => array_var($_POST, 'notify') == 'true', 'time_estimate' => $task->getTimeEstimate(), 'forever' => $task->getRepeatForever(), 'rend' => $rend, 'rnum' => $rnum, 'rjump' => $rjump, 'rsel1' => $rsel1, 'rsel2' => $rsel2, 'rsel3' => $rsel3, 'occ' => $occ, 'repeat_by' => $task->getRepeatBy(), 'object_subtype' => array_var($_POST, "object_subtype", $task->getObjectSubtype() != 0 ? $task->getObjectSubtype() : config_option('default task co type')));
         // array
         $handins = ObjectHandins::getAllHandinsByObject($task);
         $id = 0;
         if ($handins) {
             foreach ($handins as $handin) {
                 $task_data['handin' . $id] = array('title' => $handin->getTitle(), 'assigned_to' => $handin->getResponsibleCompanyId() . ':' . $handin->getResponsibleUserId());
                 // array
                 $id = $id + 1;
                 if ($id > 3) {
                     break;
                 }
             }
             // foreach
         }
         // if
     }
     // if
     tpl_assign('task', $task);
     tpl_assign('task_data', $task_data);
     if (is_array(array_var($_POST, 'task'))) {
         //MANAGE CONCURRENCE WHILE EDITING
         $upd = array_var($_POST, 'updatedon');
         if ($upd && $task->getUpdatedOn()->getTimestamp() > $upd && !array_var($_POST, 'merge-changes') == 'true') {
             ajx_current('empty');
             evt_add("handle edit concurrence", array("updatedon" => $task->getUpdatedOn()->getTimestamp(), "genid" => array_var($_POST, 'genid')));
             return;
         }
         if (array_var($_POST, 'merge-changes') == 'true') {
             $this->setTemplate('view_list');
             $edited_task = ProjectTasks::findById($task->getId());
             ajx_set_no_toolbar(true);
             ajx_set_panel(lang('tab name', array('name' => $edited_task->getTitle())));
             tpl_assign('task_list', $edited_task);
             ajx_extra_data(array("title" => $edited_task->getTitle(), 'icon' => 'ico-task'));
             return;
         }
         $old_owner = $task->getAssignedTo();
         if (array_var($task_data, 'parent_id') == $task->getId()) {
             flash_error(lang("task own parent error"));
             ajx_current("empty");
             return;
         }
         $old_is_private = $task->isPrivate();
         $old_project_id = $task->getProjectId();
         $project_id = array_var($_POST, 'ws_ids', 0);
         if ($old_project_id != $project_id) {
             $newProject = Projects::findById($project_id);
             if (!$newProject instanceof Project || !$task->canAdd(logged_user(), $newProject)) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
         }
         $task_data['due_date'] = getDateValue(array_var($_POST, 'task_due_date'));
         $task_data['start_date'] = getDateValue(array_var($_POST, 'task_start_date'));
         try {
             $err_msg = $this->setRepeatOptions($task_data);
             if ($err_msg) {
                 flash_error($err_msg);
                 ajx_current("empty");
                 return;
             }
             if (!isset($task_data['parent_id'])) {
                 $task_data['parent_id'] = 0;
             }
             $was_template = $task->getIsTemplate();
             $task->setFromAttributes($task_data);
             $task->setIsTemplate($was_template);
             // is_template value must not be changed from ui
             // Set assigned to
             $assigned_to = explode(':', array_var($task_data, 'assigned_to', ''));
             $company_id = array_var($assigned_to, 0, 0);
             $user_id = array_var($assigned_to, 1, 0);
             $can_assign = can_assign_task_to_company_user(logged_user(), $task, $company_id, $user_id);
             if ($can_assign !== true) {
                 flash_error($can_assign);
                 return;
             }
             $task->setAssignedToCompanyId($company_id);
             $task->setAssignedToUserId($user_id);
             if (!logged_user()->isMemberOfOwnerCompany()) {
                 $task->setIsPrivate($old_is_private);
             }
             $totalMinutes = array_var($task_data, 'time_estimate_hours') * 60 + array_var($task_data, 'time_estimate_minutes');
             $task->setTimeEstimate($totalMinutes);
             //Add handins
             $handins = array();
             for ($i = 0; $i < 4; $i++) {
                 if (isset($task_data["handin{$i}"]) && is_array($task_data["handin{$i}"]) && trim(array_var($task_data["handin{$i}"], 'title')) != '') {
                     $assigned_to = explode(':', array_var($task_data["handin{$i}"], 'assigned_to', ''));
                     $handins[] = array('title' => array_var($task_data["handin{$i}"], 'title'), 'responsible_company_id' => array_var($assigned_to, 0, 0), 'responsible_user_id' => array_var($assigned_to, 1, 0));
                     // array
                 }
                 // if
             }
             // for
             if ($task->getParentId() > 0 && $task->hasChild($task->getParentId())) {
                 flash_error(lang('task child of child error'));
                 ajx_current("empty");
                 return;
             }
             DB::beginWork();
             $task->save();
             $task->setTagsFromCSV(array_var($task_data, 'tags'));
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($task, !$task->getIsTemplate());
             $object_controller->link_to_new_object($task);
             $object_controller->add_subscribers($task);
             $object_controller->add_custom_properties($task);
             $object_controller->add_reminders($task);
             // apply values to subtasks
             $subtasks = $task->getAllSubTasks();
             $project = $task->getProject();
             $milestone_id = $task->getMilestoneId();
             $apply_ws = array_var($task_data, 'apply_ws_subtasks') == "checked";
             $apply_ms = array_var($task_data, 'apply_milestone_subtasks') == "checked";
             $apply_at = array_var($task_data, 'apply_assignee_subtasks', '') == "checked";
             foreach ($subtasks as $sub) {
                 $modified = false;
                 if ($apply_at || !$sub->getAssignedTo() instanceof ApplicationDataObject) {
                     $sub->setAssignedToCompanyId($company_id);
                     $sub->setAssignedToUserId($user_id);
                     $modified = true;
                 }
                 if ($apply_ws) {
                     $sub->setProject($project);
                     $modified = true;
                 }
                 if ($apply_ms) {
                     $sub->setMilestoneId($milestone_id);
                     $modified = true;
                 }
                 if ($modified) {
                     $sub->save();
                 }
             }
             $task->resetIsRead();
             ApplicationLogs::createLog($task, $task->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             try {
                 if (array_var($task_data, 'send_notification') == 'checked') {
                     $new_owner = $task->getAssignedTo();
                     if ($new_owner instanceof User) {
                         Notifier::taskAssigned($task);
                     }
                     // if
                 }
                 // if
             } catch (Exception $e) {
             }
             // try
             flash_success(lang('success edit task list', $task->getTitle()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
 }
 function index()
 {
     ajx_set_panel("more-panel");
     ajx_set_no_toolbar();
 }
 function __construct()
 {
     parent::__construct();
     prepare_company_website_controller($this, 'website');
     ajx_set_panel("search");
 }
 /**
  * Edit specific message
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     $this->setTemplate('add_message');
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current('empty');
         return;
     }
     $message = ProjectMessages::findById(get_id());
     if (!$message instanceof ProjectMessage) {
         flash_error(lang('message dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$message->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $message_data = array_var($_POST, 'message');
     if (!is_array($message_data)) {
         $tag_names = $message->getTagNames();
         $message_data = array('milestone_id' => $message->getMilestoneId(), 'title' => $message->getTitle(), 'text' => $message->getText(), 'additional_text' => $message->getAdditionalText(), 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $message->isPrivate(), 'is_important' => $message->getIsImportant(), 'comments_enabled' => $message->getCommentsEnabled(), 'anonymous_comments_enabled' => $message->getAnonymousCommentsEnabled());
         // array
     }
     // if
     tpl_assign('message', $message);
     tpl_assign('message_data', $message_data);
     if (is_array(array_var($_POST, 'message'))) {
         try {
             //MANAGE CONCURRENCE WHILE EDITING
             $upd = array_var($_POST, 'updatedon');
             if ($upd && $message->getUpdatedOn()->getTimestamp() > $upd && !array_var($_POST, 'merge-changes') == 'true') {
                 ajx_current('empty');
                 evt_add("handle edit concurrence", array("updatedon" => $message->getUpdatedOn()->getTimestamp(), "genid" => array_var($_POST, 'genid')));
                 return;
             }
             if (array_var($_POST, 'merge-changes') == 'true') {
                 $this->setTemplate('view');
                 $edited_note = ProjectMessages::findById($message->getId());
                 tpl_assign('message', $edited_note);
                 tpl_assign('subscribers', $edited_note->getSubscribers());
                 ajx_extra_data(array("title" => $edited_note->getTitle(), 'icon' => 'ico-message'));
                 ajx_set_no_toolbar(true);
                 ajx_set_panel(lang('tab name', array('name' => $edited_note->getTitle())));
                 return;
             }
             $old_is_private = $message->isPrivate();
             $old_is_important = $message->getIsImportant();
             $old_comments_enabled = $message->getCommentsEnabled();
             $old_anonymous_comments_enabled = $message->getAnonymousCommentsEnabled();
             $message->setFromAttributes($message_data);
             // Options are reserved only for members of owner company
             if (!logged_user()->isMemberOfOwnerCompany()) {
                 $message->setIsPrivate($old_is_private);
                 $message->setIsImportant($old_is_important);
                 $message->setCommentsEnabled($old_comments_enabled);
                 $message->setAnonymousCommentsEnabled($old_anonymous_comments_enabled);
             }
             // if
             DB::beginWork();
             $message->save();
             $message->setTagsFromCSV(array_var($message_data, 'tags'));
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($message);
             $object_controller->link_to_new_object($message);
             $object_controller->add_subscribers($message);
             $object_controller->add_custom_properties($message);
             $message->resetIsRead();
             ApplicationLogs::createLog($message, $message->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             flash_success(lang('success edit message', $message->getTitle()));
             if (array_var($_POST, 'popup', false)) {
                 ajx_current("reload");
             } else {
                 ajx_current("back");
             }
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
 }
	function __construct() {
		$this->pagination = new StdClass();
		parent::__construct();
		prepare_company_website_controller($this, 'website');
		ajx_set_panel("search");
		self::$MYSQL_MIN_WORD_LENGHT = (int)array_var(DB::executeOne("SHOW variables LIKE 'ft_min_word_len' "),"Value");
	}
 function edit()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('event');
     $event = ProjectEvents::findById(get_id());
     $user_filter = isset($_GET['user_id']) ? $_GET['user_id'] : logged_user()->getId();
     $inv = EventInvitations::findById(array('event_id' => $event->getId(), 'user_id' => $user_filter));
     if ($inv != null) {
         $event->addInvitation($inv);
     }
     if (!$event->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     tpl_assign('active_projects', logged_user()->getActiveProjects());
     $event_data = array_var($_POST, 'event');
     if (!is_array($event_data)) {
         $tag_names = $event->getTagNames();
         $setlastweek = false;
         $rsel1 = false;
         $rsel2 = false;
         $rsel3 = false;
         $forever = $event->getRepeatForever();
         $occ = 1;
         if ($event->getRepeatD() > 0) {
             $occ = 2;
             $rjump = $event->getRepeatD();
         }
         if ($event->getRepeatD() > 0 and $event->getRepeatD() % 7 == 0) {
             $occ = 3;
             $rjump = $event->getRepeatD() / 7;
         }
         if ($event->getRepeatM() > 0) {
             $occ = 4;
             $rjump = $event->getRepeatM();
         }
         if ($event->getRepeatY() > 0) {
             $occ = 5;
             $rjump = $event->getRepeatY();
         }
         if ($event->getRepeatH() > 0) {
             $occ = 6;
         }
         if ($event->getRepeatH() == 2) {
             $setlastweek = true;
         }
         if ($event->getRepeatEnd()) {
             $rend = $event->getRepeatEnd();
         }
         if ($event->getRepeatNum() > 0) {
             $rnum = $event->getRepeatNum();
         }
         if (!isset($rjump) || !is_numeric($rjump)) {
             $rjump = 1;
         }
         // decide which repeat type it is
         if ($forever) {
             $rsel1 = true;
         } else {
             if (isset($rnum) and $rnum > 0) {
                 $rsel2 = true;
             } else {
                 if (isset($rend) and $rend instanceof DateTimeValue) {
                     $rsel3 = true;
                 }
             }
         }
         //repeat until
         //if(isset($rend) AND $rend=="9999-00-00") $rend = "";
         // organize the time and date data for the html select drop downs.
         $thetime = $event->getStart()->getTimestamp() + logged_user()->getTimezone() * 3600;
         $durtime = $event->getDuration()->getTimestamp() + logged_user()->getTimezone() * 3600 - $thetime;
         $hour = date('G', $thetime);
         // format time to 24-hour or 12-hour clock.
         if (!user_config_option('time_format_use_24')) {
             if ($hour >= 12) {
                 $pm = 1;
                 $hour = $hour - 12;
             } else {
                 $pm = 0;
             }
         }
         $event_data = array('subject' => $event->getSubject(), 'description' => $event->getDescription(), 'name' => $event->getCreatedById(), 'username' => $event->getCreatedById(), 'typeofevent' => $event->getTypeId(), 'forever' => $event->getRepeatForever(), 'usetimeandduration' => $event->getTypeId() == 3 ? 0 : 1, 'occ' => $occ, 'rjump' => $rjump, 'setlastweek' => $setlastweek, 'rend' => isset($rend) ? $rend : NULL, 'rnum' => isset($rnum) ? $rnum : NULL, 'rsel1' => $rsel1, 'rsel2' => $rsel2, 'rsel3' => $rsel3, 'thetime' => $event->getStart()->getTimestamp(), 'hour' => $hour, 'minute' => date('i', $thetime), 'month' => date('n', $thetime), 'year' => date('Y', $thetime), 'day' => date('j', $thetime), 'durtime' => $event->getDuration()->getTimestamp() - $thetime, 'durationmin' => $durtime / 60 % 60, 'durationhour' => $durtime / 3600 % 24, 'durday' => floor($durtime / 86400), 'pm' => isset($pm) ? $pm : 0, 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '', 'repeat_dow' => $event->getRepeatDow(), 'repeat_wnum' => $event->getRepeatWnum(), 'repeat_mjump' => $event->getRepeatMjump());
         // array
     }
     // if
     tpl_assign('event_data', $event_data);
     tpl_assign('event', $event);
     if (is_array(array_var($_POST, 'event'))) {
         //	MANAGE CONCURRENCE WHILE EDITING
         $upd = array_var($_POST, 'updatedon');
         if ($upd && $event->getUpdatedOn()->getTimestamp() > $upd && !array_var($_POST, 'merge-changes') == 'true') {
             ajx_current('empty');
             evt_add("handle edit concurrence", array("updatedon" => $event->getUpdatedOn()->getTimestamp(), "genid" => array_var($_POST, 'genid')));
             return;
         }
         if (array_var($_POST, 'merge-changes') == 'true') {
             $this->setTemplate('view_event');
             $editedEvent = ProjectEvents::findById($event->getId());
             $this->viewevent();
             ajx_set_panel(lang('tab name', array('name' => $editedEvent->getTitle())));
             ajx_extra_data(array("title" => $editedEvent->getTitle(), 'icon' => 'ico-event'));
             ajx_set_no_toolbar(true);
             ajx_set_panel(lang('tab name', array('name' => $editedEvent->getTitle())));
             return;
         }
         try {
             $data = $this->getData($event_data);
             // run the query to set the event data
             $event->setFromAttributes($data);
             $this->registerInvitations($data, $event, false);
             if (isset($data['confirmAttendance'])) {
                 $this->change_invitation_state($data['confirmAttendance'], $event->getId(), $user_filter);
             }
             if (isset($data['send_notification']) && $data['send_notification']) {
                 $users_to_inv = array();
                 foreach ($data['users_to_invite'] as $us => $v) {
                     if ($us != logged_user()->getId()) {
                         $users_to_inv[] = Users::findById(array('id' => $us));
                     }
                 }
                 Notifier::notifEvent($event, $users_to_inv, 'modified', logged_user());
             }
             foreach ($data['users_to_invite'] as $user_id => $v) {
                 $user = Users::findById(array('id' => $user_id));
                 if ($user instanceof User) {
                     $phone_num = Users::getPhoneNumberCustomProperty($user_id);
                     $sms_obj = new SmsController();
                     $sms_obj->prepareEventInvitee($user->getDisplayName(), $event->getTitle(), get_class($event));
                     $sms_obj->sendSms($phone_num);
                 }
             }
             if (!logged_user()->isMemberOfOwnerCompany()) {
                 $event->setIsPrivate(false);
             }
             DB::beginWork();
             $event->save();
             $event->setTagsFromCSV(array_var($event_data, 'tags'));
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($event);
             $object_controller->link_to_new_object($event);
             $object_controller->add_subscribers($event);
             $object_controller->add_custom_properties($event);
             $object_controller->add_reminders($event);
             $event->resetIsRead();
             ApplicationLogs::createLog($event, $event->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             DB::commit();
             flash_success(lang('success edit event', clean($event->getObjectName())));
             if (array_var($_POST, 'popup', false)) {
                 ajx_current("reload");
             } else {
                 ajx_current("back");
             }
             ajx_add("overview-panel", "reload");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
             //tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
 /**
  * Edit specific webpage
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add');
     $webpage = ProjectWebpages::findById(get_id());
     if (!$webpage instanceof ProjectWebpage) {
         flash_error(lang('webpage dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$webpage->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $webpage_data = array_var($_POST, 'webpage');
     if (!is_array($webpage_data)) {
         $tag_names = $webpage->getTagNames();
         $webpage_data = array('url' => $webpage->getUrl(), 'title' => $webpage->getTitle(), 'description' => $webpage->getDescription(), 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '', 'is_private' => $webpage->isPrivate());
         // array
     }
     // if
     if (is_array(array_var($_POST, 'webpage'))) {
         //MANAGE CONCURRENCE WHILE EDITING
         $upd = array_var($_POST, 'updatedon');
         if ($upd && $webpage->getUpdatedOn()->getTimestamp() > $upd && !array_var($_POST, 'merge-changes') == 'true') {
             ajx_current('empty');
             evt_add("handle edit concurrence", array("updatedon" => $webpage->getUpdatedOn()->getTimestamp(), "genid" => array_var($_POST, 'genid')));
             return;
         }
         if (array_var($_POST, 'merge-changes') == 'true') {
             $this->setTemplate('view');
             $edited_wp = ProjectWebpages::findById($webpage->getId());
             ajx_set_no_toolbar(true);
             ajx_set_panel(lang('tab name', array('name' => $edited_wp->getTitle())));
             tpl_assign('object', $edited_wp);
             ajx_extra_data(array("title" => $edited_wp->getTitle(), 'icon' => 'ico-webpage'));
             return;
         }
         try {
             $old_is_private = $webpage->isPrivate();
             $webpage->setFromAttributes($webpage_data);
             // Options are reserved only for members of owner company
             if (!logged_user()->isMemberOfOwnerCompany()) {
                 $webpage->setIsPrivate($old_is_private);
             }
             // if
             DB::beginWork();
             $webpage->save();
             $webpage->setTagsFromCSV(array_var($webpage_data, 'tags'));
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($webpage);
             $object_controller->link_to_new_object($webpage);
             $object_controller->add_subscribers($webpage);
             $object_controller->add_custom_properties($webpage);
             ApplicationLogs::createLog($webpage, $webpage->getWorkspaces(), ApplicationLogs::ACTION_EDIT);
             $webpage->resetIsRead();
             DB::commit();
             flash_success(lang('success edit webpage', $webpage->getTitle()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
     tpl_assign('webpage', $webpage);
     tpl_assign('webpage_data', $webpage_data);
 }
 /**
  * Edit specific contact
  *
  * @access public
  * @param void
  * @return null
  */
 function edit()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('edit_contact');
     if (active_project() instanceof Project) {
         tpl_assign('isAddProject', true);
     }
     $contact = Contacts::findById(get_id());
     if (!$contact instanceof Contact) {
         flash_error(lang('contact dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$contact->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $im_types = ImTypes::findAll(array('order' => '`id`'));
     $active_project = active_project();
     $role = "";
     if ($active_project) {
         $pc = $contact->getRole(active_project());
         if ($pc instanceof ProjectContact) {
             $role = $pc->getRole();
         }
     }
     $contact_data = array_var($_POST, 'contact');
     if (!is_array($contact_data)) {
         $tag_names = $contact->getTagNames();
         $contact_data = array('firstname' => $contact->getFirstName(), 'lastname' => $contact->getLastName(), 'middlename' => $contact->getMiddleName(), 'department' => $contact->getDepartment(), 'job_title' => $contact->getJobTitle(), 'email' => $contact->getEmail(), 'email2' => $contact->getEmail2(), 'email3' => $contact->getEmail3(), 'w_web_page' => $contact->getWWebPage(), 'w_address' => $contact->getWAddress(), 'w_city' => $contact->getWCity(), 'w_state' => $contact->getWState(), 'w_zipcode' => $contact->getWZipcode(), 'w_country' => $contact->getWCountry(), 'w_phone_number' => $contact->getWPhoneNumber(), 'w_phone_number2' => $contact->getWPhoneNumber2(), 'w_fax_number' => $contact->getWFaxNumber(), 'w_assistant_number' => $contact->getWAssistantNumber(), 'w_callback_number' => $contact->getWCallbackNumber(), 'h_web_page' => $contact->getHWebPage(), 'h_address' => $contact->getHAddress(), 'h_city' => $contact->getHCity(), 'h_state' => $contact->getHState(), 'h_zipcode' => $contact->getHZipcode(), 'h_country' => $contact->getHCountry(), 'h_phone_number' => $contact->getHPhoneNumber(), 'h_phone_number2' => $contact->getHPhoneNumber2(), 'h_fax_number' => $contact->getHFaxNumber(), 'h_mobile_number' => $contact->getHMobileNumber(), 'h_pager_number' => $contact->getHPagerNumber(), 'o_web_page' => $contact->getOWebPage(), 'o_address' => $contact->getOAddress(), 'o_city' => $contact->getOCity(), 'o_state' => $contact->getOState(), 'o_zipcode' => $contact->getOZipcode(), 'o_country' => $contact->getOCountry(), 'o_phone_number' => $contact->getOPhoneNumber(), 'o_phone_number2' => $contact->getOPhoneNumber2(), 'o_fax_number' => $contact->getOFaxNumber(), 'o_birthday' => $contact->getOBirthday(), 'picture_file' => $contact->getPictureFile(), 'timezone' => $contact->getTimezone(), 'notes' => $contact->getNotes(), 'is_private' => $contact->getIsPrivate(), 'company_id' => $contact->getCompanyId(), 'role' => $role, 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '');
         // array
         if (is_array($im_types)) {
             foreach ($im_types as $im_type) {
                 $contact_data['im_' . $im_type->getId()] = $contact->getImValue($im_type);
             }
             // forech
         }
         // if
         $default_im = $contact->getDefaultImType();
         $contact_data['default_im'] = $default_im instanceof ImType ? $default_im->getId() : '';
     }
     // if
     tpl_assign('contact', $contact);
     tpl_assign('contact_data', $contact_data);
     tpl_assign('im_types', $im_types);
     if (is_array(array_var($_POST, 'contact'))) {
         //	MANAGE CONCURRENCE WHILE EDITING
         $upd = array_var($_POST, 'updatedon');
         if ($upd && $contact->getUpdatedOn()->getTimestamp() > $upd && !array_var($_POST, 'merge-changes') == 'true') {
             ajx_current('empty');
             evt_add("handle edit concurrence", array("updatedon" => $contact->getUpdatedOn()->getTimestamp(), "genid" => array_var($_POST, 'genid')));
             return;
         }
         if (array_var($_POST, 'merge-changes') == 'true') {
             $this->setTemplate('card');
             $new_contact = Contacts::findById($contact->getId());
             ajx_set_panel(lang('tab name', array('name' => $new_contact->getDisplayName())));
             ajx_extra_data(array("title" => $new_contact->getDisplayName(), 'icon' => 'ico-contact'));
             ajx_set_no_toolbar(true);
             //ajx_set_panel(lang ('tab name',array('name'=>$new_contact->getDisplayName())));
             return;
         }
         try {
             DB::beginWork();
             $newCompany = false;
             if (array_var($contact_data, 'isNewCompany') == 'true' && is_array(array_var($_POST, 'company'))) {
                 $company_data = array_var($_POST, 'company');
                 $company = new Company();
                 $company->setFromAttributes($company_data);
                 $company->setClientOfId(1);
                 $company->save();
                 ApplicationLogs::createLog($company, null, ApplicationLogs::ACTION_ADD);
                 $newCompany = true;
                 if (active_project() instanceof Project && $company->canAdd(logged_user(), active_project())) {
                     $company->addToWorkspace(active_project());
                 } else {
                     $company->addToWorkspace(logged_user()->getPersonalProject());
                 }
             }
             $contact_data['o_birthday'] = getDateValue(array_var($contact_data, "o_birthday_value", ''));
             $contact->setFromAttributes($contact_data);
             /*if (!is_null($contact->getOBirthday()) && $contact_data["o_birthday_year"] == 0){
             			$contact->setOBirthday(null);
             		} else if ($contact_data["o_birthday_year"] != 0) {
             			$bday = new DateTimeValue(0);
             			$bday->setYear($contact_data["o_birthday_year"]);
             			$bday->setMonth($contact_data["o_birthday_month"]);
             			$bday->setDay($contact_data["o_birthday_day"]);
             			$contact->setOBirthday($bday);
             		}*/
             if ($newCompany) {
                 $contact->setCompanyId($company->getId());
             }
             $contact->save();
             $contact->setTagsFromCSV(array_var($contact_data, 'tags'));
             $contact->clearImValues();
             foreach ($im_types as $im_type) {
                 $value = trim(array_var($contact_data, 'im_' . $im_type->getId()));
                 if ($value != '') {
                     $contact_im_value = new ContactImValue();
                     $contact_im_value->setContactId($contact->getId());
                     $contact_im_value->setImTypeId($im_type->getId());
                     $contact_im_value->setValue($value);
                     $contact_im_value->setIsDefault(array_var($contact_data, 'default_im') == $im_type->getId());
                     $contact_im_value->save();
                 }
                 // if
             }
             // foreach
             $object_controller = new ObjectController();
             $object_controller->add_to_workspaces($contact, !can_manage_contacts(logged_user()));
             $object_controller->link_to_new_object($contact);
             $object_controller->add_subscribers($contact);
             $object_controller->add_custom_properties($contact);
             ApplicationLogs::createLog($contact, null, ApplicationLogs::ACTION_EDIT);
             DB::commit();
             if (trim(array_var($contact_data, 'role', '')) != '' && active_project() instanceof Project) {
                 if (!ProjectContact::canAdd(logged_user(), active_project())) {
                     flash_error(lang('error contact added but not assigned', $contact->getDisplayName(), active_project()->getName()));
                     ajx_current("back");
                     return;
                 }
                 // if
                 $pc = $contact->getRole(active_project());
                 if (!$pc instanceof ProjectContact) {
                     $pc = new ProjectContact();
                     $pc->setContactId($contact->getId());
                     $pc->setProjectId(active_project()->getId());
                 }
                 $pc->setRole(array_var($contact_data, 'role'));
                 $pc->save();
                 //ApplicationLogs::createLog($contact, $contact->getWorkspaces(), ApplicationLogs::ACTION_ADD);
             }
             flash_success(lang('success edit contact', $contact->getDisplayName()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             flash_error($e->getMessage());
             ajx_current("empty");
         }
         // try
     }
     // if
 }