5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2002-2004, GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright (C) 2009-2013 Alain Peyrat, Alcatel-Lucent
9 * Copyright 2012, Thorsten “mirabilos” Glaser <t.glaser@tarent.de>
10 * Copyright 2014-2015, Franck Villaume - TrivialDev
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 * Standard Alcatel-Lucent disclaimer for contributing to open source
31 * "The Artifact ("Contribution") has not been tested and/or
32 * validated for release as or in products, combinations with products or
33 * other commercial use. Any use of the Contribution is entirely made at
34 * the user's own responsibility and the user can not rely on any features,
35 * functionalities or performances Alcatel-Lucent has attributed to the
38 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
39 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
40 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
41 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
42 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
43 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
44 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
46 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
49 require_once $gfcommon.'include/Error.class.php';
50 require_once $gfcommon.'tracker/ArtifactMessage.class.php';
51 require_once $gfcommon.'tracker/ArtifactExtraField.class.php';
52 require_once $gfcommon.'tracker/ArtifactWorkflow.class.php';
53 require_once $gfcommon.'tracker/ArtifactStorage.class.php';
54 require_once $gfcommon.'include/MonitorElement.class.php';
56 // This string is used when sending the notification mail for identifying the
58 define('ARTIFACT_MAIL_MARKER', '#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+');
61 * Factory method which creates an Artifact from an artifact ID
63 * @param int $artifact_id The artifact ID
64 * @param array|bool $data The result array, if it's passed in
65 * @return Artifact Artifact object
67 function &artifact_get_object($artifact_id,$data=false) {
69 if (!isset($ARTIFACT_OBJ["_".$artifact_id."_"])) {
71 //the db result handle was passed in
73 $res = db_query_params ('SELECT * FROM artifact_vw WHERE artifact_id=$1',
74 array ($artifact_id)) ;
75 if (db_numrows($res) <1 ) {
76 $ARTIFACT_OBJ["_".$artifact_id."_"]=false;
79 $data = db_fetch_array($res);
81 $ArtifactType =& artifactType_get_object($data["group_artifact_id"]);
82 $ARTIFACT_OBJ["_".$artifact_id."_"]= new Artifact($ArtifactType,$data);
84 return $ARTIFACT_OBJ["_".$artifact_id."_"];
87 class Artifact extends Error {
92 * @var int $status_res.
97 * Artifact Type object.
99 * @var object $ArtifactType.
104 * Array of artifact data.
106 * @var array $data_array.
111 * Array of artifact data for extra fields defined by Admin.
113 * @var array $extra_field_data.
115 var $extra_field_data;
118 * Array of ArtifactFile objects.
125 * Database result set of related tasks
127 * @var result $relatedtasks
132 * cached return value of getVotes
133 * @var int|bool $votes
138 * Artifact - constructor.
140 * @param ArtifactType $ArtifactType The ArtifactType object.
141 * @param int|bool $data (primary key from database OR complete assoc array)
142 * ONLY OPTIONAL WHEN YOU PLAN TO IMMEDIATELY CALL ->create()
144 function __construct(&$ArtifactType, $data=false) {
147 $this->ArtifactType =& $ArtifactType;
149 // Was ArtifactType legit?
150 if (!$ArtifactType || !is_object($ArtifactType)) {
151 $this->setError(_('Invalid Artifact Type'));
155 // Did ArtifactType have an error?
156 if ($ArtifactType->isError()) {
157 $this->setError($ArtifactType->getErrorMessage());
161 // Make sure this person has permission to view artifacts
162 if (!forge_check_perm ('tracker', $this->ArtifactType->getID(), 'read')) {
163 $this->setError(_('Only project members can view private artifact types'));
168 if (is_array($data)) {
169 $this->data_array =& $data;
171 $this->fetchData($data);
177 * create - construct a new Artifact in the database.
179 * @param string $summary The artifact summary.
180 * @param string $details Details of the artifact.
181 * @param int $assigned_to The ID of the user to which this artifact is to be assigned.
182 * @param int $priority The artifacts priority.
183 * @param array $extra_fields Array of extra fields like: array(15=>'foobar',22=>'1');
184 * @param array $importData Array of data to change submitter and time of submit like:
185 * array('user' => 127, 'time' => 1234556789)
186 * @return bool id on success / false on failure.
188 function create( $summary, $details, $assigned_to=100, $priority=3, $extra_fields=array(), $importData = array()) {
190 // make sure this person has permission to add artifacts
196 if(array_key_exists('user', $importData)){
197 $user = $importData['user'];
199 if (!forge_check_perm ('tracker',$this->ArtifactType->getID(),'submit')) {
200 $this->setError(_('You are not currently allowed to submit items to this tracker.'));
204 if (session_loggedin()) {
215 $this->setError(_('Message Summary Is Required'));
219 $this->setError(_('Message Body Is Required'));
228 $status_id=1; // on creation, status is set to "open"
231 // They may be using an extra field "status" box so we have to remap
232 // the status_id based on the extra field - this keeps the counters
235 $status_id = $this->ArtifactType->remapStatus($status_id,$extra_fields);
237 $this->setError($this->ArtifactType->getErrorMessage());
242 if (array_key_exists('time',$importData)){
243 $time = $importData['time'];
247 $res = db_query_params ('INSERT INTO artifact
248 (group_artifact_id,status_id,priority,
249 submitted_by,assigned_to,open_date,summary,details)
250 VALUES ($1,$2,$3,$4,$5,$6,$7,$8)',
251 array ($this->ArtifactType->getID(),
257 htmlspecialchars($summary),
258 htmlspecialchars($details))) ;
260 $this->setError(db_error());
265 $artifact_id=db_insertid($res,'artifact','artifact_id');
267 if (!$res || !$artifact_id) {
268 $this->setError(db_error());
273 // Now set up our internal data structures
275 if (!$this->fetchData($artifact_id)) {
279 // the changes to the extra fields will be logged in this array.
280 // (we won't use it however)
281 $extra_field_changes = array();
282 if (!$this->updateExtraFields($extra_fields,$extra_field_changes)) {
288 // now send an email if appropriate
290 $this->mailFollowupEx(0, 1);
298 * fetchData - re-fetch the data for this Artifact from the database.
300 * @param int $artifact_id The artifact ID.
301 * @return boolean success.
303 function fetchData($artifact_id) {
304 $this->votes = false;
305 $res = db_query_params ('SELECT * FROM artifact_vw WHERE artifact_id=$1 AND group_artifact_id=$2',
307 $this->ArtifactType->getID())) ;
308 if (!$res || db_numrows($res) < 1) {
309 $this->setError(_('Invalid Artifact ID'));
312 $this->data_array = db_fetch_array($res);
313 db_free_result($res);
318 * getArtifactType - get the ArtifactType Object this Artifact is associated with.
320 * @return object ArtifactType.
322 function &getArtifactType() {
323 return $this->ArtifactType;
327 * getID - get this ArtifactID.
329 * @return int The artifact_id #.
332 return $this->data_array['artifact_id'];
336 * getStringID - get a string display for this ArtifactID.
338 * @return string The artifact_id #.
340 function getStringID() {
341 return '[#'.$this->data_array['artifact_id'].']';
345 * getStatusID - get open/closed/deleted flag.
347 * @return int Status: (1) Open, (2) Closed, (3) Deleted.
349 function getStatusID() {
350 return $this->data_array['status_id'];
354 * getStatusName - get open/closed/deleted text.
356 * @return string The status name.
358 function getStatusName() {
359 return $this->data_array['status_name'];
363 * getCustomStatusName - get custom status value text.
365 * @return string The custom status name.
367 function getCustomStatusName() {
368 $custom_status_id = $this->ArtifactType->getCustomStatusField();
369 if ($custom_status_id) {
370 $result = db_query_params ('SELECT element_name FROM artifact_extra_field_elements aefe, artifact_extra_field_data aefd
371 WHERE artifact_id=$1 AND aefd.extra_field_id=$2 AND CAST(aefd.field_data AS INTEGER)=aefe.element_id',
372 array ($this->getID(), $custom_status_id)) ;
374 return db_result($result, 0, 'element_name');
377 return $this->data_array['status_name'];
381 * getPriority - get priority flag.
383 * @return int priority.
385 function getPriority() {
386 return $this->data_array['priority'];
390 * getSubmittedBy - get ID of submitter.
392 * @return int user_id of submitter.
394 function getSubmittedBy() {
395 return $this->data_array['submitted_by'];
399 * getSubmittedEmail - get email of submitter.
401 * @return string The email of submitter.
403 function getSubmittedEmail() {
404 return $this->data_array['submitted_email'];
408 * getSubmittedRealName - get real name of submitter.
410 * @return string The real name of submitter.
412 function getSubmittedRealName() {
413 return $this->data_array['submitted_realname'];
417 * getSubmittedUnixName - get login name of submitter.
419 * @return string The unix name of submitter.
421 function getSubmittedUnixName() {
422 return $this->data_array['submitted_unixname'];
426 * getAssignedTo - get ID of assignee.
428 * @return int user_id of assignee.
430 function getAssignedTo() {
431 return $this->data_array['assigned_to'];
435 * getAssignedEmail - get email of assignee.
437 * @return string The email of assignee.
439 function getAssignedEmail() {
440 return $this->data_array['assigned_email'];
444 * getAssignedRealName - get real name of assignee.
446 * @return string The real name of assignee.
448 function getAssignedRealName() {
449 return $this->data_array['assigned_realname'];
453 * getAssignedUnixName - get login name of assignee.
455 * @return string The unix name of assignee.
457 function getAssignedUnixName() {
458 return $this->data_array['assigned_unixname'];
462 * getOpenDate - get unix time of creation.
464 * @return int unix time.
466 function getOpenDate() {
467 return $this->data_array['open_date'];
471 * getCloseDate - get unix time of closure.
473 * @return int unix time.
475 function getCloseDate() {
476 return $this->data_array['close_date'];
480 * getLastModifiedDate - the last_modified_date of this task.
482 * @return int the last_modified_date.
484 function getLastModifiedDate() {
485 return $this->data_array['last_modified_date'];
489 * getSummary - get text summary of artifact.
491 * @return string The summary (subject).
493 function getSummary() {
494 return $this->data_array['summary'];
498 * getDetails - get text body (message) of artifact.
500 * @return string The body (message).
502 function getDetails() {
503 return $this->data_array['details'];
507 * delete - delete this tracker and all its related data.
509 * @param bool $sure I'm Sure.
510 * @return bool true/false;
512 function delete($sure) {
514 $this->setMissingParamsError(_('Please tick all checkboxes.'));
517 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
518 $this->setPermissionDeniedError();
522 $res = db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1',
523 array ($this->getID())) ;
525 $this->setError(_('Error deleting extra field data: ').db_error());
530 ArtifactStorage::instance()->deleteFromQuery('SELECT id FROM artifact_file WHERE artifact_id=$1',
531 array ($this->getID())) ;
533 $res = db_query_params ('DELETE FROM artifact_file WHERE artifact_id=$1',
534 array ($this->getID())) ;
536 $this->setError(_('Error deleting file from db: ').db_error());
538 ArtifactStorage::instance()->rollback();
541 $res = db_query_params ('DELETE FROM artifact_message WHERE artifact_id=$1',
542 array ($this->getID())) ;
544 $this->setError(_('Error deleting message: ').db_error());
546 ArtifactStorage::instance()->rollback();
549 $res = db_query_params ('DELETE FROM artifact_history WHERE artifact_id=$1',
550 array ($this->getID())) ;
552 $this->setError(_('Error deleting history: ').db_error());
554 ArtifactStorage::instance()->rollback();
557 $MonitorElementObject = new MonitorElement('artifact');
558 if (!$MonitorElementObject->clearMonitor($this->getID())) {
559 $this->setError(_('Error deleting monitor: ').db_error());
561 ArtifactStorage::instance()->rollback();
564 $res = db_query_params ('DELETE FROM artifact WHERE artifact_id=$1',
565 array ($this->getID())) ;
567 $this->setError(_('Error deleting artifact: ').db_error());
569 ArtifactStorage::instance()->rollback();
573 if ($this->getStatusID() == 1) {
574 $res = db_query_params ('UPDATE artifact_counts_agg SET count=count-1,open_count=open_count-1
575 WHERE group_artifact_id=$1',
576 array ($this->getID())) ;
578 $this->setError(_('Error updating artifact counts: ').db_error());
580 ArtifactStorage::instance()->rollback();
583 } elseif ($this->getStatusID() == 2) {
584 $res = db_query_params ('UPDATE artifact_counts_agg SET count=count-1
585 WHERE group_artifact_id=$1',
586 array ($this->getID())) ;
588 $this->setError(_('Error updating artifact counts: ').db_error());
590 ArtifactStorage::instance()->rollback();
596 ArtifactStorage::instance()->commit();
601 * setMonitor - user can monitor this artifact.
603 * @return bool Always false - always use the getErrorMessage() for feedback
605 function setMonitor() {
607 if (session_loggedin()) {
608 $user_id = user_getid();
610 $this->setError(_('You can only monitor if you are logged in.'));
614 $MonitorElementObject = new MonitorElement('artifact');
615 if (!$this->isMonitoring()) {
616 if (!$MonitorElementObject->enableMonitoringByUserId($this->getID(), $user_id)) {
617 $this->setError($MonitorElementObject->getErrorMessage());
620 $feedback = _('Monitoring Started');
623 if (!$MonitorElementObject->disableMonitoringByUserId($this->getID(), $user_id)) {
624 $this->setError($MonitorElementObject->getErrorMessage());
627 $feedback = _('Monitoring Stopped');
633 function isMonitoring() {
634 if (!session_loggedin()) {
637 $MonitorElementObject = new MonitorElement('artifact');
638 return $MonitorElementObject->isMonitoredByUserId($this->getID(), user_getid());
642 * getMonitorIds - array of email addresses monitoring this Artifact.
644 * @return array of email addresses monitoring this Artifact.
646 function getMonitorIds() {
647 $MonitorElementObject = new MonitorElement('artifact');
648 return $MonitorElementObject->getMonitorUsersIdsInArray($this->getID());
652 * getHistory - returns a result set of audit trail for this support request.
654 * @return resource result set.
656 function getHistory() {
657 return db_query_params ('SELECT * FROM artifact_history_user_vw WHERE artifact_id=$1 ORDER BY entrydate DESC, id ASC',
658 array ($this->getID())) ;
662 * getMessages - get the list of messages attached to this artifact.
664 * @param string $order
665 * @return resource result set.
667 function getMessages($ascending='up') {
669 * This is necessary because someone committed a change
670 * to this method in FusionForge trunk that accepts 'up'
671 * as default (luckily, it’s the same!) and 'down' as
672 * alternative probability, whereas FusionForge 5.2 has
673 * false as default and true for ascending order, so we
674 * need to check this out and use === to be sure ☹
676 if ($ascending === 'up') {
678 } elseif ($ascending === true) {
680 } elseif ($ascending === false) {
685 return db_query_params('SELECT * FROM artifact_message_user_vw WHERE artifact_id=$1 ORDER BY adddate ' . $order . ', id ASC',
686 array($this->getID()));
690 * getMessage - get a message attached to this artifact.
692 * @param int $msg_id id of the message.
694 * @return resource database result set.
696 function getMessage($msg_id) {
700 return db_query_params ('SELECT * FROM artifact_message_user_vw WHERE id=$1',
705 * getMessageObjects - get an array of message objects.
707 * @return array Of ArtifactMessage objects.
709 function &getMessageObjects() {
710 $res=$this->getMessages();
712 while ($arr = db_fetch_array($res)) {
713 $return[] = new ArtifactMessage($this, $arr);
719 * getFiles - get array of ArtifactFile's.
721 * @return array of ArtifactFile's.
723 function &getFiles() {
724 if (!isset($this->files)) {
725 $res = db_query_params ('SELECT id,artifact_id,description,filename,filesize,' .
726 'filetype,adddate,submitted_by,user_name,realname
727 FROM artifact_file_user_vw WHERE artifact_id=$1',
728 array ($this->getID())) ;
729 $rows=db_numrows($res);
731 for ($i=0; $i < $rows; $i++) {
732 $this->files[$i]=new ArtifactFile($this,db_fetch_array($res));
735 $this->files=array();
742 * getRelatedTasks - get array of related tasks
744 * @return resource Database result set
746 function getRelatedTasks() {
747 if (!$this->relatedtasks) {
748 $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
749 FROM project_task pt, project_group_list pgl, project_status ps
750 WHERE pt.group_project_id = pgl.group_project_id
751 AND ps.status_id = pt.status_id
752 AND EXISTS (SELECT project_task_id FROM project_task_artifact
753 WHERE project_task_id=pt.project_task_id
754 AND artifact_id = $1)',
755 array ($this->getID())) ;
757 return $this->relatedtasks;
761 * addMessage - attach a text message to this Artifact.
763 * @param string $body The $string message being attached.
764 * @param bool $by Email $string address of message creator.
765 * @param bool $send_followup Whether $bool to email out a followup.
766 * @return bool success.
768 function addMessage($body,$by=false,$send_followup=false) {
770 $this->setMissingParamsError();
773 if (!forge_check_perm ('tracker',$this->ArtifactType->getID(),'submit')) {
774 $this->setError(_('You are not currently allowed to submit items to this tracker.'));
777 if (session_loggedin()) {
778 $user_id=user_getid();
779 $user = user_get_object($user_id);
780 if (!$user || !is_object($user)) {
781 $this->setError('Error: Logged In User But Could Not Get User Object');
784 // we'll store this email even though it will likely never be used -
785 // since we have their correct user_id, we can join the USERS table to get email
786 $by=$user->getEmail();
789 if (!$by || !validate_email($by)) {
790 $this->setMissingParamsError();
796 $res = db_query_params ('INSERT INTO artifact_message (artifact_id,submitted_by,from_email,adddate,body) VALUES ($1,$2,$3,$4,$5)',
797 array ($this->getID(),
801 htmlspecialchars($body))) ;
803 $this->updateLastModifiedDate();
805 if ($send_followup) {
806 $this->mailFollowupEx($now, 2, false);
812 * addHistory - add an entry to audit trail.
814 * @param string $field_name The name of the field in the database being modified.
815 * @param string $old_value The former value of this field.
816 * @param array $importData Array of data to change submitter and time of submit like:
817 * array('user' => 127, 'time' => 1234556789)
819 * @return boolean success.
821 function addHistory($field_name,$old_value, $importData = array()) {
822 if (array_key_exists('user', $importData)){
823 $user = $importData['user'];
825 if (!session_loggedin()) {
831 if (array_key_exists('time',$importData)){
832 $time = $importData['time'];
836 return db_query_params ('INSERT INTO artifact_history(artifact_id,field_name,old_value,mod_by,entrydate) VALUES ($1,$2,$3,$4,$5)',
837 array ($this->getID(),
845 * setStatus - set the status of this artifact.
847 * @param int $status_id The artifact status ID.
848 * @param int|bool $closingTime Closing date if status = 1
850 * @return bool success.
852 function setStatus($status_id, $closingTime=false) {
854 $qpa = db_construct_qpa(array(), 'UPDATE artifact SET status_id=$1', array ($status_id)) ;
855 if ($closingTime && $status_id != 1) {
857 $qpa = db_construct_qpa($qpa, ', close_date=$1 ', array ($time)) ;
859 $qpa = db_construct_qpa($qpa,
860 'WHERE artifact_id=$1 AND group_artifact_id=$2',
861 array ($this->getID(), $this->ArtifactType->getID())) ;
862 $result=db_query_qpa($qpa);
864 if (!$result || db_affected_rows($result) < 1) {
865 $this->setError('Error - update failed!'.db_error());
869 if (!$this->fetchData($this->getID())) {
881 * update - update the fields in this artifact.
883 * @param int $priority The artifact priority.
884 * @param int $status_id The artifact status ID.
885 * @param int $assigned_to The person to which this artifact is to be assigned.
886 * @param string $summary The artifact summary.
887 * @param int $canned_response The canned response.
888 * @param string $details Attaching another comment.
889 * @param int $new_artifact_type_id Allows you to move an artifact to another type.
890 * @param array $extra_fields Array of extra fields like: array(15=>'foobar',22=>'1');
891 * @param string $description The description.
892 * @return boolean success.
894 function update($priority,$status_id,
895 $assigned_to,$summary,$canned_response,$details,$new_artifact_type_id,
896 $extra_fields=array(), $description='') {
899 Field-level permission checking
901 if (!forge_check_perm ('tracker', $this->ArtifactType->getID(), 'manager')) {
902 // Non-managers cannot modify these fields
903 $priority=$this->getPriority();
904 $summary=htmlspecialchars_decode($this->getSummary());
905 $description=htmlspecialchars_decode($this->getDetails());
906 $canned_response=100;
907 $new_artifact_type_id=$this->ArtifactType->getID();
908 $assigned_to=$this->getAssignedTo();
910 if (!forge_check_perm ('tracker', $this->ArtifactType->getID(), 'tech')) {
911 $this->setPermissionDeniedError();
916 // They may be using an extra field "status" box so we have to remap
917 // the status_id based on the extra field - this keeps the counters
920 if (count($extra_fields) > 0) {
921 $status_id=$this->ArtifactType->remapStatus($status_id,$extra_fields);
923 if (!$this->getID()) {
924 $this->setMissingParamsError('ID');
928 $this->setMissingParamsError(_('Assigned to'));
932 $this->setMissingParamsError(_('State'));
935 if (!$canned_response) {
936 $this->setMissingParamsError(_('Canned Response'));
939 if (!$new_artifact_type_id) {
940 $this->setMissingParamsError(_('Data Type'));
944 // Check that assigned_to is a tech for the tracker
945 if ($assigned_to != 100) {
946 if (!forge_check_perm_for_user ($assigned_to, 'tracker', $this->ArtifactType->getID(), 'tech')) {
947 $this->setError(_("Invalid assigned person: must be a technician"));
952 // Array to record which properties were changed
959 // Get a lock on this row in the database
961 db_query_params ('SELECT * FROM artifact WHERE artifact_id=$1 FOR UPDATE', array ($this->getID())) ;
962 $artifact_type_id = $this->ArtifactType->getID();
964 // Attempt to move this Artifact to a new ArtifactType
965 // need to instantiate new ArtifactType obj and test perms
967 if ($new_artifact_type_id != $artifact_type_id) {
968 $newArtifactType= new ArtifactType($this->ArtifactType->getGroup(), $new_artifact_type_id);
969 if (!is_object($newArtifactType) || $newArtifactType->isError()) {
970 $this->setError(_('Could not move to new Artifact Type'). $newArtifactType->getErrorMessage());
974 // do they have perms for new ArtifactType?
975 if (!forge_check_perm ('tracker', $newArtifactType->getID(), 'manager')) {
976 $this->setPermissionDeniedError();
981 // Add a message to explain that the tracker was moved.
982 $message = sprintf(_('Moved from %1$s to %2$s'),
983 $this->ArtifactType->getName(),
984 $newArtifactType->getName());
985 $this->addHistory('type', $this->ArtifactType->getName());
986 $this->addMessage($message,'',0);
988 // Fake change to send a mail when moved.
989 $changes['Type'] = 1;
991 // Try to remap extra_fields values when possible.
992 // If there is an extra_field with the same alias
993 // and if the value exist in the new one, then recode
994 // the value to keep it.
995 $new_extra_fields = array();
996 $ef = $this->ArtifactType->getExtraFields();
997 $ef_new = $newArtifactType->getExtraFields();
998 foreach($extra_fields as $extra_id => $value) {
999 $alias = preg_replace('/^@/', '', $ef[$extra_id]['alias']);
1000 $type = $ef[$extra_id]['field_type'];
1002 // Search if there is an extra field with the same alias.
1004 foreach($ef_new as $id => $arr) {
1005 if (preg_replace('/^@/', '', $arr['alias']) == $alias) {
1010 // If we found one, copy for simple fields or
1011 // search if there is the same value.
1013 if ($type == ARTIFACT_EXTRAFIELDTYPE_TEXT ||
1014 $type == ARTIFACT_EXTRAFIELDTYPE_INTEGER ||
1015 $type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA ||
1016 $type == ARTIFACT_EXTRAFIELDTYPE_RELATION) {
1017 $new_extra_fields[$new_id] = $value;
1019 $values = $newArtifactType->getExtraFieldElements($new_id);
1020 if (is_array($value)) {
1021 foreach($value as $v) {
1022 $v = $this->ArtifactType->getElementName($v);
1023 foreach($values as $ev) {
1024 if ($ev['element_name'] == $v) {
1025 $new_extra_fields[$new_id][] = $ev['element_id'];
1030 $value = $this->ArtifactType->getElementName($value);
1031 foreach($values as $ev) {
1032 if ($ev['element_name'] == $value) {
1033 $new_extra_fields[$new_id] = $ev['element_id'];
1041 // Special case if moving to a tracker with custom status (previous has not).
1042 $custom_status_id = $newArtifactType->getCustomStatusField();
1043 if ($custom_status_id && !$new_extra_fields[$custom_status_id]) {
1044 $atw = new ArtifactWorkflow($newArtifactType, $custom_status_id);
1045 $nodes = $atw->getNextNodes(100);
1047 $new_extra_fields[$custom_status_id] = $nodes[0];
1051 $extra_fields = $new_extra_fields;
1053 $res = db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1',
1054 array ($this->getID()));
1056 $this->setError(_('Removal of old artifact_extra_field_data failed: ').db_error());
1061 // Check that assigned_to is a tech in the new tracker
1062 if ($assigned_to != 100) {
1063 if (!forge_check_perm ('tracker', $newArtifactType->getID(), 'tech')) {
1068 //can't send a canned response when changing ArtifactType
1069 $canned_response=100;
1070 $this->ArtifactType =& $newArtifactType;
1074 $qpa = db_construct_qpa();
1075 $qpa = db_construct_qpa($qpa, 'UPDATE artifact SET');
1078 // handle audit trail
1081 if ($this->getStatusID() != $status_id) {
1082 $this->addHistory('status_id',$this->getStatusID());
1083 $qpa = db_construct_qpa($qpa, ' status_id=$1,', array($status_id));
1084 $changes['status'] = 1;
1087 if ($status_id != 1) {
1088 $qpa = db_construct_qpa($qpa, ' close_date=$1,', array($now));
1090 $qpa = db_construct_qpa($qpa, ' close_date=$1,', array(0));
1092 $this->addHistory('close_date', $this->getCloseDate());
1094 if ($this->getPriority() != $priority) {
1095 $this->addHistory('priority',$this->getPriority());
1096 $qpa = db_construct_qpa($qpa, ' priority=$1,', array($priority));
1097 $changes['priority'] = 1;
1101 if ($this->getAssignedTo() != $assigned_to) {
1102 $this->addHistory('assigned_to',$this->getAssignedTo());
1103 $qpa = db_construct_qpa($qpa, ' assigned_to=$1,', array($assigned_to));
1104 $changes['assigned_to'] = 1;
1107 if ($summary && ($this->getSummary() != htmlspecialchars($summary))) {
1108 $this->addHistory('summary', $this->getSummary());
1109 $qpa = db_construct_qpa($qpa, ' summary=$1,', array(htmlspecialchars($summary)));
1110 $changes['summary'] = 1;
1113 if ($description && ($this->getDetails() != htmlspecialchars($description))) {
1114 $this->addHistory('details', $this->getDetails());
1115 $qpa = db_construct_qpa($qpa, ' details=$1,', array(htmlspecialchars($description)));
1116 $changes['details'] = 1;
1120 $this->addMessage($details,'',0);
1121 $changes['details'] = 1;
1126 Finally, update the artifact itself
1129 $qpa = db_construct_qpa($qpa, ' group_artifact_id=$1
1130 WHERE artifact_id=$2 AND group_artifact_id=$3',
1131 array($new_artifact_type_id,
1132 $this->getID(), $artifact_type_id));
1133 $result = db_query_qpa($qpa);
1135 if (!$result || db_affected_rows($result) < 1) {
1136 $this->setError(_('Update failed').db_error());
1140 if (!$this->fetchData($this->getID())) {
1147 //extra field handling
1149 if (!$this->updateExtraFields($extra_fields,$changes)) {
1150 //TODO - see if anything actually did change
1156 handle canned responses
1158 Instantiate ArtifactCanned and get the body of the message
1160 if ($canned_response != 100) {
1161 //don't care if this response is for this group - could be hacked
1162 $acr=new ArtifactCanned($this->ArtifactType,$canned_response);
1163 if (!$acr || !is_object($acr)) {
1164 $this->setError(_('Could Not Create Canned Response Object'));
1165 } elseif ($acr->isError()) {
1166 $this->setError($acr->getErrorMessage());
1168 $body = $acr->getBody();
1170 if (!$this->addMessage(util_unconvert_htmlspecialchars($body),'',0)) {
1177 $this->setError(_('Unable to Use Canned Response'));
1183 if ($update || $send_message){
1184 if (!empty($changes)) {
1185 // Send the email with changes
1186 $this->mailFollowupEx($now, 2, false, $changes);
1191 //nothing changed, so cancel the transaction
1192 $this->setError(_('Nothing Changed - Update Cancelled'));
1200 * updateLastModifiedDate - update the last_modified_date attribute of this artifact.
1202 * @return bool true on success / false on failure
1204 function updateLastModifiedDate() {
1205 $res = db_query_params ('UPDATE artifact SET last_modified_date=EXTRACT(EPOCH FROM now())::integer WHERE artifact_id=$1',
1206 array ($this->getID()));
1211 * assignToMe - assigns this artifact to current user
1213 * @return bool true on success / false on failure
1215 function assignToMe() {
1216 if (!session_loggedin() || !(forge_check_perm('tracker', $this->ArtifactType->getID(), 'manager') || forge_check_perm('tracker', $this->ArtifactType->getID(), 'tech'))) {
1217 $this->setPermissionDeniedError();
1221 $user_id = user_getid();
1222 $res = db_query_params ('UPDATE artifact SET assigned_to=$1 WHERE artifact_id=$2',
1223 array ($user_id, $this->getID())) ;
1225 $this->setError(_('Error updating assigned_to in artifact: ').db_error());
1228 $this->fetchData($this->getID());
1234 * updateExtraFields - updates the extra data elements for this artifact
1235 * e.g. the extra fields created and defined by the admin.
1237 * @param array Array of extra fields like: array(15=>'foobar',22=>'1');
1238 * @param array Array where changes to the extra fields should be logged
1239 * @return bool true on success / false on failure
1241 function updateExtraFields($extra_fields,&$changes){
1243 This is extremely complex code - we have take the passed array
1244 and see if we need to insert it into the db, and may have to
1245 add history rows for the audit trail
1247 start by getting all the available extra fields from ArtifactType
1248 For each field from ArtifacType, check the passed array -
1249 This prevents someone from passing bogus extra field entries - they will be ignored
1250 if the passed entry is blank, may have to force a default value
1251 if the passed array is different from the existing data in db,
1252 delete old entry and insert new entries, along with possible audit trail
1254 skip it and continue to next item
1259 //get a list of extra fields for this artifact_type
1260 $ef = $this->ArtifactType->getExtraFields();
1261 $efk=array_keys($ef);
1263 if (empty($extra_fields) && empty($ef)) {
1267 // If there is a status field, then check against the workflow.
1268 // Unless if we change type.
1269 if (! isset($changes['Type']) || !$changes['Type']) {
1270 for ($i=0; $i<count($efk); $i++) {
1272 $type=$ef[$efid]['field_type'];
1273 if ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
1274 // Get previous value.
1275 $res = db_query_params ('SELECT field_data FROM artifact_extra_field_data
1276 WHERE artifact_id=$1 AND extra_field_id=$2',
1277 array($this->getID(),
1279 $old = (db_numrows($res)>0) ? db_result($res,0,'field_data') : 100;
1280 if ($old != $extra_fields[$efid]) {
1281 $atw = new ArtifactWorkflow($this->ArtifactType, $efid);
1282 if (!$atw->checkEvent($old, $extra_fields[$efid])) {
1283 $this->setError('Workflow error: You are not authorized to change the Status ('.$old.' => '.$extra_fields[$efid].')');
1291 //now we'll update this artifact for each extra field
1292 for ($i=0; $i<count($efk); $i++) {
1294 $type=$ef[$efid]['field_type'];
1296 // check required fields
1297 if ($ef[$efid]['is_required']) {
1298 if (!array_key_exists($efid, $extra_fields)) {
1299 if ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
1300 $this->setError(_('Status Custom Field Must Be Set'));
1303 $this->setMissingParamsError($ef[$efid]['field_name']);
1308 if ($extra_fields[$efid] === '') {
1309 if ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
1310 $this->setError(_('Status Custom Field Must Be Set'));
1313 $this->setMissingParamsError($ef[$efid]['field_name']);
1318 if (($type == ARTIFACT_EXTRAFIELDTYPE_SELECT || $type == ARTIFACT_EXTRAFIELDTYPE_RADIO) &&
1319 $extra_fields[$efid] == '100') {
1320 $this->setMissingParamsError($ef[$efid]['field_name']);
1323 elseif (($type == ARTIFACT_EXTRAFIELDTYPE_MULTISELECT || $type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) &&
1324 (count($extra_fields[$efid]) == 1 && $extra_fields[$efid][0] == '100')) {
1325 $this->setMissingParamsError($ef[$efid]['field_name']);
1332 // Force each field to have some value if it is a numeric field
1333 // text fields will just be purged and skipped
1335 if (!array_key_exists($efid, $extra_fields) || $extra_fields[$efid] === '') {
1336 if (($type == ARTIFACT_EXTRAFIELDTYPE_SELECT) || ($type == ARTIFACT_EXTRAFIELDTYPE_RADIO)) {
1337 $extra_fields[$efid]='100';
1338 } elseif (($type == ARTIFACT_EXTRAFIELDTYPE_MULTISELECT) || ($type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX)) {
1339 $extra_fields[$efid]=array('100');
1341 db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1342 array ($this->getID(),
1349 // get the old rows of data
1351 $resd = db_query_params ('SELECT * FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1352 array ($this->getID(),
1354 $rows=db_numrows($resd);
1355 if ($resd && $rows) {
1357 //POTENTIAL PROBLEM - no entry was there before, but adding one now - may need history
1360 // Compare for history purposes
1363 // these types have arrays associated to them, so they need
1364 // special handling to check for differences
1365 if ($type == ARTIFACT_EXTRAFIELDTYPE_MULTISELECT || $type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) {
1366 // check the differences between the old values and the new values
1367 $old_values = util_result_column_to_array($resd,"field_data");
1369 $added_values = array_diff($extra_fields[$efid], $old_values);
1370 $deleted_values = array_diff($old_values, $extra_fields[$efid]);
1372 if (!empty($added_values) || !empty($deleted_values)) { // there are differences...
1373 $field_name = $ef[$efid]['field_name'];
1374 if (!preg_match('/^@/', $ef[$efid]['alias'])) {
1375 $changes["extra_fields"][$efid] = 1;
1378 $this->addHistory($field_name, $this->ArtifactType->getElementName(array_reverse($old_values)));
1381 db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1382 array ($this->getID(),
1387 } elseif (db_result($resd,0,'field_data') == htmlspecialchars($extra_fields[$efid])) {
1388 //element did not change
1391 //element DID change - do a history entry
1392 $field_name = $ef[$efid]['field_name'];
1393 if (!preg_match('/^@/', $ef[$efid]['alias'])) {
1394 $changes["extra_fields"][$efid] = 1;
1396 db_query_params ('DELETE FROM artifact_extra_field_data WHERE artifact_id=$1 AND extra_field_id=$2',
1397 array ($this->getID(),
1400 // Adding history with previous value.
1401 if (($type == ARTIFACT_EXTRAFIELDTYPE_SELECT) || ($type == ARTIFACT_EXTRAFIELDTYPE_RADIO) || ($type == ARTIFACT_EXTRAFIELDTYPE_STATUS)) {
1402 $this->addHistory($field_name,$this->ArtifactType->getElementName(db_result($resd,0,'field_data')));
1404 $this->addHistory($field_name, db_result($resd,0,'field_data'));
1410 //no history for this extra field exists
1415 // Some rewrite & consistency checks on the relation type field.
1417 // 1) Convert syntax [#NNN] to NNN
1418 // 2) Allow multiple spaces as separator.
1419 // 3) Ensure that only integers are given.
1420 // 4) Ensure that id corresponds to valid tracker id.
1422 if ($type == ARTIFACT_EXTRAFIELDTYPE_RELATION) {
1423 $value = preg_replace('/\[\#(\d+)\]/', "\\1", trim($extra_fields[$efid]));
1424 $value = preg_replace('/\\s+/', ' ', $value);
1426 foreach (explode(' ',$value) as $id) {
1427 if (preg_match('/^(\d+)$/', $id)) {
1428 // Control that the id is present in the db
1430 $res = db_query_params ('SELECT artifact_id FROM artifact WHERE artifact_id=$1',
1432 if (db_numrows($res) == 1) {
1435 $this->setError('Illegal id '.$id.', it\'s not a valid tracker id for field: '.$ef[$efid]['field_name'].'.'); // @todo: lang
1439 $this->setError('Illegal value '.$id.', only trackers id are allowed for field: '.$ef[$efid]['field_name'].'.'); // @todo: lang
1443 $extra_fields[$efid] = trim($new);
1446 // Ensure that only integer are allowed for type ARTIFACT_EXTRAFIELDTYPE_INTEGER
1447 if ($type == ARTIFACT_EXTRAFIELDTYPE_INTEGER) {
1448 $extra_fields[$efid] = trim($extra_fields[$efid]);
1449 if (!preg_match('/^[-+]?(\d+)$/', $extra_fields[$efid])) {
1450 $this->setError('Illegal value '.$extra_fields[$efid].' for field '.$ef[$efid]['field_name'].': Only integer is allowed.');
1453 if ($extra_fields[$efid] < -2147483648 || $extra_fields[$efid] > 2147483647) {
1454 $this->setError('Illegal value '.$extra_fields[$efid].' for field '.$ef[$efid]['field_name'].': Integer out of range (-2147483648 to +2147483647).');
1457 $extra_fields[$efid] = intval($extra_fields[$efid]);
1461 // See if anything was even passed for this extra_field_id
1463 if ($extra_fields[$efid] === '') {
1464 //nothing in field to update - text fields may be blank
1466 //determine the type of field and whether it should have multiple rows supporting it
1467 $type=$ef[$efid]['field_type'];
1468 if (($type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) || ($type==ARTIFACT_EXTRAFIELDTYPE_MULTISELECT)) {
1470 $count=count($extra_fields[$efid]);
1471 for ($fin=0; $fin<$count; $fin++) {
1472 $res = db_query_params ('INSERT INTO artifact_extra_field_data (artifact_id,extra_field_id,field_data) VALUES ($1,$2,$3)',
1473 array ($this->getID(),
1475 $extra_fields[$efid][$fin])) ;
1477 $this->setError(db_error());
1484 $res = db_query_params ('INSERT INTO artifact_extra_field_data (artifact_id,extra_field_id,field_data) VALUES ($1,$2,$3)',
1485 array ($this->getID(),
1487 htmlspecialchars($extra_fields[$efid]))) ;
1489 $this->setError(db_error());
1496 unset($this->extra_field_data);
1499 $this->updateLastModifiedDate();
1505 * getExtraFieldData - get an array of data for the extra fields associated with this artifact
1507 * @return array array of data
1509 function &getExtraFieldData() {
1510 if (!isset($this->extra_field_data)) {
1511 $this->extra_field_data = array();
1512 $res = db_query_params ('SELECT * FROM artifact_extra_field_data WHERE artifact_id=$1 ORDER BY extra_field_id',
1513 array ($this->getID())) ;
1514 $ef = $this->ArtifactType->getExtraFields();
1515 while ($arr = db_fetch_array($res)) {
1516 $type=$ef[$arr['extra_field_id']]['field_type'];
1517 if (($type == ARTIFACT_EXTRAFIELDTYPE_CHECKBOX) || ($type==ARTIFACT_EXTRAFIELDTYPE_MULTISELECT)) {
1518 //accumulate a sub-array of values in cases where you may have multiple rows
1519 if (!array_key_exists($arr['extra_field_id'], $this->extra_field_data) || !is_array($this->extra_field_data[$arr['extra_field_id']])) {
1520 $this->extra_field_data[$arr['extra_field_id']] = array();
1522 $this->extra_field_data[$arr['extra_field_id']][]=$arr['field_data'];
1524 $this->extra_field_data[$arr['extra_field_id']] = $arr['field_data'];
1528 return $this->extra_field_data;
1532 * marker - adds the > symbol to fields that have been modified for the email message
1536 function marker($prop_name,$changes,$extra_field_id=0) {
1537 if ($prop_name == 'extra_fields' && isset($changes[$prop_name][$extra_field_id])) {
1539 } elseif ($prop_name != 'extra_fields' && isset($changes[$prop_name])) {
1547 * mailFollowupEx - send out an email update for this artifact.
1549 * @param time_t $tm Time of the change
1550 * @param int $type (1) initial/creation (2) update
1551 * @param array $more_addresses Array of additional addresses to mail to
1552 * @param array $changes Array of fields changed in this update
1554 * @return boolean success.
1556 function mailFollowupEx($tm, $type, $more_addresses = false, $changes='') {
1558 $monitor_ids = array();
1564 $sess = session_get_user();
1565 $name = util_unconvert_htmlspecialchars($this->ArtifactType->getName());
1566 $body = $this->ArtifactType->Group->getUnixName() . '-' . $name .' '. $this->getStringID();
1569 $body .= ' was opened at '.date('Y-m-d H:i', $this->getOpenDate());
1570 } elseif ($type == 3) {
1571 $body .= ' was deleted at '.date('Y-m-d H:i', time());
1573 $body .= ' was changed at '.date('Y-m-d H:i', $tm);
1576 $body .= ' by ' . $sess->getRealName();
1579 if ($type == 1 || $type == 2) {
1580 $body .= "\nYou can respond by visiting: ".
1581 "\n".util_make_url ('/tracker/?func=detail&atid='. $this->ArtifactType->getID() .
1582 "&aid=". $this->getID() .
1583 "&group_id=". $this->ArtifactType->Group->getID());
1584 if (false) { // currently not working
1586 "\nOr by replying to this e-mail entering your response between the following markers: ".
1587 "\n".ARTIFACT_MAIL_MARKER.
1588 "\n(enter your response here, only in plain text format)".
1589 "\n".ARTIFACT_MAIL_MARKER;
1594 $body .= "\n".$this->marker('status',$changes).
1595 "Status: ". $this->getStatusName() ."\n".
1596 $this->marker('priority',$changes).
1597 "Priority: ". $this->getPriority() ."\n".
1598 "Submitted By: ". $this->getSubmittedRealName() .
1599 " (". $this->getSubmittedUnixName(). ")"."\n".
1600 $this->marker('assigned_to',$changes).
1601 "Assigned to: ". $this->getAssignedRealName() .
1602 " (". $this->getAssignedUnixName(). ")"."\n".
1603 $this->marker('summary',$changes).
1604 "Summary: ". util_unconvert_htmlspecialchars( $this->getSummary() )." \n";
1606 // Now display the extra fields
1607 $efd = $this->getExtraFieldDataText();
1608 foreach ($efd as $efid => $ef) {
1609 $body .= $this->marker('extra_fields', $changes, $efid);
1610 $body .= $ef["name"].": ".htmlspecialchars_decode($ef["value"])."\n";
1613 $subject='['. $this->ArtifactType->Group->getUnixName() . '-' . $name . ']' . $this->getStringID() .' '. util_unconvert_htmlspecialchars( $this->getSummary() );
1616 // get all the email addresses that are monitoring this request or the ArtifactType
1617 $monitor_ids = array_merge($this->getMonitorIds(), $this->ArtifactType->getMonitorIds());
1619 // initial creation, we just get the users monitoring the ArtifactType
1620 $monitor_ids = $this->ArtifactType->getMonitorIds();
1624 if ($more_addresses) {
1625 $emails[] = $more_addresses;
1627 //we don't email the current user
1628 if ($this->getAssignedTo() != user_getid()) {
1629 $monitor_ids[] = $this->getAssignedTo();
1631 if ($this->getSubmittedBy() != user_getid()) {
1632 $monitor_ids[] = $this->getSubmittedBy();
1634 //initial submission
1636 //if an email is set for this ArtifactType
1637 //add that address to the BCC: list
1638 if ($this->ArtifactType->getEmailAddress()) {
1639 $emails[] = $this->ArtifactType->getEmailAddress();
1643 if ($this->ArtifactType->emailAll()) {
1644 $emails[] = $this->ArtifactType->getEmailAddress();
1648 $body .= "\n\nInitial Comment:".
1649 "\n".util_unconvert_htmlspecialchars( $this->getDetails() ) .
1650 "\n\n----------------------------------------------------------------------";
1654 Now include the followups
1656 $result2=$this->getMessages();
1658 $rows=db_numrows($result2);
1660 if ($result2 && $rows > 0) {
1661 for ($i=0; $i<$rows; $i++) {
1663 // for messages posted by non-logged-in users,
1664 // we grab the email they gave us
1666 // otherwise we use the confirmed one from the users table
1668 if (db_result($result2,$i,'user_id') == 100) {
1669 $emails[] = db_result($result2,$i,'from_email');
1671 $monitor_ids[] = db_result($result2,$i,'user_id');
1676 $body .= $this->marker('details',$changes);
1678 $body .= "Comment By: ". db_result($result2,$i,'realname') . " (".db_result($result2,$i,'user_name').")".
1679 "\nDate: ". date( _('Y-m-d H:i'),db_result($result2,$i,'adddate') ).
1681 "\n".util_unconvert_htmlspecialchars( db_result($result2,$i,'body') ).
1682 "\n\n----------------------------------------------------------------------";
1688 $body .= "\n\nYou can respond by visiting: ".
1689 "\n".util_make_url ('/tracker/?func=detail&atid='. $this->ArtifactType->getID() .
1690 "&aid=". $this->getID() .
1691 "&group_id=". $this->ArtifactType->Group->getID());
1693 //only send if some recipients were found
1694 if (count($emails) < 1 && count($monitor_ids) < 1) {
1698 if (count($monitor_ids) < 1) {
1699 $monitor_ids=array();
1701 $monitor_ids=array_unique($monitor_ids);
1704 $from = $this->ArtifactType->getReturnEmailAddress();
1705 $extra_headers = 'Reply-to: '.$from;
1707 // load the e-mail addresses of the users
1708 $users = user_get_objects($monitor_ids);
1709 if (count($users) > 0) {
1710 foreach ($users as $user) {
1711 if ($user->getStatus() == "A") { //we are only sending emails to active users
1712 $emails[] = $user->getEmail();
1717 //now remove all duplicates from the email list
1718 if (count($emails) > 0) {
1719 $bcc = implode(',',array_unique($emails));
1720 util_send_message('', $subject, $body, $from, $bcc, '', $extra_headers);
1723 $this->sendSubjectMsg = $subject;
1724 $this->sendBodyMsg = $body;
1726 //util_handle_message($monitor_ids,$subject,$body,$BCC);
1732 * getExtraFieldDataText - Return the extra fields' data in a human-readable form.
1734 * @return array Array containing field ID => field name and value associated to it for
1737 function getExtraFieldDataText() {
1738 // First we get the list of extra fields and the data
1739 // associated to the fields
1740 $efs = $this->ArtifactType->getExtraFields();
1741 $efd = $this->getExtraFieldData();
1745 foreach ($efs as $efid => $ef) {
1746 $name = $ef["field_name"];
1747 $type = $ef["field_type"];
1749 // Get the value according to the type
1752 // for these types, the associated value comes straight
1753 case ARTIFACT_EXTRAFIELDTYPE_TEXT:
1754 case ARTIFACT_EXTRAFIELDTYPE_TEXTAREA:
1755 case ARTIFACT_EXTRAFIELDTYPE_RELATION:
1756 case ARTIFACT_EXTRAFIELDTYPE_INTEGER:
1757 if (isset($efd[$efid])) {
1758 $value = $efd[$efid];
1764 // the other types have and ID or an array of IDs associated to them
1766 if (isset($efd[$efid])) {
1767 $value = $this->ArtifactType->getElementName($efd[$efid]);
1773 $return[$efid] = array("name" => $name, "value" => $value, 'type' => $type);
1780 * castVote - Vote on this tracker item or retract the vote
1781 * @param bool $value true to cast, false to retract
1782 * @return bool success (false sets error message)
1784 function castVote($value = true) {
1785 if (!($uid = user_getid()) || $uid == 100) {
1786 $this->setMissingParamsError(_('User ID not passed'));
1789 if (!$this->ArtifactType->canVote()) {
1790 $this->setPermissionDeniedError();
1793 $has_vote = $this->hasVote($uid);
1794 if ($has_vote == $value) {
1795 /* nothing changed */
1799 $res = db_query_params('INSERT INTO artifact_votes (artifact_id, user_id) VALUES ($1, $2)',
1800 array($this->getID(), $uid));
1802 $res = db_query_params('DELETE FROM artifact_votes WHERE artifact_id=$1 AND user_id=$2',
1803 array($this->getID(), $uid));
1806 $this->setError(db_error());
1813 * hasVote - Check if a user has voted on this tracker item
1815 * @param int $uid user ID (default: current user)
1816 * @return bool true if a vote exists
1818 function hasVote($uid=false) {
1820 $uid = user_getid();
1822 if (!$uid || $uid == 100) {
1825 $res = db_query_params('SELECT * FROM artifact_votes WHERE artifact_id=$1 AND user_id=$2',
1826 array($this->getID(), $uid));
1827 return (db_numrows($res) == 1);
1831 * getVotes - get number of valid cast and potential votes
1833 * @return array (votes, voters, percent)
1835 function getVotes() {
1836 if ($this->votes !== false) {
1837 return $this->votes;
1840 $voters = $this->ArtifactType->getVoters();
1841 unset($voters[0]); /* just in case */
1842 unset($voters[100]); /* need users */
1843 if (($numvoters = count($voters)) < 1) {
1844 $this->votes = array(0, 0, 0);
1845 return $this->votes;
1848 $res = db_query_params('SELECT COUNT(*) AS count FROM artifact_votes WHERE artifact_id=$1 AND user_id=ANY($2)',
1849 array($this->getID(), db_int_array_to_any_clause($voters)));
1850 $db_count = db_fetch_array($res);
1851 $numvotes = $db_count['count'];
1853 /* check for invalid values */
1854 if ($numvotes < 0 || $numvoters < $numvotes) {
1855 $this->votes = array(-1, -1, 0);
1857 $this->votes = array($numvotes, $numvoters,
1858 (int)($numvotes * 100 / $numvoters + 0.5));
1860 return $this->votes;
1863 function hasRelations() {
1864 $aid = $this->getID();
1865 $res = db_query_params('SELECT *
1866 FROM artifact_extra_field_list, artifact_extra_field_data, artifact_group_list, artifact, groups
1868 AND artifact_extra_field_list.extra_field_id=artifact_extra_field_data.extra_field_id
1869 AND artifact_group_list.group_artifact_id = artifact_extra_field_list.group_artifact_id
1870 AND artifact.artifact_id = artifact_extra_field_data.artifact_id
1871 AND groups.group_id = artifact_group_list.group_id
1872 AND (field_data = $1 OR field_data LIKE $2 OR field_data LIKE $3 OR field_data LIKE $4)
1873 ORDER BY artifact_group_list.group_id ASC, name ASC, artifact.artifact_id ASC',
1878 if (db_numrows($res)>0) {
1885 class ArtifactComparator {
1886 var $criterion = 'artifact_id' ;
1887 var $order = 'ASC' ;
1889 function Compare ($a, $b) {
1890 if ($this->order == 'DESC') {
1891 $c = $a ; $a = $b ; $b = $c ;
1893 switch ($this->criterion) {
1895 $namecmp = strcoll ($a->getSummary(),
1897 if ($namecmp != 0) {
1902 $namecmp = strcoll (user_get_object($a->getAssignedTo())->getRealName(),
1903 user_get_object($b->getAssignedTo())->getRealName()) ;
1904 if ($namecmp != 0) {
1908 case 'submitted_by':
1909 $namecmp = strcoll (user_get_object($a->getSubmittedBy())->getRealName(),
1910 user_get_object($b->getSubmittedBy())->getRealName()) ;
1911 if ($namecmp != 0) {
1916 $a_date = $a->getOpenDate() ;
1917 $b_date = $b->getOpenDate() ;
1918 return ($a_date < $b_date) ? -1 : 1;
1921 $a_date = $a->getCloseDate() ;
1922 $b_date = $b->getCloseDate() ;
1923 return ($a_date < $b_date) ? -1 : 1;
1925 case 'last_modified_date':
1926 $a_date = $a->getLastModifiedDate() ;
1927 $b_date = $b->getLastModifiedDate() ;
1928 return ($a_date < $b_date) ? -1 : 1;
1931 $a_priority = $a->getPriority() ;
1932 $b_priority = $b->getPriority() ;
1933 return ($a_priority < $b_priority) ? -1 : 1;
1937 $a_votes = $a->votes[0];
1939 $b_votes = $b->votes[0];
1940 return ($a_votes < $b_votes) ? -1 : 1;
1944 $a_votes = $a->votes[1];
1946 $b_votes = $b->votes[1];
1947 return ($a_votes < $b_votes) ? -1 : 1;
1951 $a_votes = $a->votes[2];
1953 $b_votes = $b->votes[2];
1954 return ($a_votes < $b_votes) ? -1 : 1;
1957 $aa=$a->getExtraFieldDataText();
1958 $ba=$b->getExtraFieldDataText();
1959 if(!isset($this->criterion) || empty($this->criterion)) {
1962 $criterion = $this->criterion;
1964 $af=$aa[$criterion]['value'];
1965 $bf=$ba[$criterion]['value'];
1966 $namecmp = strcoll ($af,$bf) ;
1967 if ($namecmp != 0) {
1973 // When in doubt, sort on artifact ID
1974 $aid = $a->getID() ;
1975 $bid = $b->getID() ;
1979 return ($aid < $bid) ? -1 : 1;
1983 function sortArtifactList (&$list, $criterion='name', $order='ASC') {
1984 $cmp = new ArtifactComparator () ;
1985 $cmp->criterion = $criterion ;
1986 $cmp->order = $order ;
1988 return usort ($list, array ($cmp, 'Compare')) ;
1993 // c-file-style: "bsd"