2 require_once '../env.inc.php';
3 require_once $gfwww.'include/pre.php';
4 require_once($gfcommon.'tracker/ArtifactType.class.php');
5 require_once($gfcommon.'tracker/Artifact.class.php');
6 require_once($gfcommon.'include/User.class.php');
7 require_once($gfcommon.'tracker/ArtifactExtraField.class.php');
8 require_once($gfcommon.'tracker/ArtifactFile.class.php');
9 //require_once($gfcommon.'import/import_arrays.php');
11 define('TRACKER_IS_PUBLIC', 1);
12 define('TRACKER_ALLOW_ANON', 0);
13 define('TRACKER_BUGS', 1);
14 define('TRACKER_SUPPORT', 2);
15 define('TRACKER_PATCHES', 3);
16 define('TRACKER_FEATURES', 4);
18 static $NOT_EXTRA_FIELDS = array('assigned_to', 'attachments', 'class', 'comments', 'date', 'history', 'priority', 'status_id', 'submitter', 'summary', 'closed_at', 'description', 'type', 'type_of_search', 'id');//last 3 should not be there at all.
21 * findType - get the type of a field from its name, value, and vocabulary : default 0 (text box), otherwise 1 (select box) or 2 (multi choice field)
22 * @param string Name of the field
23 * @param string|array Value of the field
24 * @param $vocabulary Vocabulary of a tracker
26 function findType($fieldName, $fieldValue, $vocabulary){
27 if (is_array($fieldValue)){
28 return ARTIFACT_EXTRAFIELDTYPE_MULTISELECT;
29 } elseif(array_key_exists($fieldName, $vocabulary)) {
30 return ARTIFACT_EXTRAFIELDTYPE_SELECT;
32 return ARTIFACT_EXTRAFIELDTYPE_TEXT;
37 * createFieldElements - Add elements (choices) to an extra field
38 * @param ArtifactExtraField The artifact extra field where the choices should be added
39 * @param array The choices to be declared for the specified extra field
40 * @return false if failed
42 function createFieldElements($aef, $vocabulary){
43 //TODO:Add each element to tracker extra field
44 foreach($vocabulary as $element){
45 if(!($element=='None')){
46 $aefe = new ArtifactExtraFieldElement($aef);
47 if (!$aefe->create(addslashes($element))) {
57 * createFields - Create the custom fields for the specified ArtifactType
58 * @param ArtifactType The artifact type to be modified
59 * @param array Vocabulary and artifacts for the ArtifactType
60 * @return false if failed
62 function createFields($at, $data){
63 global $NOT_EXTRA_FIELDS;
65 //TODO:Create ExtraFields
66 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
67 $artifactToCheck = $data["artifacts"][0];
68 foreach($artifactToCheck as $fieldName => $fieldValue){
69 if (!in_array($fieldName, $NOT_EXTRA_FIELDS)){
70 $type = findType($fieldName, $fieldValue, $data["vocabulary"]);
71 $aef = new ArtifactExtraField($at);
73 $defaultExtraFieldsSettings = array(0,0,0);
74 $defaultTextFieldsSettings = array(40,100,0);
76 if($type==ARTIFACT_EXTRAFIELDTYPE_TEXT){
77 $extraFieldSettings = $defaultTextFieldsSettings;
80 $extraFieldSettings = $defaultExtraFieldsSettings;
82 if (!$aef->create(addslashes($fieldName), $type, $extraFieldSettings[0], $extraFieldSettings[1], $extraFieldSettings[2])) {
87 if(!($type==ARTIFACT_EXTRAFIELDTYPE_TEXT)){
88 createFieldElements($aef, $data['vocabulary'][$fieldName]);
97 * createTracker - Create a specific tracker from data in the specified group
98 * @param string Tracker type (bugs, support, ...)
99 * @param Group The group which the tracker belongs to
100 * @param array Tracker data from JSON
101 * @return ArtifactType the tracker created
104 function createTracker($tracker, $group, $data){
107 $at = new ArtifactType($group);
108 if (!$at || !is_object($at)) {
112 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
114 $base_tracker_association = array( 'bugs' => TRACKER_BUGS, 'support' => TRACKER_SUPPORT, 'patches' => TRACKER_PATCHES, 'features' => TRACKER_FEATURES );
115 if(array_key_exists($tracker, $base_tracker_association)){
116 $valueType = $base_tracker_association[$tracker];
121 $is_public = TRACKER_IS_PUBLIC;
122 $allow_anon = TRACKER_ALLOW_ANON;
127 $submit_instructions = 0;
130 if (!$at->create($data["label"], $data["label"], $is_public, $allow_anon, $email_all, $email_address, $due_period, $use_resolution, $submit_instructions, $use_resolution, $valueType)) {
134 // Create each field in the tracker
135 createFields($at, $data);
142 * deleteTrackers - Delete all existing default trackers from a projet
143 * @param Group A Group object
145 function deleteTrackers($group){
146 $res = db_query_params ('SELECT group_artifact_id FROM artifact_group_list
147 WHERE group_id=$1 AND datatype > 0',
148 array ($group->getID()));
149 while($row=db_fetch_array($res)){
150 $at = & artifactType_get_object($row['group_artifact_id']);
151 $at->delete(true,true);
152 //print $at->getID();
158 * addComments - Add followup comments to an Artifact Object
159 * @param Artifact the artifact object where history should be added
160 * @param array the artifact's data in json format (an array)
162 function addComments($artifact, $jsonArtifact){
163 foreach($jsonArtifact['comments'] as $c){
164 $time = strtotime($c['date']);
165 $uid =&user_get_object_by_name($c['submitter'])->getID();
166 $importData = array('time' => $time, 'user' => $uid);
167 $artifact->addMessage($c['comment'],false,false, $importData);
172 * addHistory - Add history of changes to an Artifact Object
173 * @param Artifact the artifact object where history should be added
174 * @param array the artifact's data in json format (an array)
176 function addHistory($artifact, $jsonArtifact){
177 foreach($jsonArtifact['history'] as $h){
178 $time = strtotime($h['date']);
179 $uid =&user_get_object_by_name($h['by'])->getID();
180 $importData = array('time' => $time, 'user' => $uid);
183 if($h['field']=='assigned_to'){
185 $old =&user_get_object_by_name($old)->getID();
190 if($h['field']=='status_id'){
191 $status = array('Open' =>1, 'Closed' => 2, 'Deleted' => 3);
192 $old = $status[$old];
194 if($h['field']=='close_date'){
195 $old = strtotime($old);
198 $artifact->addHistory($h['field'],$old, $importData);
203 function addFiles($artifact, $jsonArtifact){
204 foreach($jsonArtifact['attachments'] as $a){
206 $path = '/tmp/'.$a['url'];
208 $af = new ArtifactFile($artifact);
209 $fn = $a['filename'];
210 //$bin_data = 0;//load bin data from $a['url']?
212 $bin_data = file_get_contents($path);
214 $fs = filesize($path);
217 $finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // Retourne le type mime
219 echo "error opening fileinfo";
223 $ftype = $finfo->file($path);
224 $time = strtotime($a['date']);
225 $uid =&user_get_object_by_name($a['by'])->getID();
226 $importData = array('user' => $uid, 'time' => $time);
228 //we have no descriptions for files => None
229 $af->create($fn,$ftype,$fs,$bin_data,'None',$importData);
236 * createArtifacts - Create all the artifacts for an ArtifactType from an array of data.
237 * @param ArtifactType The ArtifactType object which the artifacts to be added belong to.
238 * @param array The data of all the artifacts of the current Type (dictionary)
242 function createArtifacts($at, $data, $hashrn, $hashlogin) {
243 global $NOT_EXTRA_FIELDS;
246 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
247 $extra_fields_ids = $at->getExtraFields();
250 foreach($extra_fields_ids as $extraField => $val){
251 $extras[$val[2]] = $extraField;
254 foreach($extras as $fieldName => $fieldId){
255 $elements = $at->getExtraFieldElements($fieldId);
256 foreach($elements as $extra_element){
257 $extra_elements[$fieldId][$extra_element['element_name']]=$extra_element['element_id'];
260 //new dBug($extra_elements);
261 //$arti = new Artifact($at);
262 //$artifactTypeExtraFields = $arti->getExtraFieldDataText();
265 foreach ($data as $artifact){
266 $arti = new Artifact($at);
267 $extra_fields_array = array();
268 foreach($artifact as $fieldName => $fieldValue){
269 if(!in_array($fieldName, $NOT_EXTRA_FIELDS)){
270 //new dBug(array($fieldName,$fieldValue));
271 if(is_array($fieldValue)){
273 foreach($fieldValue as $multiFieldValue){
274 $mf[] = $extra_elements[$extras[$fieldName]][$multiFieldValue];
276 $extra_fields_array[$extras[$fieldName]] = $mf;
278 $extra_fields_array[$extras[$fieldName]] = $extra_elements[$extras[$fieldName]][$fieldValue];
282 //create the artif here with $extra_fields_array as extra fields
283 //new dBug($extra_fields_array);
285 $uid =&user_get_object_by_name($artifact['submitter'])->getID();
286 //TODO:Search in hash table for corresponding mail for id, lookup object by mail, get ID
287 //get time from epoch
288 $timestamp = strtotime($artifact['date']);
289 //TODO:get the id of the assigned_to user, in the meantime -> Nobody (100)
292 //assigned to : real name dans le pluck
293 if($artifact['assigned_to']=='Nobody'){
296 $m = $hashrn[$artifact['assigned_to']];
297 $assigned_to =&user_get_object_by_mail($m)->getID();
298 //new dBug(array($m,$assigned_to));
301 $arti->create($artifact['summary'],$artifact['description'],$assigned_to,substr($artifact['priority'],0,1),$extra_fields_array,array('user' => $uid, 'time' => $timestamp));
302 //TODO:pass only relevant JSON info
303 addComments($arti, $artifact);
304 addHistory($arti, $artifact);
305 addFiles($arti, $artifact);
307 if(array_key_exists('closed_at', $artifact)){
309 $timestamp_closed = strtotime($artifact['closed_at']);
311 $arti->setStatus(2, $timestamp_closed);
316 * tracker_fill - Create trackers from an array in a given group
317 * @param array Trackers part of a JSON pluck, including label, artifacts, vocabulary...
318 * @param int Group id of the group where the trackers should be added
320 function tracker_fill($trackers, $group_id, $users){
322 $group =& group_get_object($group_id);
323 if (!$group || !is_object($group)) {
324 print "error retrieving group from id";
325 } elseif ($group->isError()) {
329 //create hash table hashrn{real_name:mail} & hashlogin{id:mail}
330 foreach($users as $user => $infos){
331 $hashrn[$infos['real_name']] = $infos['mail'];
332 $hashlogin[$user] = $infos['mail'];
335 //existing tracker deletion
336 deleteTrackers($group);
339 foreach ($trackers as $data){
342 $at = createTracker($data['type'], $group, $data);
343 createArtifacts($at, $data['artifacts'], $hashrn, $hashlogin);