コード例 #1
0
 function add_timeslot()
 {
     if (!can_add(logged_user(), active_context(), Timeslots::instance()->getObjectTypeId())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     ajx_current("empty");
     $timeslot_data = array_var($_POST, 'timeslot');
     try {
         $hoursToAdd = array_var($timeslot_data, 'hours', 0);
         $minutes = array_var($timeslot_data, 'minutes', 0);
         if (strpos($hoursToAdd, ',') && !strpos($hoursToAdd, '.')) {
             $hoursToAdd = str_replace(',', '.', $hoursToAdd);
         }
         if (strpos($hoursToAdd, ':') && !strpos($hoursToAdd, '.')) {
             $pos = strpos($hoursToAdd, ':') + 1;
             $len = strlen($hoursToAdd) - $pos;
             $minutesToAdd = substr($hoursToAdd, $pos, $len);
             if (!strlen($minutesToAdd) <= 2 || !strlen($minutesToAdd) > 0) {
                 $minutesToAdd = substr($minutesToAdd, 0, 2);
             }
             $mins = $minutesToAdd / 60;
             $hours = substr($hoursToAdd, 0, $pos - 1);
             $hoursToAdd = $hours + $mins;
         }
         if ($minutes) {
             $min = str_replace('.', '', $minutes / 6);
             $hoursToAdd = $hoursToAdd + ("0." . $min);
         }
         if ($hoursToAdd <= 0) {
             flash_error(lang('time has to be greater than 0'));
             return;
         }
         $startTime = getDateValue(array_var($timeslot_data, 'date'));
         $startTime = $startTime->add('h', 8 - logged_user()->getTimezone());
         $endTime = getDateValue(array_var($timeslot_data, 'date'));
         $endTime = $endTime->add('h', 8 - logged_user()->getTimezone() + $hoursToAdd);
         $timeslot_data['start_time'] = $startTime;
         $timeslot_data['end_time'] = $endTime;
         $timeslot_data['name'] = $timeslot_data['description'];
         $timeslot_data['object_id'] = 0;
         //array_var($timeslot_data,'project_id');
         $timeslot = new Timeslot();
         //Only admins can change timeslot user
         if (!array_var($timeslot_data, 'contact_id', false) || !logged_user()->isAdministrator()) {
             $timeslot_data['contact_id'] = logged_user()->getId();
         }
         $timeslot->setFromAttributes($timeslot_data);
         $user = Contacts::findById($timeslot_data['contact_id']);
         $billing_category_id = $user->getDefaultBillingId();
         $bc = BillingCategories::findById($billing_category_id);
         if ($bc instanceof BillingCategory) {
             $timeslot->setBillingId($billing_category_id);
             $hourly_billing = $bc->getDefaultValue();
             $timeslot->setHourlyBilling($hourly_billing);
             $timeslot->setFixedBilling($hourly_billing * $hoursToAdd);
             $timeslot->setIsFixedBilling(false);
         }
         DB::beginWork();
         $timeslot->save();
         $member_ids = array();
         $context = active_context();
         foreach ($context as $selection) {
             if ($selection instanceof Member) {
                 $member_ids[] = $selection->getId();
             }
         }
         $object_controller = new ObjectController();
         $object_controller->add_to_members($timeslot, $member_ids);
         DB::commit();
         $show_billing = can_manage_billing(logged_user());
         ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo($show_billing)));
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
     }
     // try
 }
コード例 #2
0
 function add_project_timeslot()
 {
     if (!can_manage_time(logged_user(), true)) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     ajx_current("empty");
     $timeslot_data = array_var($_POST, 'timeslot');
     try {
         $hoursToAdd = array_var($timeslot_data, 'hours', 0);
         if (strpos($hoursToAdd, ',') && !strpos($hoursToAdd, '.')) {
             $hoursToAdd = str_replace(',', '.', $hoursToAdd);
         }
         if (strpos($hoursToAdd, ':') && !strpos($hoursToAdd, '.')) {
             $pos = strpos($hoursToAdd, ':') + 1;
             $len = strlen($hoursToAdd) - $pos;
             $minutesToAdd = substr($hoursToAdd, $pos, $len);
             if (!strlen($minutesToAdd) <= 2 || !strlen($minutesToAdd) > 0) {
                 $minutesToAdd = substr($minutesToAdd, 0, 2);
             }
             $mins = $minutesToAdd / 60;
             $hours = substr($hoursToAdd, 0, $pos - 1);
             $hoursToAdd = $hours + $mins;
         }
         if ($hoursToAdd <= 0) {
             flash_error(lang('time has to be greater than 0'));
             return;
         }
         $startTime = getDateValue(array_var($timeslot_data, 'date'));
         $startTime = $startTime->add('h', 8 - logged_user()->getTimezone());
         $endTime = getDateValue(array_var($timeslot_data, 'date'));
         $endTime = $endTime->add('h', 8 - logged_user()->getTimezone() + $hoursToAdd);
         $timeslot_data['start_time'] = $startTime;
         $timeslot_data['end_time'] = $endTime;
         $timeslot_data['object_id'] = array_var($timeslot_data, 'project_id');
         $timeslot_data['object_manager'] = 'Projects';
         $timeslot = new Timeslot();
         //Only admins can change timeslot user
         if (!array_var($timeslot_data, 'user_id', false) || !logged_user()->isAdministrator()) {
             $timeslot_data['user_id'] = logged_user()->getId();
         }
         $timeslot->setFromAttributes($timeslot_data);
         /* Billing */
         $user = Users::findById($timeslot_data['user_id']);
         $billing_category_id = $user->getDefaultBillingId();
         $project = Projects::findById(array_var($timeslot_data, 'project_id'));
         $timeslot->setBillingId($billing_category_id);
         $hourly_billing = $project->getBillingAmount($billing_category_id);
         $timeslot->setHourlyBilling($hourly_billing);
         $timeslot->setFixedBilling($hourly_billing * $hoursToAdd);
         $timeslot->setIsFixedBilling(false);
         DB::beginWork();
         $timeslot->save();
         DB::commit();
         $show_billing = can_manage_security(logged_user()) && logged_user()->isAdministrator();
         ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo($show_billing)));
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
     }
     // try
 }
コード例 #3
0
 function add_timeslot()
 {
     $object_id = array_var($_REQUEST, "object_id", false);
     ajx_current("empty");
     $timeslot_data = array_var($_POST, 'timeslot');
     if ($object_id) {
         $object = Objects::findObject($object_id);
         if (!$object instanceof ContentDataObject || !$object->canAddTimeslot(logged_user())) {
             flash_error(lang('no access permissions'));
             ajx_current("empty");
             return;
         }
         $member_ids = $object->getMemberIds();
     } else {
         $member_ids = json_decode(array_var($_POST, 'members', array()));
         // clean member_ids
         $tmp_mids = array();
         foreach ($member_ids as $mid) {
             if (!is_null($mid) && trim($mid) != "") {
                 $tmp_mids[] = $mid;
             }
         }
         $member_ids = $tmp_mids;
         if (empty($member_ids)) {
             if (!can_add(logged_user(), active_context(), Timeslots::instance()->getObjectTypeId())) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
         } else {
             if (count($member_ids) > 0) {
                 $enteredMembers = Members::findAll(array('conditions' => 'id IN (' . implode(",", $member_ids) . ')'));
             } else {
                 $enteredMembers = array();
             }
             if (!can_add(logged_user(), $enteredMembers, Timeslots::instance()->getObjectTypeId())) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
         }
         $object_id = 0;
     }
     try {
         $hoursToAdd = array_var($timeslot_data, 'hours', 0);
         $minutes = array_var($timeslot_data, 'minutes', 0);
         if (strpos($hoursToAdd, ',') && !strpos($hoursToAdd, '.')) {
             $hoursToAdd = str_replace(',', '.', $hoursToAdd);
         }
         if (strpos($hoursToAdd, ':') && !strpos($hoursToAdd, '.')) {
             $pos = strpos($hoursToAdd, ':') + 1;
             $len = strlen($hoursToAdd) - $pos;
             $minutesToAdd = substr($hoursToAdd, $pos, $len);
             if (!strlen($minutesToAdd) <= 2 || !strlen($minutesToAdd) > 0) {
                 $minutesToAdd = substr($minutesToAdd, 0, 2);
             }
             $mins = $minutesToAdd / 60;
             $hours = substr($hoursToAdd, 0, $pos - 1);
             $hoursToAdd = $hours + $mins;
         }
         if ($minutes) {
             $min = str_replace('.', '', $minutes / 6);
             $hoursToAdd = $hoursToAdd + ("0." . $min);
         }
         if ($hoursToAdd <= 0) {
             flash_error(lang('time has to be greater than 0'));
             return;
         }
         $startTime = getDateValue(array_var($timeslot_data, 'date'));
         $startTime = $startTime->add('h', 8 - logged_user()->getTimezone());
         $endTime = getDateValue(array_var($timeslot_data, 'date'));
         $endTime = $endTime->add('h', 8 - logged_user()->getTimezone() + $hoursToAdd);
         //use current time
         if (array_var($_REQUEST, "use_current_time", false)) {
             $currentStartTime = DateTimeValueLib::now();
             $currentEndTime = DateTimeValueLib::now();
             $currentStartTime = $currentStartTime->add('h', -$hoursToAdd);
             $startTime->setHour($currentStartTime->getHour());
             $startTime->setMinute($currentStartTime->getMinute());
             $endTime->setHour($currentEndTime->getHour());
             $endTime->setMinute($currentEndTime->getMinute());
         }
         $timeslot_data['start_time'] = $startTime;
         $timeslot_data['end_time'] = $endTime;
         $timeslot_data['description'] = html_to_text($timeslot_data['description']);
         $timeslot_data['name'] = $timeslot_data['description'];
         $timeslot_data['rel_object_id'] = $object_id;
         //array_var($timeslot_data,'project_id');
         $timeslot = new Timeslot();
         //Only admins can change timeslot user
         if (!array_var($timeslot_data, 'contact_id', false) || !SystemPermissions::userHasSystemPermission(logged_user(), 'can_manage_time')) {
             $timeslot_data['contact_id'] = logged_user()->getId();
         }
         $timeslot->setFromAttributes($timeslot_data);
         $user = Contacts::findById($timeslot_data['contact_id']);
         $billing_category_id = $user->getDefaultBillingId();
         $bc = BillingCategories::findById($billing_category_id);
         if ($bc instanceof BillingCategory) {
             $timeslot->setBillingId($billing_category_id);
             $hourly_billing = $bc->getDefaultValue();
             $timeslot->setHourlyBilling($hourly_billing);
             $timeslot->setFixedBilling($hourly_billing * $hoursToAdd);
             $timeslot->setIsFixedBilling(false);
         }
         DB::beginWork();
         $timeslot->save();
         $task = ProjectTasks::findById($object_id);
         if ($task instanceof ProjectTask) {
             $task->calculatePercentComplete();
         }
         if (!isset($member_ids) || !is_array($member_ids) || count($member_ids) == 0) {
             $member_ids = json_decode(array_var($_POST, 'members'));
         }
         $object_controller = new ObjectController();
         $object_controller->add_to_members($timeslot, $member_ids);
         DB::commit();
         ApplicationLogs::createLog($timeslot, ApplicationLogs::ACTION_ADD);
         $show_billing = can_manage_billing(logged_user());
         ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo($show_billing), "real_obj_id" => $timeslot->getRelObjectId()));
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
     }
     // try
 }