5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2002-2004, GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright 2009, Alcatel-Lucent
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * Standard Alcatel-Lucent disclaimer for contributing to open source
29 * "The Artifact ("Contribution") has not been tested and/or
30 * validated for release as or in products, combinations with products or
31 * other commercial use. Any use of the Contribution is entirely made at
32 * the user's own responsibility and the user can not rely on any features,
33 * functionalities or performances Alcatel-Lucent has attributed to the
36 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
37 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
38 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
39 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
40 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
41 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
42 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
43 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
44 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
47 require_once $gfcommon.'include/Error.class.php';
48 require_once $gfcommon.'tracker/ArtifactMessage.class.php';
49 require_once $gfcommon.'tracker/ArtifactExtraField.class.php';
50 require_once $gfcommon.'tracker/ArtifactWorkflow.class.php';
51 require_once $gfcommon.'tracker/ArtifactStorage.class.php';
53 // This string is used when sending the notification mail for identifying the
55 define('ARTIFACT_MAIL_MARKER', '#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+');
58 * Factory method which creates an Artifact from an artifact ID
60 * @param int $artifact_id The artifact ID
61 * @param array|bool $data The result array, if it's passed in
62 * @return Artifact Artifact object
64 function &artifact_get_object($artifact_id,$data=false) {
66 if (!isset($ARTIFACT_OBJ["_".$artifact_id."_"])) {
68 //the db result handle was passed in
70 $res = db_query_params ('SELECT * FROM artifact_vw WHERE artifact_id=$1',
71 array ($artifact_id)) ;
72 if (db_numrows($res) <1 ) {
73 $ARTIFACT_OBJ["_".$artifact_id."_"]=false;
76 $data = db_fetch_array($res);
78 $ArtifactType =& artifactType_get_object($data["group_artifact_id"]);
79 $ARTIFACT_OBJ["_".$artifact_id."_"]= new Artifact($ArtifactType,$data);
81 return $ARTIFACT_OBJ["_".$artifact_id."_"];
84 class Artifact extends Error {
89 * @var int $status_res.
94 * Artifact Type object.
96 * @var object $ArtifactType.
101 * Array of artifact data.
103 * @var array $data_array.
108 * Array of artifact data for extra fields defined by Admin.
110 * @var array $extra_field_data.
112 var $extra_field_data;
115 * Array of ArtifactFile objects.
122 * Database result set of related tasks
124 * @var result $relatedtasks
129 * Artifact - constructor.
131 * @param ArtifactType $ArtifactType The ArtifactType object.
132 * @param int|bool $data (primary key from database OR complete assoc array)
133 * ONLY OPTIONAL WHEN YOU PLAN TO IMMEDIATELY CALL ->create()
135 function __construct(&$ArtifactType, $data=false) {
138 $this->ArtifactType =& $ArtifactType;
140 // Was ArtifactType legit?
141 if (!$ArtifactType || !is_object($ArtifactType)) {
142 $this->setError(_('Invalid Artifact Type'));
146 // Did ArtifactType have an error?
147 if ($ArtifactType->isError()) {
148 $this->setError($ArtifactType->getErrorMessage());
152 // Make sure this person has permission to view artifacts
153 if (!forge_check_perm ('tracker', $this->ArtifactType->getID(), 'read')) {
154 $this->setError(_('Only project members can view private artifact types'));
159 if (is_array($data)) {
160 $this->data_array =& $data;
162 $this->fetchData($data);
168 * create - construct a new Artifact in the database.
170 * @param string $summary The artifact summary.
171 * @param string $details Details of the artifact.
172 * @param int $assigned_to The ID of the user to which this artifact is to be assigned.
173 * @param int $priority The artifacts priority.
174 * @param array $extra_fields Array of extra fields like: array(15=>'foobar',22=>'1');
175 * @param array $importData Array of data to change submitter and time of submit like:
176 * array('user' => 127, 'time' => 1234556789)
177 * @return bool id on success / false on failure.
179 function create( $summary, $details, $assigned_to=100, $priority=3, $extra_fields=array(), $importData = array()) {
181 // make sure this person has permission to add artifacts
187 if(array_key_exists('user', $importData)){
188 $user = $importData['user'];
190 if (!forge_check_perm ('tracker',$this->ArtifactType->getID(),'submit')) {
191 $this->setError(_('You are not currently allowed to submit items to this tracker.'));
195 if (session_loggedin()) {
206 $this->setError(_('Message Summary Is Required'));
210 $this->setError(_('Message Body Is Required'));
219 // if (!$status_id) {
220 $status_id=1; // on creation, status is set to "open"
223 // They may be using an extra field "status" box so we have to remap
224 // the status_id based on the extra field - this keeps the counters
227 $status_id = $this->ArtifactType->remapStatus($status_id,$extra_fields);
229 $this->setError(_('Error remapping status'));
234 if (array_key_exists('time',$importData)){
235 $time = $importData['time'];
239 $res = db_query_params ('INSERT INTO artifact
240 (group_artifact_id,status_id,priority,
241 submitted_by,assigned_to,open_date,summary,details)
242 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
243 array ($this->ArtifactType->getID(),
249 htmlspecialchars($summary),
250 htmlspecialchars($details))) ;
252 $this->setError(db_error());
257 $artifact_id=db_insertid($res,'artifact','artifact_id');
259 if (!$res || !$artifact_id) {
260 $this->setError(db_error());
265 // Now set up our internal data structures
267 if (!$this->fetchData($artifact_id)) {
271 // the changes to the extra fields will be logged in this array.
272 // (we won't use it however)
273 $extra_field_changes = array();
274 if (!$this->updateExtraFields($extra_fields,$extra_field_changes)) {
280 // now send an email if appropriate
282 $this->mailFollowupEx(0, 1);
290 * fetchData - re-fetch the data for this Artifact from the database.
292 * @param int $artifact_id The artifact ID.
293 * @return boolean success.
295 function fetchData($artifact_id) {
296 $res = db_query_params ('SELECT * FROM artifact_vw WHERE artifact_id=$1 AND group_artifact_id=$2',
298 $this->ArtifactType->getID())) ;
299 if (!$res || db_numrows($res) < 1) {
300 $this->setError(_('Invalid Artifact ID'));
303 $this->data_array = db_fetch_array($res);
304 db_free_result($res);
309 * getArtifactType - get the ArtifactType Object this Artifact is associated with.
311 * @return object ArtifactType.
313 function &getArtifactType() {
314 return $this->ArtifactType;
318 * getID - get this ArtifactID.
320 * @return int The artifact_id #.
323 return $this->data_array['artifact_id'];
327 * getStringID - get a string display for this ArtifactID.
329 * @return string The artifact_id #.
331 function getStringID() {
332 return '[#'.$this->data_array['artifact_id'].']';
336 * getStatusID - get open/closed/deleted flag.
338 * @return int Status: (1) Open, (2) Closed, (3) Deleted.
340 function getStatusID() {
341 return $this->data_array['status_id'];
345 * getStatusName - get open/closed/deleted text.
347 * @return string The status name.
349 function getStatusName() {
350 return $this->data_array['status_name'];
354 * getCustomStatusName - get custom status value text.
356 * @return string The custom status name.
358 function getCustomStatusName() {
359 $custom_status_id = $this->ArtifactType->getCustomStatusField();
360 if ($custom_status_id) {
361 $result = db_query_params ('SELECT element_name FROM artifact_extra_field_elements aefe, artifact_extra_field_data aefd
362 WHERE artifact_id=$1 AND aefd.extra_field_id=$2 AND CAST(aefd.field_data AS INTEGER)=aefe.element_id',
363 array ($this->getID(), $custom_status_id)) ;
365 return db_result($result, 0, 'element_name');
368 return $this->data_array['status_name'];
372 * getPriority - get priority flag.
374 * @return int priority.
376 function getPriority() {
377 return $this->data_array['priority'];
381 * getSubmittedBy - get ID of submitter.
383 * @return int user_id of submitter.
385 function getSubmittedBy() {
386 return $this->data_array['submitted_by'];
390 * getSubmittedEmail - get email of submitter.
392 * @return string The email of submitter.
394 function getSubmittedEmail() {
395 return $this->data_array['submitted_email'];
399 * getSubmittedRealName - get real name of submitter.
401 * @return string The real name of submitter.
403 function getSubmittedRealName() {
404 return $this->data_array['submitted_realname'];
408 * getSubmittedUnixName - get login name of submitter.
410 * @return string The unix name of submitter.
412 function getSubmittedUnixName() {
413 return $this->data_array['submitted_unixname'];
417 * getAssignedTo - get ID of assignee.
419 * @return int user_id of assignee.
421 function getAssignedTo() {
422 return $this->data_array['assigned_to'];
426 * getAssignedEmail - get email of assignee.
428 * @return string The email of assignee.
430 function getAssignedEmail() {
431 return $this->data_array['assigned_email'];
435 * getAssignedRealName - get real name of assignee.
437 * @return string The real name of assignee.
439 function getAssignedRealName() {
440 return $this->data_array['assigned_realname'];
444 * getAssignedUnixName - get login name of assignee.
446 * @return string The unix name of assignee.
448 function getAssignedUnixName() {
449 return $this->data_array['assigned_unixname'];
453 * getOpenDate - get unix time of creation.
455 * @return int unix time.
457 function getOpenDate() {
458 return $this->data_array['open_date'];
462 * getCloseDate - get unix time of closure.
464 * @return int unix time.
466 function getCloseDate() {
467 return $this->data_array['close_date'];
471 * getLastModifiedDate - the last_modified_date of this task.
473 * @return int the last_modified_date.
475 function getLastModifiedDate() {
476 return $this->data_array['last_modified_date'];
480 * getSummary - get text summary of artifact.
482 * @return string The summary (subject).
484 function getSummary() {
485 return $this->data_array['summary'];
489 * getDetails - get text body (message) of artifact.
491 * @return string The body (message).
493 function getDetails() {
494 return $this->data_array['details'];
498 * delete - delete this tracker and all its related data.
500 * @param bool $sure I'm Sure.
501 * @return bool true/false;
503 function delete($sure) {
505 $this->setMissingParamsError(_('Please tick all checkboxes.'));
508 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
509 $this->setPermissionDeniedError();
513 $res = db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1',
514 array ($this->getID())) ;
516 $this->setError(_('Error deleting extra field data: ').db_error());
521 ArtifactStorage::instance()->deleteFromQuery('SELECT id FROM artifact_file WHERE artifact_id=$1',
522 array ($this->getID())) ;
524 $res = db_query_params ('DELETE FROM artifact_file WHERE artifact_id=$1',
525 array ($this->getID())) ;
527 $this->setError(_('Error deleting file from db: ').db_error());
529 ArtifactStorage::instance()->rollback();
532 $res = db_query_params ('DELETE FROM artifact_message WHERE artifact_id=$1',
533 array ($this->getID())) ;
535 $this->setError(_('Error deleting message: ').db_error());
537 ArtifactStorage::instance()->rollback();
540 $res = db_query_params ('DELETE FROM artifact_history WHERE artifact_id=$1',
541 array ($this->getID())) ;
543 $this->setError(_('Error deleting history: ').db_error());
545 ArtifactStorage::instance()->rollback();
548 $res = db_query_params ('DELETE FROM artifact_monitor WHERE artifact_id=$1',
549 array ($this->getID())) ;
551 $this->setError(_('Error deleting monitor: ').db_error());
553 ArtifactStorage::instance()->rollback();
556 $res = db_query_params ('DELETE FROM artifact WHERE artifact_id=$1',
557 array ($this->getID())) ;
559 $this->setError(_('Error deleting artifact: ').db_error());
561 ArtifactStorage::instance()->rollback();
565 if ($this->getStatusID() == 1) {
566 $res = db_query_params ('UPDATE artifact_counts_agg SET count=count-1,open_count=open_count-1
567 WHERE group_artifact_id=$1',
568 array ($this->getID())) ;
570 $this->setError(_('Error updating artifact counts: ').db_error());
572 ArtifactStorage::instance()->rollback();
575 } elseif ($this->getStatusID() == 2) {
576 $res = db_query_params ('UPDATE artifact_counts_agg SET count=count-1
577 WHERE group_artifact_id=$1',
578 array ($this->getID())) ;
580 $this->setError(_('Error updating artifact counts: ').db_error());
582 ArtifactStorage::instance()->rollback();
588 ArtifactStorage::instance()->commit();
593 * setMonitor - user can monitor this artifact.
595 * @return bool Always false - always use the getErrorMessage() for feedback
597 function setMonitor() {
598 if (session_loggedin()) {
599 $user_id=user_getid();
601 $this->setError(_('Valid Email Address Required'));
605 $res = db_query_params ('SELECT * FROM artifact_monitor WHERE artifact_id=$1 AND user_id=$2',
606 array ($this->getID(),
609 if (!$res || db_numrows($res) < 1) {
611 $res = db_query_params ('INSERT INTO artifact_monitor (artifact_id,user_id) VALUES ($1,$2)',
612 array ($this->getID(),
615 $this->setError(db_error());
618 $this->setError(_('Monitoring Started'));
622 //already monitoring - remove their monitor
623 db_query_params ('DELETE FROM artifact_monitor
626 array ($this->getID(),
628 $this->setError(_('Monitoring Stopped'));
633 function isMonitoring() {
634 if (!session_loggedin()) {
637 $result = db_query_params ('SELECT count(*) AS count FROM artifact_monitor WHERE user_id=$1 AND artifact_id=$2',
640 $row_count = db_fetch_array($result);
641 return $result && $row_count['count'] > 0;
645 * getMonitorIds - array of email addresses monitoring this Artifact.
647 * @return array of email addresses monitoring this Artifact.
649 function getMonitorIds() {
650 $res = db_query_params ('SELECT user_id FROM artifact_monitor WHERE artifact_id=$1',
651 array ($this->getID())) ;
652 return array_unique(array_merge($this->ArtifactType->getMonitorIds(),util_result_column_to_array($res)));
656 * getHistory - returns a result set of audit trail for this support request.
658 * @return resource result set.
660 function getHistory() {
661 return db_query_params ('SELECT * FROM artifact_history_user_vw WHERE artifact_id=$1 ORDER BY entrydate DESC, id ASC',
662 array ($this->getID())) ;
666 * getMessages - get the list of messages attached to this artifact.
668 * @param string $order
669 * @return resource result set.
671 function getMessages($ascending='up') {
673 * This is necessary because someone committed a change
674 * to this method in FusionForge trunk that accepts 'up'
675 * as default (luckily, it’s the same!) and 'down' as
676 * alternative probability, whereas FusionForge 5.2 has
677 * false as default and true for ascending order, so we
678 * need to check this out and use === to be sure ☹
680 if ($ascending === 'up') {
682 } elseif ($ascending === true) {
684 } elseif ($ascending === false) {
689 return db_query_params('SELECT * FROM artifact_message_user_vw WHERE artifact_id=$1 ORDER BY adddate ' . $order . ', id ASC',
690 array($this->getID()));
694 * getMessage - get a message attached to this artifact.
696 * @param int $msg_id id of the message.
698 * @return database result set.
700 function getMessage($msg_id) {
704 return db_query_params ('SELECT * FROM artifact_message_user_vw WHERE id=$1',
709 * getMessageObjects - get an array of message objects.
711 * @return array Of ArtifactMessage objects.
713 function &getMessageObjects() {
714 $res=$this->getMessages();
716 while ($arr = db_fetch_array($res)) {
717 //$return[]=new ArtifactMessage($arr['artifact_id'],$arr);
718 $return[] = new ArtifactMessage($this, $arr);
724 * getFiles - get array of ArtifactFile's.
726 * @return array of ArtifactFile's.
728 function &getFiles() {
729 if (!isset($this->files)) {
730 $res = db_query_params ('SELECT id,artifact_id,description,filename,filesize,' .
731 'filetype,adddate,submitted_by,user_name,realname
732 FROM artifact_file_user_vw WHERE artifact_id=$1',
733 array ($this->getID())) ;
734 $rows=db_numrows($res);
736 for ($i=0; $i < $rows; $i++) {
737 $this->files[$i]=new ArtifactFile($this,db_fetch_array($res));
740 $this->files=array();
747 * getRelatedTasks - get array of related tasks
749 * @return resource Database result set
751 function getRelatedTasks() {
752 if (!$this->relatedtasks) {
753 $this->relatedtasks = db_query_params ('SELECT pt.group_project_id,pt.project_task_id,pt.summary,pt.start_date,pt.end_date,pgl.group_id,pt.status_id,pt.percent_complete,ps.status_name
754 FROM project_task pt, project_group_list pgl, project_status ps
755 WHERE pt.group_project_id = pgl.group_project_id
756 AND ps.status_id = pt.status_id
757 AND EXISTS (SELECT project_task_id FROM project_task_artifact
758 WHERE project_task_id=pt.project_task_id
759 AND artifact_id = $1)',
760 array ($this->getID())) ;
762 return $this->relatedtasks;
766 * addMessage - attach a text message to this Artifact.
768 * @param string $body The $string message being attached.
769 * @param bool $by Email $string address of message creator.
770 * @param bool $send_followup Whether $bool to email out a followup.
771 * @return bool success.
773 function addMessage($body,$by=false,$send_followup=false) {
775 $this->setMissingParamsError();
778 if (!forge_check_perm ('tracker',$this->ArtifactType->getID(),'submit')) {
779 $this->setError(_('You are not currently allowed to submit items to this tracker.'));
782 if (session_loggedin()) {
783 $user_id=user_getid();
784 $user = user_get_object($user_id);
785 if (!$user || !is_object($user)) {
786 $this->setError('Error: Logged In User But Could Not Get User Object');
789 // we'll store this email even though it will likely never be used -
790 // since we have their correct user_id, we can join the USERS table to get email
791 $by=$user->getEmail();
794 if (!$by || !validate_email($by)) {
795 $this->setMissingParamsError();
801 $res = db_query_params ('INSERT INTO artifact_message (artifact_id,submitted_by,from_email,adddate,body) VALUES ($1,$2,$3,$4,$5)',
802 array ($this->getID(),
806 htmlspecialchars($body))) ;
808 $this->updateLastModifiedDate();
810 if ($send_followup) {
811 $this->mailFollowupEx($now, 2, false);
817 * addHistory - add an entry to audit trail.
819 * @param string $field_name The name of the field in the database being modified.
820 * @param string $old_value The former value of this field.
821 * @param array $importData Array of data to change submitter and time of submit like:
822 * array('user' => 127, 'time' => 1234556789)
824 * @return boolean success.
826 function addHistory($field_name,$old_value, $importData = array()) {
827 if (array_key_exists('user', $importData)){
828 $user = $importData['user'];
830 if (!session_loggedin()) {
836 if (array_key_exists('time',$importData)){
837 $time = $importData['time'];
841 return db_query_params ('INSERT INTO artifact_history(artifact_id,field_name,old_value,mod_by,entrydate) VALUES ($1,$2,$3,$4,$5)',
842 array ($this->getID(),
850 * setStatus - set the status of this artifact.
852 * @param int The artifact status ID.
853 * @param int Closing date if status = 1
855 * @return boolean success.
857 function setStatus($status_id, $closingTime=False) {
859 $qpa = db_construct_qpa (false, 'UPDATE artifact SET status_id=$1', array ($status_id)) ;
860 if ($closingTime && $status_id != 1) {
862 $qpa = db_construct_qpa ($qpa, ', close_date=$1 ', array ($time)) ;
864 $qpa = db_construct_qpa ($qpa,
865 'WHERE artifact_id=$1 AND group_artifact_id=$2',
866 array ($this->getID(), $artifact_type_id)) ;
867 $result=db_query_qpa($qpa);
869 if (!$result || db_affected_rows($result) < 1) {
870 $this->setError('Error - update failed!'.db_error());
874 if (!$this->fetchData($this->getID())) {
886 * update - update the fields in this artifact.
888 * @param int $priority The artifact priority.
889 * @param int $status_id The artifact status ID.
890 * @param int $assigned_to The person to which this artifact is to be assigned.
891 * @param string $summary The artifact summary.
892 * @param int $canned_response The canned response.
893 * @param string $details Attaching another comment.
894 * @param int $new_artifact_type_id Allows you to move an artifact to another type.
895 * @param array $extra_fields Array of extra fields like: array(15=>'foobar',22=>'1');
896 * @param string $description The description.
897 * @return boolean success.
899 function update($priority,$status_id,
900 $assigned_to,$summary,$canned_response,$details,$new_artifact_type_id,
901 $extra_fields=array(), $description='') {
904 Field-level permission checking
906 if (!forge_check_perm ('tracker', $this->ArtifactType->getID(), 'manager')) {
907 // Non-managers cannot modify these fields
908 $priority=$this->getPriority();
909 $summary=htmlspecialchars_decode($this->getSummary());
910 $description=htmlspecialchars_decode($this->getDetails());
911 $canned_response=100;
912 $new_artifact_type_id=$this->ArtifactType->getID();
913 $assigned_to=$this->getAssignedTo();
915 if (!forge_check_perm ('tracker', $this->ArtifactType->getID(), 'tech')) {
916 $this->setPermissionDeniedError();
921 // They may be using an extra field "status" box so we have to remap
922 // the status_id based on the extra field - this keeps the counters
925 if (count($extra_fields) > 0) {
926 $status_id=$this->ArtifactType->remapStatus($status_id,$extra_fields);
928 if (!$this->getID()) {
929 $this->setMissingParamsError('ID');
933 $this->setMissingParamsError(_('Assigned to'));
937 $this->setMissingParamsError(_('State'));
940 if (!$canned_response) {
941 $this->setMissingParamsError(_('Canned Response'));
944 if (!$new_artifact_type_id) {
945 $this->setMissingParamsError(_('Data Type'));
949 // Check that assigned_to is a tech for the tracker
950 if ($assigned_to != 100) {
951 if (!forge_check_perm_for_user ($assigned_to, 'tracker', $this->ArtifactType->getID(), 'tech')) {
952 $this->setError(_("Invalid assigned_to (assigned person is not a technician)"));
957 // Array to record which properties were changed
964 // Get a lock on this row in the database
966 db_query_params ('SELECT * FROM artifact WHERE artifact_id=$1 FOR UPDATE', array ($this->getID())) ;
967 $artifact_type_id = $this->ArtifactType->getID();
969 // Attempt to move this Artifact to a new ArtifactType
970 // need to instantiate new ArtifactType obj and test perms
972 if ($new_artifact_type_id != $artifact_type_id) {
973 $newArtifactType= new ArtifactType($this->ArtifactType->getGroup(), $new_artifact_type_id);
974 if (!is_object($newArtifactType) || $newArtifactType->isError()) {
975 $this->setError(_('Could not move to new Artifact Type'). $newArtifactType->getErrorMessage());
979 // do they have perms for new ArtifactType?
980 if (!forge_check_perm ('tracker', $newArtifactType->getID(), 'manager')) {
981 $this->setPermissionDeniedError();
986 // Add a message to explain that the tracker was moved.
987 $message = sprintf(_('Moved from %1$s to %2$s'),
988 $this->ArtifactType->getName(),
989 $newArtifactType->getName());
990 $this->addHistory('type', $this->ArtifactType->getName());
991 $this->addMessage($message,'',0);
993 // Fake change to send a mail when moved.
994 $changes['Type'] = 1;
996 // Try to remap extra_fields values when possible.
997 // If there is an extra_field with the same alias
998 // and if the value exist in the new one, then recode
999 // the value to keep it.
1000 $new_extra_fields = array();
1001 $ef = $this->ArtifactType->getExtraFields();
1002 $ef_new = $newArtifactType->getExtraFields();
1003 foreach($extra_fields as $extra_id => $value) {
1004 $alias = preg_replace('/^@/', '', $ef[$extra_id]['alias']);
1005 $type = $ef[$extra_id]['field_type'];
1007 // Search if there is an extra field with the same alias.
1009 foreach($ef_new as $id => $arr) {
1010 if (preg_replace('/^@/', '', $arr['alias']) == $alias) {
1015 // If we found one, copy for simple fields or
1016 // search if there is the same value.
1018 if ($type == ARTIFACT_EXTRAFIELDTYPE_TEXT ||
1019 $type == ARTIFACT_EXTRAFIELDTYPE_INTEGER ||
1020 $type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA ||
1021 $type == ARTIFACT_EXTRAFIELDTYPE_RELATION) {
1022 $new_extra_fields[$new_id] = $value;
1024 $values = $newArtifactType->getExtraFieldElements($new_id);
1025 if (is_array($value)) {
1026 foreach($value as $v) {
1027 $v = $this->ArtifactType->getElementName($v);
1028 foreach($values as $ev) {
1029 if ($ev['element_name'] == $v) {
1030 $new_extra_fields[$new_id][] = $ev['element_id'];
1035 $value = $this->ArtifactType->getElementName($value);
1036 foreach($values as $ev) {
1037 if ($ev['element_name'] == $value) {
1038 $new_extra_fields[$new_id] = $ev['element_id'];
1046 // Special case if moving to a tracker with custom status (previous has not).
1047 $custom_status_id = $newArtifactType->getCustomStatusField();
1048 if ($custom_status_id && !$new_extra_fields[$custom_status_id]) {
1049 $atw = new ArtifactWorkflow($newArtifactType, $custom_status_id);
1050 $nodes = $atw->getNextNodes(100);
1052 $new_extra_fields[$custom_status_id] = $nodes[0];
1056 $extra_fields = $new_extra_fields;
1058 $res = db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1',
1059 array ($this->getID()));
1061 $this->setError(_('Removal of old artifact_extra_field_data failed: ').db_error());
1066 // Check that assigned_to is a tech in the new tracker
1067 if ($assigned_to != 100) {
1068 if (!forge_check_perm ('tracker', $newArtifactType->getID(), 'tech')) {
1073 //can't send a canned response when changing ArtifactType
1074 $canned_response=100;
1075 $this->ArtifactType =& $newArtifactType;
1079 $qpa = db_construct_qpa();
1080 $qpa = db_construct_qpa($qpa, 'UPDATE artifact SET');
1083 // handle audit trail
1086 if ($this->getStatusID() != $status_id) {
1087 $this->addHistory('status_id',$this->getStatusID());
1088 $qpa = db_construct_qpa($qpa, ' status_id=$1,', array($status_id));
1089 $changes['status'] = 1;
1092 if ($status_id != 1) {
1093 $qpa = db_construct_qpa($qpa, ' close_date=$1,', array($now));
1095 $qpa = db_construct_qpa($qpa, ' close_date=$1,', array(0));
1097 $this->addHistory('close_date', $this->getCloseDate());
1099 if ($this->getPriority() != $priority) {
1100 $this->addHistory('priority',$this->getPriority());
1101 $qpa = db_construct_qpa($qpa, ' priority=$1,', array($priority));
1102 $changes['priority'] = 1;
1106 if ($this->getAssignedTo() != $assigned_to) {
1107 $this->addHistory('assigned_to',$this->getAssignedTo());
1108 $qpa = db_construct_qpa($qpa, ' assigned_to=$1,', array($assigned_to));
1109 $changes['assigned_to'] = 1;
1112 if ($summary && ($this->getSummary() != htmlspecialchars($summary))) {
1113 $this->addHistory('summary', $this->getSummary());
1114 $qpa = db_construct_qpa($qpa, ' summary=$1,', array(htmlspecialchars($summary)));
1115 $changes['summary'] = 1;
1118 if ($description && ($this->getDetails() != htmlspecialchars($description))) {
1119 $this->addHistory('details', $this->getDetails());
1120 $qpa = db_construct_qpa($qpa, ' details=$1,', array(htmlspecialchars($description)));
1121 $changes['details'] = 1;
1125 $this->addMessage($details,'',0);
1126 $changes['details'] = 1;
1131 Finally, update the artifact itself
1134 $qpa = db_construct_qpa($qpa, ' group_artifact_id=$1
1135 WHERE artifact_id=$2 AND group_artifact_id=$3',
1136 array($new_artifact_type_id,
1137 $this->getID(), $artifact_type_id));
1138 $result = db_query_qpa($qpa);
1140 if (!$result || db_affected_rows($result) < 1) {
1141 $this->setError(_('Update failed').db_error());
1145 if (!$this->fetchData($this->getID())) {
1152 //extra field handling
1154 if (!$this->updateExtraFields($extra_fields,$changes)) {
1155 //TODO - see if anything actually did change
1161 handle canned responses
1163 Instantiate ArtifactCanned and get the body of the message
1165 if ($canned_response != 100) {
1166 //don't care if this response is for this group - could be hacked
1167 $acr=new ArtifactCanned($this->ArtifactType,$canned_response);
1168 if (!$acr || !is_object($acr)) {
1169 $this->setError(_('Could Not Create Canned Response Object'));
1170 } elseif ($acr->isError()) {
1171 $this->setError($acr->getErrorMessage());
1173 $body = $acr->getBody();
1175 if (!$this->addMessage(util_unconvert_htmlspecialchars($body),'',0)) {
1182 $this->setError(_('Unable to Use Canned Response'));
1188 if ($update || $send_message){
1189 if (!empty($changes)) {
1190 // Send the email with changes
1191 $this->mailFollowupEx($now, 2, false, $changes);
1196 //nothing changed, so cancel the transaction
1197 $this->setError(_('Nothing Changed - Update Cancelled'));
1205 * updateLastModifiedDate - update the last_modified_date attribute of this artifact.
1207 * @return bool true on success / false on failure
1209 function updateLastModifiedDate() {
1210 $res = db_query_params ('UPDATE artifact SET last_modified_date=EXTRACT(EPOCH FROM now())::integer WHERE artifact_id=$1',
1211 array ($this->getID()));
1216 * assignToMe - assigns this artifact to current user
1218 * @return bool true on success / false on failure
1220 function assignToMe() {
1221 if (!session_loggedin() || !($this->ArtifactType->userIsAdmin() || $this->ArtifactType->userIsTechnician())) {
1222 $this->setPermissionDeniedError();
1226 $user_id = user_getid();
1227 $res = db_query_params ('UPDATE artifact SET assigned_to=$1 WHERE artifact_id=$2',
1228 array ($user_id, $this->getID())) ;
1230 $this->setError(_('Error updating assigned_to in artifact: ').db_error());
1233 $this->fetchData($this->getID());
1239 * updateExtraFields - updates the extra data elements for this artifact
1240 * e.g. the extra fields created and defined by the admin.
1242 * @param array Array of extra fields like: array(15=>'foobar',22=>'1');
1243 * @param array Array where changes to the extra fields should be logged
1244 * @return bool true on success / false on failure
1246 function updateExtraFields($extra_fields,&$changes){
1248 This is extremely complex code - we have take the passed array
1249 and see if we need to insert it into the db, and may have to
1250 add history rows for the audit trail
1252 start by getting all the available extra fields from ArtifactType
1253 For each field from ArtifacType, check the passed array -
1254 This prevents someone from passing bogus extra field entries - they will be ignored
1255 if the passed entry is blank, may have to force a default value
1256 if the passed array is different from the existing data in db,
1257 delete old entry and insert new entries, along with possible audit trail
1259 skip it and continue to next item
1264 //get a list of extra fields for this artifact_type
1265 $ef = $this->ArtifactType->getExtraFields();
1266 $efk=array_keys($ef);
1268 if (empty($extra_fields) && empty($ef)) {
1272 // If there is a status field, then check against the workflow.
1273 // Unless if we change type.
1274 if (! isset($changes['Type']) || !$changes['Type']) {
1275 for ($i=0; $i<count($efk); $i++) {
1277 $type=$ef[$efid]['field_type'];
1278 if ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
1279 // Get previous value.
1280 $res = db_query_params ('SELECT field_data FROM artifact_extra_field_data
1281 WHERE artifact_id=$1 AND extra_field_id=$2',
1282 array($this->getID(),
1284 $old = (db_numrows($res)>0) ? db_result($res,0,'field_data') : 100;
1285 if ($old != $extra_fields[$efid]) {
1286 $atw = new ArtifactWorkflow($this->ArtifactType, $efid);
1287 if (!$atw->checkEvent($old, $extra_fields[$efid])) {
1288 $this->setError('Workflow error: You are not authorized to change the Status ('.$old.' => '.$extra_fields[$efid].')');
1296 //now we'll update this artifact for each extra field
1297 for ($i=0; $i<count($efk); $i++) {
1299 $type=$ef[$efid]['field_type'];
1301 // check required fields
1302 if ($ef[$efid]['is_required']) {
1303 if (!array_key_exists($efid, $extra_fields)) {
1304 if ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
1305 $this->setError(_('Status Custom Field Must Be Set'));
1308 $this->setMissingParamsError($ef[$efid]['field_name']);
1313 if ($extra_fields[$efid] === '') {
1314 if ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
1315 $this->setError(_('Status Custom Field Must Be Set'));
1318 $this->setMissingParamsError($ef[$efid]['field_name']);
1323 if (($type == ARTIFACT_EXTRAFIELDTYPE_SELECT || $type == ARTIFACT_EXTRAFIELDTYPE_RADIO) &&
1324 $extra_fields[$efid] == '100') {
1325 $this->setMissingParamsError($ef[$efid]['field_name']);
1328 elseif (($type == ARTIFACT_EXTRAFIELDTYPE_MULTISELECT || $type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) &&
1329 (count($extra_fields[$efid]) == 1 && $extra_fields[$efid][0] == '100')) {
1330 $this->setMissingParamsError($ef[$efid]['field_name']);
1337 // Force each field to have some value if it is a numeric field
1338 // text fields will just be purged and skipped
1340 if (!array_key_exists($efid, $extra_fields) || $extra_fields[$efid] === '') {
1341 if (($type == ARTIFACT_EXTRAFIELDTYPE_SELECT) || ($type == ARTIFACT_EXTRAFIELDTYPE_RADIO)) {
1342 $extra_fields[$efid]='100';
1343 } elseif (($type == ARTIFACT_EXTRAFIELDTYPE_MULTISELECT) || ($type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX)) {
1344 $extra_fields[$efid]=array('100');
1346 db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1347 array ($this->getID(),
1354 // get the old rows of data
1356 $resd = db_query_params ('SELECT * FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1357 array ($this->getID(),
1359 $rows=db_numrows($resd);
1360 if ($resd && $rows) {
1362 //POTENTIAL PROBLEM - no entry was there before, but adding one now - may need history
1365 // Compare for history purposes
1368 // these types have arrays associated to them, so they need
1369 // special handling to check for differences
1370 if ($type == ARTIFACT_EXTRAFIELDTYPE_MULTISELECT || $type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) {
1371 // check the differences between the old values and the new values
1372 $old_values = util_result_column_to_array($resd,"field_data");
1374 $added_values = array_diff($extra_fields[$efid], $old_values);
1375 $deleted_values = array_diff($old_values, $extra_fields[$efid]);
1377 if (!empty($added_values) || !empty($deleted_values)) { // there are differences...
1378 $field_name = $ef[$efid]['field_name'];
1379 if (!preg_match('/^@/', $ef[$efid]['alias'])) {
1380 $changes["extra_fields"][$efid] = 1;
1383 $this->addHistory($field_name, $this->ArtifactType->getElementName(array_reverse($old_values)));
1386 db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1387 array ($this->getID(),
1392 } elseif (db_result($resd,0,'field_data') == htmlspecialchars($extra_fields[$efid])) {
1393 //element did not change
1396 //element DID change - do a history entry
1397 $field_name = $ef[$efid]['field_name'];
1398 if (!preg_match('/^@/', $ef[$efid]['alias'])) {
1399 $changes["extra_fields"][$efid] = 1;
1401 db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1402 array ($this->getID(),
1405 // Adding history with previous value.
1406 if (($type == ARTIFACT_EXTRAFIELDTYPE_SELECT) || ($type == ARTIFACT_EXTRAFIELDTYPE_RADIO) || ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS)) {
1407 $this->addHistory($field_name,$this->ArtifactType->getElementName(db_result($resd,0,'field_data')));
1409 $this->addHistory($field_name, db_result($resd,0,'field_data'));
1415 //no history for this extra field exists
1420 // Some rewrite & consistency checks on the relation type field.
1422 // 1) Convert syntax [#NNN] to NNN
1423 // 2) Allow multiple spaces as separator.
1424 // 3) Ensure that only integers are given.
1425 // 4) Ensure that id corresponds to valid tracker id.
1427 if ($type == ARTIFACT_EXTRAFIELDTYPE_RELATION) {
1428 $value = preg_replace('/\[\#(\d+)\]/', "\\1", trim($extra_fields[$efid]));
1429 $value = preg_replace('/\\s+/', ' ', $value);
1431 foreach (explode(' ',$value) as $id) {
1432 if (preg_match('/^(\d+)$/', $id)) {
1433 // Control that the id is present in the db
1435 $res = db_query_params ('SELECT artifact_id FROM artifact WHERE artifact_id=$1',
1437 if (db_numrows($res) == 1) {
1440 $this->setError('Illegal id '.$id.', it\'s not a valid tracker id for field: '.$ef[$efid]['field_name'].'.'); // @todo: lang
1444 $this->setError('Illegal value '.$id.', only trackers id are allowed for field: '.$ef[$efid]['field_name'].'.'); // @todo: lang
1448 $extra_fields[$efid] = trim($new);
1451 // Ensure that only integer are allowed for type ARTIFACT_EXTRAFIELDTYPE_INTEGER
1452 if ($type == ARTIFACT_EXTRAFIELDTYPE_INTEGER) {
1453 $extra_fields[$efid] = trim($extra_fields[$efid]);
1454 if (!preg_match('/^[-+]?(\d+)$/', $extra_fields[$efid])) {
1455 $this->setError('Illegal value '.$extra_fields[$efid].' for field '.$ef[$efid]['field_name'].': Only integer is allowed.');
1458 if ($extra_fields[$efid] < -2147483648 || $extra_fields[$efid] > 2147483647) {
1459 $this->setError('Illegal value '.$extra_fields[$efid].' for field '.$ef[$efid]['field_name'].': Integer out of range (-2147483648 to +2147483647).');
1462 $extra_fields[$efid] = intval($extra_fields[$efid]);
1466 // See if anything was even passed for this extra_field_id
1468 if ($extra_fields[$efid] === '') {
1469 //nothing in field to update - text fields may be blank
1471 //determine the type of field and whether it should have multiple rows supporting it
1472 $type=$ef[$efid]['field_type'];
1473 if (($type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) || ($type==ARTIFACT_EXTRAFIELDTYPE_MULTISELECT)) {
1475 $count=count($extra_fields[$efid]);
1476 for ($fin=0; $fin<$count; $fin++) {
1477 $res = db_query_params ('INSERT INTO artifact_extra_field_data (artifact_id,extra_field_id,field_data) VALUES ($1,$2,$3)',
1478 array ($this->getID(),
1480 $extra_fields[$efid][$fin])) ;
1482 $this->setError(db_error());
1489 $res = db_query_params ('INSERT INTO artifact_extra_field_data (artifact_id,extra_field_id,field_data) VALUES ($1,$2,$3)',
1490 array ($this->getID(),
1492 htmlspecialchars($extra_fields[$efid]))) ;
1494 $this->setError(db_error());
1501 unset($this->extra_field_data);
1504 $this->updateLastModifiedDate();
1510 * getExtraFieldData - get an array of data for the extra fields associated with this artifact
1512 * @return array array of data
1514 function &getExtraFieldData() {
1515 if (!isset($this->extra_field_data)) {
1516 $this->extra_field_data = array();
1517 $res = db_query_params ('SELECT * FROM artifact_extra_field_data WHERE artifact_id=$1 ORDER BY extra_field_id',
1518 array ($this->getID())) ;
1519 $ef = $this->ArtifactType->getExtraFields();
1520 while ($arr = db_fetch_array($res)) {
1521 $type=$ef[$arr['extra_field_id']]['field_type'];
1522 if (($type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) || ($type==ARTIFACT_EXTRAFIELDTYPE_MULTISELECT)) {
1523 //accumulate a sub-array of values in cases where you may have multiple rows
1524 if (!array_key_exists($arr['extra_field_id'], $this->extra_field_data) || !is_array($this->extra_field_data[$arr['extra_field_id']])) {
1525 $this->extra_field_data[$arr['extra_field_id']] = array();
1527 $this->extra_field_data[$arr['extra_field_id']][]=$arr['field_data'];
1529 $this->extra_field_data[$arr['extra_field_id']]=$arr['field_data'];
1533 return $this->extra_field_data;
1537 * marker - adds the > symbol to fields that have been modified for the email message
1541 function marker($prop_name,$changes,$extra_field_id=0) {
1542 if ($prop_name == 'extra_fields' && isset($changes[$prop_name][$extra_field_id])) {
1544 } elseif ($prop_name != 'extra_fields' && isset($changes[$prop_name])) {
1552 * mailFollowupEx - send out an email update for this artifact.
1554 * @param time_t Time of the change
1555 * @param int (1) initial/creation (2) update.
1556 * @param array Array of additional addresses to mail to.
1557 * @param array Array of fields changed in this update .
1559 * @return boolean success.
1561 function mailFollowupEx($tm, $type, $more_addresses = false, $changes='') {
1563 $monitor_ids = array();
1569 $sess = session_get_user() ;
1570 if ($type == 1) { // Initial opening
1572 $body = $this->ArtifactType->Group->getUnixName() . '-' . $this->ArtifactType->getName() ." item #". $this->getID() .", was opened at ". date( _('Y-m-d H:i'), $this->getOpenDate() ) . " by " . $sess->getRealName () ;
1574 $body = $this->ArtifactType->Group->getUnixName() . '-' . $this->ArtifactType->getName() ." item #". $this->getID() .", was opened at ". date( _('Y-m-d H:i'), $this->getOpenDate() ) ;
1578 $body = $this->ArtifactType->Group->getUnixName() . '-' . $this->ArtifactType->getName() .
1579 " item #" . $this->getID() .
1580 " was changed at " .
1581 date(_('Y-m-d H:i'), $tm) . " by " .
1582 $sess->getRealName();
1584 $body = $this->ArtifactType->Group->getUnixName() . '-' . $this->ArtifactType->getName() .
1585 " item #" . $this->getID() .
1586 " was changed at " .
1587 date(_('Y-m-d H:i'), $tm);
1591 $body .= "\nYou can respond by visiting: ".
1592 "\n".util_make_url ('/tracker/?func=detail&atid='. $this->ArtifactType->getID() .
1593 "&aid=". $this->getID() .
1594 "&group_id=". $this->ArtifactType->Group->getID()) .
1595 "\nOr by replying to this e-mail entering your response between the following markers: ".
1596 "\n".ARTIFACT_MAIL_MARKER.
1597 "\n(enter your response here, only in plain text format)".
1598 "\n".ARTIFACT_MAIL_MARKER.
1600 $this->marker('status',$changes).
1601 "Status: ". $this->getStatusName() ."\n".
1602 $this->marker('priority',$changes).
1603 "Priority: ". $this->getPriority() ."\n".
1604 "Submitted By: ". $this->getSubmittedRealName() .
1605 " (". $this->getSubmittedUnixName(). ")"."\n".
1606 $this->marker('assigned_to',$changes).
1607 "Assigned to: ". $this->getAssignedRealName() .
1608 " (". $this->getAssignedUnixName(). ")"."\n".
1609 $this->marker('summary',$changes).
1610 "Summary: ". util_unconvert_htmlspecialchars( $this->getSummary() )." \n";
1612 // Now display the extra fields
1613 $efd = $this->getExtraFieldDataText();
1614 foreach ($efd as $efid => $ef) {
1615 $body .= $this->marker('extra_fields', $changes, $efid);
1616 $body .= $ef["name"].": ".$ef["value"]."\n";
1619 $subject='['. $this->ArtifactType->Group->getUnixName() . '-' . $this->ArtifactType->getName() . '][' . $this->getID() .'] '. util_unconvert_htmlspecialchars( $this->getSummary() );
1622 // get all the email addresses that are monitoring this request or the ArtifactType
1623 $monitor_ids = $this->getMonitorIds();
1625 // initial creation, we just get the users monitoring the ArtifactType
1626 $monitor_ids = $this->ArtifactType->getMonitorIds();
1630 if ($more_addresses) {
1631 $emails[] = $more_addresses;
1633 //we don't email the current user
1634 if ($this->getAssignedTo() != user_getid()) {
1635 $monitor_ids[] = $this->getAssignedTo();
1637 if ($this->getSubmittedBy() != user_getid()) {
1638 $monitor_ids[] = $this->getSubmittedBy();
1640 //initial submission
1642 //if an email is set for this ArtifactType
1643 //add that address to the BCC: list
1644 if ($this->ArtifactType->getEmailAddress()) {
1645 $emails[] = $this->ArtifactType->getEmailAddress();
1649 if ($this->ArtifactType->emailAll()) {
1650 $emails[] = $this->ArtifactType->getEmailAddress();
1654 $body .= "\n\nInitial Comment:".
1655 "\n".util_unconvert_htmlspecialchars( $this->getDetails() ) .
1656 "\n\n----------------------------------------------------------------------";
1660 Now include the followups
1662 $result2=$this->getMessages();
1664 $rows=db_numrows($result2);
1666 if ($result2 && $rows > 0) {
1667 for ($i=0; $i<$rows; $i++) {
1669 // for messages posted by non-logged-in users,
1670 // we grab the email they gave us
1672 // otherwise we use the confirmed one from the users table
1674 if (db_result($result2,$i,'user_id') == 100) {
1675 $emails[] = db_result($result2,$i,'from_email');
1677 $monitor_ids[] = db_result($result2,$i,'user_id');
1683 $body .= $this->marker('details',$changes);
1685 $body .= "Comment By: ". db_result($result2,$i,'realname') . " (".db_result($result2,$i,'user_name').")".
1686 "\nDate: ". date( _('Y-m-d H:i'),db_result($result2,$i,'adddate') ).
1688 "\n".util_unconvert_htmlspecialchars( db_result($result2,$i,'body') ).
1689 "\n\n----------------------------------------------------------------------";
1695 $body .= "\n\nYou can respond by visiting: ".
1696 "\n".util_make_url ('/tracker/?func=detail&atid='. $this->ArtifactType->getID() .
1697 "&aid=". $this->getID() .
1698 "&group_id=". $this->ArtifactType->Group->getID());
1700 //only send if some recipients were found
1701 if (count($emails) < 1 && count($monitor_ids) < 1) {
1705 if (count($monitor_ids) < 1) {
1706 $monitor_ids=array();
1708 $monitor_ids=array_unique($monitor_ids);
1711 $from = $this->ArtifactType->getReturnEmailAddress();
1712 $extra_headers = 'Reply-to: '.$from;
1714 // load the e-mail addresses of the users
1715 $users = user_get_objects($monitor_ids);
1716 if (count($users) > 0) {
1717 foreach ($users as $user) {
1718 if ($user->getStatus() == "A") { //we are only sending emails to active users
1719 $emails[] = $user->getEmail();
1724 //now remove all duplicates from the email list
1725 if (count($emails) > 0) {
1726 $BCC=implode(',',array_unique($emails));
1727 util_send_message('',$subject,$body,$from,$BCC,'',$extra_headers);
1730 $this->sendSubjectMsg = $subject;
1731 $this->sendBodyMsg = $body;
1733 //util_handle_message($monitor_ids,$subject,$body,$BCC);
1739 * getExtraFieldDataText - Return the extra fields' data in a human-readable form.
1741 * @return array Array containing field ID => field name and value associated to it for
1744 function getExtraFieldDataText() {
1745 // First we get the list of extra fields and the data
1746 // associated to the fields
1747 $efs = $this->ArtifactType->getExtraFields();
1748 $efd = $this->getExtraFieldData();
1752 foreach ($efs as $efid => $ef) {
1753 $name = $ef["field_name"];
1754 $type = $ef["field_type"];
1756 // Get the value according to the type
1759 // for these types, the associated value comes straight
1760 case ARTIFACT_EXTRAFIELDTYPE_TEXT:
1761 case ARTIFACT_EXTRAFIELDTYPE_TEXTAREA:
1762 case ARTIFACT_EXTRAFIELDTYPE_RELATION:
1763 case ARTIFACT_EXTRAFIELDTYPE_INTEGER:
1764 if (isset($efd[$efid])) {
1765 $value = $efd[$efid];
1771 // the other types have and ID or an array of IDs associated to them
1773 if (isset($efd[$efid])) {
1774 $value = $this->ArtifactType->getElementName($efd[$efid]);
1780 $return[$efid] = array("name" => $name, "value" => $value, 'type' => $type);
1787 class ArtifactComparator {
1788 var $criterion = 'artifact_id' ;
1789 var $order = 'ASC' ;
1791 function Compare ($a, $b) {
1792 if ($this->order == 'DESC') {
1793 $c = $a ; $a = $b ; $b = $c ;
1795 switch ($this->criterion) {
1797 $namecmp = strcoll ($a->getSummary(),
1799 if ($namecmp != 0) {
1804 $namecmp = strcoll (user_get_object($a->getAssignedTo())->getRealName(),
1805 user_get_object($b->getAssignedTo())->getRealName()) ;
1806 if ($namecmp != 0) {
1810 case 'submitted_by':
1811 $namecmp = strcoll (user_get_object($a->getSubmittedBy())->getRealName(),
1812 user_get_object($b->getSubmittedBy())->getRealName()) ;
1813 if ($namecmp != 0) {
1818 $a_date = $a->getOpenDate() ;
1819 $b_date = $b->getOpenDate() ;
1820 return ($a_date < $b_date) ? -1 : 1;
1823 $a_date = $a->getCloseDate() ;
1824 $b_date = $b->getCloseDate() ;
1825 return ($a_date < $b_date) ? -1 : 1;
1827 case 'last_modified_date':
1828 $a_date = $a->getLastModifiedDate() ;
1829 $b_date = $b->getLastModifiedDate() ;
1830 return ($a_date < $b_date) ? -1 : 1;
1833 $a_priority = $a->getPriority() ;
1834 $b_priority = $b->getPriority() ;
1835 return ($a_priority < $b_priority) ? -1 : 1;
1838 $aa=$a->getExtraFieldDataText();
1839 $ba=$b->getExtraFieldDataText();
1840 if(!isset($this->criterion) || empty($this->criterion)) {
1844 $criterion = $this->criterion;
1846 $af=$aa[$criterion]['value'];
1847 $bf=$ba[$criterion]['value'];
1848 $namecmp = strcoll ($af,$bf) ;
1849 if ($namecmp != 0) {
1855 // When in doubt, sort on artifact ID
1856 $aid = $a->getID() ;
1857 $bid = $b->getID() ;
1861 return ($aid < $bid) ? -1 : 1;
1865 function sortArtifactList (&$list, $criterion='name', $order='ASC') {
1866 $cmp = new ArtifactComparator () ;
1867 $cmp->criterion = $criterion ;
1868 $cmp->order = $order ;
1870 return usort ($list, array ($cmp, 'Compare')) ;
1875 // c-file-style: "bsd"