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 $fieldName Name of the field
23 * @param string|array $fieldValue Value of the field
24 * @param string $vocabulary Vocabulary of a tracker
27 function findType($fieldName, $fieldValue, $vocabulary){
28 if (is_array($fieldValue)){
29 return ARTIFACT_EXTRAFIELDTYPE_MULTISELECT;
30 } elseif(array_key_exists($fieldName, $vocabulary)) {
31 return ARTIFACT_EXTRAFIELDTYPE_SELECT;
33 return ARTIFACT_EXTRAFIELDTYPE_TEXT;
38 * createFieldElements - Add elements (choices) to an extra field
39 * @param ArtifactExtraField $aef The artifact extra field where the choices should be added
40 * @param array $vocabulary The choices to be declared for the specified extra field
41 * @return bool false if failed
43 function createFieldElements($aef, $vocabulary){
44 //TODO:Add each element to tracker extra field
45 foreach($vocabulary as $element){
46 if(!($element=='None')){
47 $aefe = new ArtifactExtraFieldElement($aef);
48 if (!$aefe->create(addslashes($element))) {
58 * createFields - Create the custom fields for the specified ArtifactType
59 * @param ArtifactType $at The artifact type to be modified
60 * @param array $data Vocabulary and artifacts for the ArtifactType
61 * @return bool false if failed
63 function createFields($at, $data){
64 global $NOT_EXTRA_FIELDS;
66 //TODO:Create ExtraFields
67 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
68 $artifactToCheck = $data["artifacts"][0];
69 foreach($artifactToCheck as $fieldName => $fieldValue){
70 if (!in_array($fieldName, $NOT_EXTRA_FIELDS)){
71 $type = findType($fieldName, $fieldValue, $data["vocabulary"]);
72 $aef = new ArtifactExtraField($at);
74 $defaultExtraFieldsSettings = array(0,0,0);
75 $defaultTextFieldsSettings = array(40,100,0);
77 if($type==ARTIFACT_EXTRAFIELDTYPE_TEXT){
78 $extraFieldSettings = $defaultTextFieldsSettings;
81 $extraFieldSettings = $defaultExtraFieldsSettings;
83 if (!$aef->create(addslashes($fieldName), $type, $extraFieldSettings[0], $extraFieldSettings[1], $extraFieldSettings[2])) {
88 if(!($type==ARTIFACT_EXTRAFIELDTYPE_TEXT)){
89 createFieldElements($aef, $data['vocabulary'][$fieldName]);
98 * createTracker - Create a specific tracker from data in the specified group
99 * @param string $tracker Tracker type (bugs, support, ...)
100 * @param Group $group The group which the tracker belongs to
101 * @param array $data Tracker data from JSON
102 * @return ArtifactType the tracker created
105 function createTracker($tracker, $group, $data){
108 $at = new ArtifactType($group);
109 if (!$at || !is_object($at)) {
113 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
115 $base_tracker_association = array( 'bugs' => TRACKER_BUGS, 'support' => TRACKER_SUPPORT, 'patches' => TRACKER_PATCHES, 'features' => TRACKER_FEATURES );
116 if(array_key_exists($tracker, $base_tracker_association)){
117 $valueType = $base_tracker_association[$tracker];
122 $is_public = TRACKER_IS_PUBLIC;
123 $allow_anon = TRACKER_ALLOW_ANON;
128 $submit_instructions = 0;
131 if (!$at->create($data["label"], $data["label"], $is_public, $allow_anon, $email_all, $email_address, $due_period, $use_resolution, $submit_instructions, $use_resolution, $valueType)) {
135 // Create each field in the tracker
136 createFields($at, $data);
143 * deleteTrackers - Delete all existing default trackers from a projet
144 * @param Group A Group object
146 function deleteTrackers($group){
147 $res = db_query_params ('SELECT group_artifact_id FROM artifact_group_list
148 WHERE group_id=$1 AND datatype > 0',
149 array ($group->getID()));
150 while($row=db_fetch_array($res)){
151 $at = & artifactType_get_object($row['group_artifact_id']);
152 $at->delete(true,true);
153 //print $at->getID();
159 * addComments - Add followup comments to an Artifact Object
160 * @param Artifact the artifact object where history should be added
161 * @param array the artifact's data in json format (an array)
163 function addComments($artifact, $jsonArtifact){
164 foreach($jsonArtifact['comments'] as $c){
165 $time = strtotime($c['date']);
166 $uid = user_get_object_by_name($c['submitter'])->getID();
167 $importData = array('time' => $time, 'user' => $uid);
168 $artifact->addMessage($c['comment'],false,false, $importData);
173 * addHistory - Add history of changes to an Artifact Object
174 * @param Artifact the artifact object where history should be added
175 * @param array the artifact's data in json format (an array)
177 function addHistory($artifact, $jsonArtifact){
178 foreach($jsonArtifact['history'] as $h){
179 $time = strtotime($h['date']);
180 $uid = user_get_object_by_name($h['by'])->getID();
181 $importData = array('time' => $time, 'user' => $uid);
184 if($h['field']=='assigned_to'){
186 $old = user_get_object_by_name($old)->getID();
191 if($h['field']=='status_id'){
192 $status = array('Open' =>1, 'Closed' => 2, 'Deleted' => 3);
193 $old = $status[$old];
195 if($h['field']=='close_date'){
196 $old = strtotime($old);
199 $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);
216 $finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // Retourne le type mime
218 echo "error opening fileinfo";
222 $ftype = $finfo->file($path);
223 $time = strtotime($a['date']);
224 $uid = user_get_object_by_name($a['by'])->getID();
225 $importData = array('user' => $uid, 'time' => $time);
227 //we have no descriptions for files => None
228 $af->create($fn,$ftype,$fs,$bin_data,'None',$importData);
234 * createArtifacts - Create all the artifacts for an ArtifactType from an array of data.
235 * @param ArtifactType The ArtifactType object which the artifacts to be added belong to.
236 * @param array The data of all the artifacts of the current Type (dictionary)
240 function createArtifacts($at, $data, $hashrn, $hashlogin) {
241 global $NOT_EXTRA_FIELDS;
244 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
245 $extra_fields_ids = $at->getExtraFields();
247 foreach($extra_fields_ids as $extraField => $val){
248 $extras[$val[2]] = $extraField;
251 foreach($extras as $fieldName => $fieldId){
252 $elements = $at->getExtraFieldElements($fieldId);
253 foreach($elements as $extra_element){
254 $extra_elements[$fieldId][$extra_element['element_name']]=$extra_element['element_id'];
257 //new dBug($extra_elements);
258 //$arti = new Artifact($at);
259 //$artifactTypeExtraFields = $arti->getExtraFieldDataText();
262 foreach ($data as $artifact){
263 $arti = new Artifact($at);
264 $extra_fields_array = array();
265 foreach($artifact as $fieldName => $fieldValue){
266 if(!in_array($fieldName, $NOT_EXTRA_FIELDS)){
267 //new dBug(array($fieldName,$fieldValue));
268 if(is_array($fieldValue)){
270 foreach($fieldValue as $multiFieldValue){
271 $mf[] = $extra_elements[$extras[$fieldName]][$multiFieldValue];
273 $extra_fields_array[$extras[$fieldName]] = $mf;
275 $extra_fields_array[$extras[$fieldName]] = $extra_elements[$extras[$fieldName]][$fieldValue];
279 //create the artif here with $extra_fields_array as extra fields
280 //new dBug($extra_fields_array);
282 $uid = user_get_object_by_name($artifact['submitter'])->getID();
283 //TODO:Search in hash table for corresponding mail for id, lookup object by mail, get ID
284 //get time from epoch
285 $timestamp = strtotime($artifact['date']);
286 //TODO:get the id of the assigned_to user, in the meantime -> Nobody (100)
288 //assigned to : real name dans le pluck
289 if($artifact['assigned_to']=='Nobody'){
292 $m = $hashrn[$artifact['assigned_to']];
293 $assigned_to = user_get_object_by_mail($m)->getID();
294 //new dBug(array($m,$assigned_to));
297 $arti->create($artifact['summary'],$artifact['description'],$assigned_to,substr($artifact['priority'],0,1),$extra_fields_array,array('user' => $uid, 'time' => $timestamp));
298 //TODO:pass only relevant JSON info
299 addComments($arti, $artifact);
300 addHistory($arti, $artifact);
301 addFiles($arti, $artifact);
303 if(array_key_exists('closed_at', $artifact)){
305 $timestamp_closed = strtotime($artifact['closed_at']);
307 $arti->setStatus(2, $timestamp_closed);
312 * tracker_fill - Create trackers from an array in a given group
313 * @param array $trackers Trackers part of a JSON pluck, including label, artifacts, vocabulary...
314 * @param int $group_id Group id of the group where the trackers should be added
315 * @param array $users
317 function tracker_fill($trackers, $group_id, $users){
319 $group = group_get_object($group_id);
320 if (!$group || !is_object($group)) {
321 print "error retrieving group from id";
322 } elseif ($group->isError()) {
326 //create hash table hashrn{real_name:mail} & hashlogin{id:mail}
327 foreach($users as $user => $infos){
328 $hashrn[$infos['real_name']] = $infos['mail'];
329 $hashlogin[$user] = $infos['mail'];
332 //existing tracker deletion
333 deleteTrackers($group);
336 foreach ($trackers as $data){
338 $at = createTracker($data['type'], $group, $data);
339 createArtifacts($at, $data['artifacts'], $hashrn, $hashlogin);