Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
// Assigned staff / team
if ($cfg->alertAssignedONTaskActivity()) {
if (isset($vars['assignee'])
&& $vars['assignee'] instanceof Staff)
$recipients[] = $vars['assignee'];
elseif ($this->isOpen() && ($assignee = $this->getStaff()))
$recipients[] = $assignee;
if ($team = $this->getTeam())
$recipients = array_merge($recipients, $team->getMembers());
}
// Dept manager
if ($cfg->alertDeptManagerONTaskActivity() && $dept && $dept->getManagerId())
$recipients[] = $dept->getManager();
$options = array();
$staffId = $thisstaff ? $thisstaff->getId() : 0;
if ($vars['threadentry'] && $vars['threadentry'] instanceof ThreadEntry) {
$options = array(
'inreplyto' => $vars['threadentry']->getEmailMessageId(),
'references' => $vars['threadentry']->getEmailReferences(),
'thread' => $vars['threadentry']);
// Activity details
if (!$vars['message'])
$vars['message'] = $vars['threadentry'];
// Staff doing the activity
$staffId = $vars['threadentry']->getStaffId() ?: $staffId;
}
$msg = $this->replaceVars($msg->asArray(),
array(
'note' => $vars['threadentry'], // For compatibility
'activity' => $vars['activity'],
'message' => $vars['message']));
$isClosed = $this->isClosed();
$sentlist=array();
foreach ($recipients as $k=>$staff) {
if (!is_object($staff)
// Don't bother vacationing staff.
|| !$staff->isAvailable()
// No need to alert the poster!
|| $staffId == $staff->getId()
// No duplicates.
|| isset($sentlist[$staff->getEmail()])
// Make sure staff has access to task
|| ($isClosed && !$this->checkStaffPerm($staff))
) {
continue;
}
$alert = $this->replaceVars($msg, array('recipient' => $staff));
$email->sendAlert($staff, $alert['subj'], $alert['body'], null, $options);
$sentlist[$staff->getEmail()] = 1;
}
}
/*
* Notify collaborators on response or new message
*
*/
function notifyCollaborators($entry, $vars = array()) {
global $cfg;
if (!$entry instanceof ThreadEntry
|| !($recipients=$this->getThread()->getParticipants())
|| !($dept=$this->getDept())
|| !($tpl=$dept->getTemplate())
|| !($msg=$tpl->getTaskActivityNoticeMsgTemplate())
|| !($email=$dept->getEmail())
) {
return;
}
// Who posted the entry?
$skip = array();
if ($entry instanceof Message) {
$poster = $entry->getUser();
// Skip the person who sent in the message
$skip[$entry->getUserId()] = 1;
// Skip all the other recipients of the message
foreach ($entry->getAllEmailRecipients() as $R) {
foreach ($recipients as $R2) {
if (0 === strcasecmp($R2->getEmail(), $R->mailbox.'@'.$R->host)) {
$skip[$R2->getUserId()] = true;
break;
}
}
}
} else {
$poster = $entry->getStaff();
}
$vars = array_merge($vars, array(
'message' => (string) $entry,
'poster' => $poster ?: _S('A collaborator'),
)
);
$msg = $this->replaceVars($msg->asArray(), $vars);
$attachments = $cfg->emailAttachments()?$entry->getAttachments():array();
$options = array('inreplyto' => $entry->getEmailMessageId(),
'thread' => $entry);
foreach ($recipients as $recipient) {
// Skip folks who have already been included on this part of
// the conversation
if (isset($skip[$recipient->getUserId()]))
continue;
$notice = $this->replaceVars($msg, array('recipient' => $recipient));
$email->send($recipient, $notice['subj'], $notice['body'], $attachments,
$options);
}
}
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
function update($forms, $vars, &$errors) {
global $thisstaff;
if (!$forms || !$this->checkStaffPerm($thisstaff, Task::PERM_EDIT))
return false;
foreach ($forms as $form) {
$form->setSource($vars);
if (!$form->isValid(function($f) {
return $f->isVisibleToStaff() && $f->isEditableToStaff();
}, array('mode'=>'edit'))) {
$errors = array_merge($errors, $form->errors());
}
}
if ($errors)
return false;
// Update dynamic meta-data
$changes = array();
foreach ($forms as $f) {
$changes += $f->getChanges();
$f->save();
}
if ($vars['note']) {
$_errors = array();
$this->postNote(array(
'note' => $vars['note'],
'title' => __('Task Update'),
),
$_errors,
$thisstaff);
}
if ($changes)
$this->logEvent('edited', array('fields' => $changes));
Signal::send('model.updated', $this);
return $this->save();
}
if (($task = self::lookup(array('number' => $number))))
return $task->getId();
}
static function isNumberUnique($number) {
return !self::lookupIdByNumber($number);
}
static function create($vars=false) {
if (!is_array($vars)
|| !$thisstaff
|| !$thisstaff->hasPerm(Task::PERM_CREATE, false))
$task = parent::create(array(
'flags' => self::ISOPEN,
'object_id' => $vars['object_id'],
'object_type' => $vars['object_type'],
'number' => $cfg->getNewTaskNumber(),
'created' => new SqlFunction('NOW'),
'updated' => new SqlFunction('NOW'),
));
if ($vars['internal_formdata']['dept_id'])
$task->dept_id = $vars['internal_formdata']['dept_id'];
if ($vars['internal_formdata']['duedate'])
$task->duedate = $vars['internal_formdata']['duedate'];
// Add dynamic data
$task->addDynamicData($vars['default_formdata']);
// Create a thread + message.
$thread = TaskThread::create($task);
$thread->addDescription($vars);
$task->logEvent('created', null, $thisstaff);
// Get role for the dept
$role = $thisstaff->getRole($task->dept_id);
// Assignment
$assignee = $vars['internal_formdata']['assignee'];
if ($assignee
// skip assignment if the user doesn't have perm.
&& $role->hasPerm(Task::PERM_ASSIGN)) {
$_errors = array();
$assigneeId = sprintf('%s%d',
($assignee instanceof Staff) ? 's' : 't',
$assignee->getId());
$form = AssignmentForm::instantiate(array('assignee' => $assigneeId));
Signal::send('task.created', $task);
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
function delete($comments='') {
global $ost, $thisstaff;
$thread = $this->getThread();
if (!parent::delete())
return false;
$thread->delete();
Draft::deleteForNamespace('task.%.' . $this->getId());
foreach (DynamicFormEntry::forObject($this->getId(), ObjectModel::OBJECT_TYPE_TASK) as $form)
$form->delete();
// Log delete
$log = sprintf(__('Task #%1$s deleted by %2$s'),
$this->getNumber(),
$thisstaff ? $thisstaff->getName() : __('SYSTEM'));
if ($comments)
$log .= sprintf('<hr>%s', $comments);
$ost->logDebug(
sprintf( __('Task #%s deleted'), $this->getNumber()),
$log);
return true;
}
static function __loadDefaultForm() {
require_once INCLUDE_DIR.'class.i18n.php';
$i18n = new Internationalization();
$tpl = $i18n->getTemplate('form.yaml');
foreach ($tpl->getData() as $f) {
if ($f['type'] == ObjectModel::OBJECT_TYPE_TASK) {
$form = DynamicForm::create($f);
$form->save();
break;
}
}
}
/* Quick staff's stats */
static function getStaffStats($staff) {
global $cfg;
/* Unknown or invalid staff */
if (!$staff
|| (!is_object($staff) && !($staff=Staff::lookup($staff)))
|| !$staff->isStaff())
return null;
$where = array('(task.staff_id='.db_input($staff->getId())
.sprintf(' AND task.flags & %d != 0 ', TaskModel::ISOPEN)
.') ');
$where2 = '';
if(($teams=$staff->getTeams()))
$where[] = ' ( task.team_id IN('.implode(',', db_input(array_filter($teams)))
.') AND '
.sprintf('task.flags & %d != 0 ', TaskModel::ISOPEN)
.')';
if(!$staff->showAssignedOnly() && ($depts=$staff->getDepts())) //Staff with limited access just see Assigned tasks.
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
$where[] = 'task.dept_id IN('.implode(',', db_input($depts)).') ';
$where = implode(' OR ', $where);
if ($where) $where = 'AND ( '.$where.' ) ';
$sql = 'SELECT \'open\', count(task.id ) AS tasks '
.'FROM ' . TASK_TABLE . ' task '
. sprintf(' WHERE task.flags & %d != 0 ', TaskModel::ISOPEN)
. $where . $where2
.'UNION SELECT \'overdue\', count( task.id ) AS tasks '
.'FROM ' . TASK_TABLE . ' task '
. sprintf(' WHERE task.flags & %d != 0 ', TaskModel::ISOPEN)
. sprintf(' AND task.flags & %d != 0 ', TaskModel::ISOVERDUE)
. $where
.'UNION SELECT \'assigned\', count( task.id ) AS tasks '
.'FROM ' . TASK_TABLE . ' task '
. sprintf(' WHERE task.flags & %d != 0 ', TaskModel::ISOPEN)
.'AND task.staff_id = ' . db_input($staff->getId()) . ' '
. $where
.'UNION SELECT \'closed\', count( task.id ) AS tasks '
.'FROM ' . TASK_TABLE . ' task '
. sprintf(' WHERE task.flags & %d = 0 ', TaskModel::ISOPEN)
. $where;
$res = db_query($sql);
$stats = array();
while ($row = db_fetch_row($res))
$stats[$row[0]] = $row[1];
return $stats;
}
static function getAgentActions($agent, $options=array()) {
if (!$agent)
return;
require STAFFINC_DIR.'templates/tasks-actions.tmpl.php';
}
}
class TaskCData extends VerySimpleModel {
static $meta = array(
'pk' => array('task_id'),
'table' => TASK_CDATA_TABLE,
'joins' => array(
'task' => array(
'constraint' => array('task_id' => 'TaskModel.task_id'),
),
),
);
class TaskForm extends DynamicForm {
static $instance;
static $defaultForm;
static $internalForm;
static $forms;
static $cdata = array(
'table' => TASK_CDATA_TABLE,
'object_id' => 'task_id',
'object_type' => 'A',
);
static function objects() {
$os = parent::objects();
return $os->filter(array('type'=>ObjectModel::OBJECT_TYPE_TASK));
}
static function getDefaultForm() {
}
static function getInstance($object_id=0, $new=false) {
if ($new || !isset(static::$instance))
static::$instance = static::getDefaultForm()->instanciate();
static::$instance->object_type = ObjectModel::OBJECT_TYPE_TASK;
if ($object_id)
static::$instance->object_id = $object_id;
return static::$instance;
}
static function getInternalForm($source=null, $options=array()) {
static::$internalForm = new TaskInternalForm($source, $options);
return static::$internalForm;
}
}
class TaskInternalForm
extends AbstractForm {
static $layout = 'GridFormLayout';
function buildFields() {
'dept_id' => new DepartmentField(array(
'id'=>1,
'label' => __('Department'),
'required' => true,
'id'=>2,
'label' => __('Assignee'),
'required' => false,
)),
'duedate' => new DatetimeField(array(
'id' => 3,
'label' => __('Due Date'),
'required' => false,
'configuration' => array(
'min' => Misc::gmtime(),
'time' => true,
'gmt' => true,
'future' => true,
),
)),
);
$mode = @$this->options['mode'];
if ($mode && $mode == 'edit') {
unset($fields['dept_id']);
unset($fields['staff_id']);
}
return $fields;
}
// Task thread class
class TaskThread extends ObjectThread {
function addDescription($vars, &$errors=array()) {
$vars['threadId'] = $this->getId();
$vars['message'] = $vars['description'];
unset($vars['description']);
return MessageThreadEntry::create($vars, $errors);
}
static function create($task) {
$id = is_object($task) ? $task->getId() : $task;
$thread = parent::create(array(
'object_id' => $id,
'object_type' => ObjectModel::OBJECT_TYPE_TASK
));
if ($thread->save())
return $thread;