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;
30 elseif(array_key_exists($fieldName, $vocabulary)){
32 return ARTIFACT_EXTRAFIELDTYPE_SELECT;
35 return ARTIFACT_EXTRAFIELDTYPE_TEXT;
41 * createFieldElements - Add elements (choices) to an extra field
42 * @param ArtifactExtraField The artifact extra field where the choices should be added
43 * @param array The choices to be declared for the specified extra field
44 * @return false if failed
46 function createFieldElements($aef, $vocabulary){
47 //TODO:Add each element to tracker extra field
48 foreach($vocabulary as $element){
49 if(!($element=='None')){
50 $aefe = new ArtifactExtraFieldElement($aef);
51 if (!$aefe->create(addslashes($element))) {
61 * createFields - Create the custom fields for the specified ArtifactType
62 * @param ArtifactType The artifact type to be modified
63 * @param array Vocabulary and artifacts for the ArtifactType
64 * @return false if failed
66 function createFields($at, $data){
67 global $NOT_EXTRA_FIELDS;
69 //TODO:Create ExtraFields
70 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
71 $artifactToCheck = $data["artifacts"][0];
72 foreach($artifactToCheck as $fieldName => $fieldValue){
73 if (!in_array($fieldName, $NOT_EXTRA_FIELDS)){
74 $type = findType($fieldName, $fieldValue, $data["vocabulary"]);
75 $aef = new ArtifactExtraField($at);
77 $defaultExtraFieldsSettings = array(0,0,0);
78 $defaultTextFieldsSettings = array(40,100,0);
80 if($type==ARTIFACT_EXTRAFIELDTYPE_TEXT){
81 $extraFieldSettings = $defaultTextFieldsSettings;
84 $extraFieldSettings = $defaultExtraFieldsSettings;
86 if (!$aef->create(addslashes($fieldName), $type, $extraFieldSettings[0], $extraFieldSettings[1], $extraFieldSettings[2])) {
91 if(!($type==ARTIFACT_EXTRAFIELDTYPE_TEXT)){
92 createFieldElements($aef, $data['vocabulary'][$fieldName]);
101 * createTracker - Create a specific tracker from data in the specified group
102 * @param string Tracker type (bugs, support, ...)
103 * @param Group The group which the tracker belongs to
104 * @param array Tracker data from JSON
105 * @return ArtifactType the tracker created
108 function createTracker($tracker, $group, $data){
111 $at = new ArtifactType($group);
112 if (!$at || !is_object($at)) {
116 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
118 $base_tracker_association = array( 'bugs' => TRACKER_BUGS, 'support' => TRACKER_SUPPORT, 'patches' => TRACKER_PATCHES, 'features' => TRACKER_FEATURES );
119 if(array_key_exists($tracker, $base_tracker_association)){
120 $valueType = $base_tracker_association[$tracker];
125 $is_public = TRACKER_IS_PUBLIC;
126 $allow_anon = TRACKER_ALLOW_ANON;
131 $submit_instructions = 0;
134 if (!$at->create($data["label"], $data["label"], $is_public, $allow_anon, $email_all, $email_address, $due_period, $use_resolution, $submit_instructions, $use_resolution, $valueType)) {
138 // Create each field in the tracker
139 createFields($at, $data);
146 * deleteTrackers - Delete all existing default trackers from a projet
147 * @param Group A Group object
149 function deleteTrackers($group){
150 $res = db_query_params ('SELECT group_artifact_id FROM artifact_group_list
151 WHERE group_id=$1 AND datatype > 0',
152 array ($group->getID()));
153 while($row=db_fetch_array($res)){
154 $at = & artifactType_get_object($row['group_artifact_id']);
155 $at->delete(true,true);
156 //print $at->getID();
162 * addComments - Add followup comments to an Artifact Object
163 * @param Artifact the artifact object where history should be added
164 * @param array the artifact's data in json format (an array)
166 function addComments($artifact, $jsonArtifact){
167 foreach($jsonArtifact['comments'] as $c){
168 $time = strtotime($c['date']);
169 $uid =&user_get_object_by_name($c['submitter'])->getID();
170 $importData = array('time' => $time, 'user' => $uid);
171 $artifact->addMessage($c['comment'],false,false, $importData);
176 * addHistory - Add history of changes to an Artifact Object
177 * @param Artifact the artifact object where history should be added
178 * @param array the artifact's data in json format (an array)
180 function addHistory($artifact, $jsonArtifact){
181 foreach($jsonArtifact['history'] as $h){
182 $time = strtotime($h['date']);
183 $uid =&user_get_object_by_name($h['by'])->getID();
184 $importData = array('time' => $time, 'user' => $uid);
187 if($h['field']=='assigned_to'){
189 $old =&user_get_object_by_name($old)->getID();
194 if($h['field']=='status_id'){
195 $status = array('Open' =>1, 'Closed' => 2, 'Deleted' => 3);
196 $old = $status[$old];
198 if($h['field']=='close_date'){
199 $old = strtotime($old);
202 $artifact->addHistory($h['field'],$old, $importData);
207 function addFiles($artifact, $jsonArtifact){
208 foreach($jsonArtifact['attachments'] as $a){
210 $path = '/tmp/'.$a['url'];
212 $af = new ArtifactFile($artifact);
213 $fn = $a['filename'];
214 //$bin_data = 0;//load bin data from $a['url']?
216 $bin_data = file_get_contents($path);
218 $fs = filesize($path);
221 $finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // Retourne le type mime
223 echo "error opening fileinfo";
227 $ftype = $finfo->file($path);
228 $time = strtotime($a['date']);
229 $uid =&user_get_object_by_name($a['by'])->getID();
230 $importData = array('user' => $uid, 'time' => $time);
232 //we have no descriptions for files => None
233 $af->create($fn,$ftype,$fs,$bin_data,'None',$importData);
240 * createArtifacts - Create all the artifacts for an ArtifactType from an array of data.
241 * @param ArtifactType The ArtifactType object which the artifacts to be added belong to.
242 * @param array The data of all the artifacts of the current Type (dictionary)
246 function createArtifacts($at, $data, $hashrn, $hashlogin) {
247 global $NOT_EXTRA_FIELDS;
250 //include $GLOBALS['gfcommon'].'import/import_arrays.php';
251 $extra_fields_ids = $at->getExtraFields();
254 foreach($extra_fields_ids as $extraField => $val){
255 $extras[$val[2]] = $extraField;
258 foreach($extras as $fieldName => $fieldId){
259 $elements = $at->getExtraFieldElements($fieldId);
260 foreach($elements as $extra_element){
261 $extra_elements[$fieldId][$extra_element['element_name']]=$extra_element['element_id'];
264 //new dBug($extra_elements);
265 //$arti = new Artifact($at);
266 //$artifactTypeExtraFields = $arti->getExtraFieldDataText();
269 foreach ($data as $artifact){
270 $arti = new Artifact($at);
271 $extra_fields_array = array();
272 foreach($artifact as $fieldName => $fieldValue){
273 if(!in_array($fieldName, $NOT_EXTRA_FIELDS)){
274 //new dBug(array($fieldName,$fieldValue));
275 if(is_array($fieldValue)){
277 foreach($fieldValue as $multiFieldValue){
278 $mf[] = $extra_elements[$extras[$fieldName]][$multiFieldValue];
280 $extra_fields_array[$extras[$fieldName]] = $mf;
282 $extra_fields_array[$extras[$fieldName]] = $extra_elements[$extras[$fieldName]][$fieldValue];
286 //create the artif here with $extra_fields_array as extra fields
287 //new dBug($extra_fields_array);
289 $uid =&user_get_object_by_name($artifact['submitter'])->getID();
290 //TODO:Search in hash table for corresponding mail for id, lookup object by mail, get ID
291 //get time from epoch
292 $timestamp = strtotime($artifact['date']);
293 //TODO:get the id of the assigned_to user, in the meantime -> Nobody (100)
296 //assigned to : real name dans le pluck
297 if($artifact['assigned_to']=='Nobody'){
300 $m = $hashrn[$artifact['assigned_to']];
301 $assigned_to =&user_get_object_by_mail($m)->getID();
302 //new dBug(array($m,$assigned_to));
305 $arti->create($artifact['summary'],$artifact['description'],$assigned_to,substr($artifact['priority'],0,1),$extra_fields_array,array('user' => $uid, 'time' => $timestamp));
306 //TODO:pass only relevant JSON info
307 addComments($arti, $artifact);
308 addHistory($arti, $artifact);
309 addFiles($arti, $artifact);
311 if(array_key_exists('closed_at', $artifact)){
313 $timestamp_closed = strtotime($artifact['closed_at']);
315 $arti->setStatus(2, $timestamp_closed);
320 * tracker_fill - Create trackers from an array in a given group
321 * @param array Trackers part of a JSON pluck, including label, artifacts, vocabulary...
322 * @param int Group id of the group where the trackers should be added
324 function tracker_fill($trackers, $group_id, $users){
326 $group =& group_get_object($group_id);
327 if (!$group || !is_object($group)) {
328 print "error retrieving group from id";
329 } elseif ($group->isError()) {
333 //create hash table hashrn{real_name:mail} & hashlogin{id:mail}
334 foreach($users as $user => $infos){
335 $hashrn[$infos['real_name']] = $infos['mail'];
336 $hashlogin[$user] = $infos['mail'];
339 //existing tracker deletion
340 deleteTrackers($group);
343 foreach ($trackers as $data){
346 $at = createTracker($data['type'], $group, $data);
347 createArtifacts($at, $data['artifacts'], $hashrn, $hashlogin);