PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/datamatrix.php

https://github.com/drobbins/s3db
PHP | 317 lines | 190 code | 96 blank | 31 comment | 36 complexity | 33ea252abaf7fd7e5b1ed6449bc0d541 MD5 | raw file
  1. <?php
  2. #datamatrix is a generic file of functions that generates the table of statements for several outputs
  3. function create_datamatrix_header($head)
  4. {
  5. ##Head is an array that must contain verbs and object from the rules to be displayed in the table. If format is empty, will consider it to be tab delimited
  6. extract($head);
  7. $color = $_REQUEST['color'];
  8. $format = $_REQUEST['format'];
  9. #If verbs and objects are not sent in the array, it's easy to find them
  10. if(is_array($rules))
  11. {
  12. if($verbs=='') $verbs = array_map('grab_verb', $rules);
  13. if($objects=='') $objects = array_map('grab_object', $rules, $verbs);
  14. }
  15. $previous_verb ='';
  16. $current_color = 0;
  17. $matrix_header='';
  18. $parser = get_parser_characters($format);
  19. #First ROW, first and second COLS
  20. if($color=='on')
  21. $matrix_header .= sprintf("%s\n", '<table width=100% border=1 style="border-collapse: collapse;">');
  22. else
  23. $matrix_header .= sprintf($parser['begin_table']);
  24. $matrix_header .= sprintf($parser['tr']);
  25. $matrix_header .= sprintf("%s", $parser['td']);
  26. if($type!='template')
  27. $matrix_header .= sprintf("%s", $class_info['entity'].$parser['middle']);
  28. $matrix_header .= sprintf("%s", $class_info['entity'].$parser['end_td']);
  29. #Remaining COLS
  30. if(is_array($verbs))
  31. foreach($verbs as $i => $value)
  32. {
  33. $verb = $verbs[$i];
  34. ##Print different verbs in different colors (for table view, mostly, yet for others there is no harm in this being here)
  35. if($previous_verb =='')
  36. $previous_verb = $verb;
  37. else if($previous_verb!=$verb)
  38. {
  39. $previous_verb = $verb;
  40. $current_color = $current_color + 1;
  41. }
  42. $my_color='';
  43. switch($current_color%3)
  44. {
  45. case 0:
  46. $my_color='red';
  47. break;
  48. case 1:
  49. $my_color='green';
  50. break;
  51. case 2:
  52. $my_color='blue';
  53. break;
  54. }
  55. if($format=='html' && $color=="on")
  56. {$colored = '<font color="'.$my_color.'">';$endcolored='</font>';}
  57. $matrix_header .=sprintf("%s", $parser['td'].$colored.$verb.$endcolored.$parser['end_td']);
  58. }
  59. #echo '<pre>';print_r($parser);
  60. #Second ROW, first and second COLS
  61. $matrix_header .= sprintf("%s\n", $parser['end_tr']);
  62. if($color=='on')
  63. $matrix_header .= sprintf("%s\n", ' <tr bgcolor="lightblue">');
  64. else
  65. $matrix_header .= sprintf("%s", $parser['tr']);
  66. $matrix_header .=sprintf($parser['td']);
  67. if($type!='template')
  68. $matrix_header .=sprintf("UID".$parser['middle']);
  69. $matrix_header .=sprintf("Notes".$parser['end_td']);
  70. #Second row, rest of COLS
  71. if (is_array($verbs))
  72. foreach($verbs as $i => $value)
  73. {
  74. $matrix_header .= sprintf("%s", $parser['td'].$objects[$i].$parser['end_td']);
  75. }
  76. $matrix_header .= sprintf("%s", $parser['end_tr']);
  77. return $matrix_header;
  78. }
  79. function render_datamatrix_values($vals)
  80. {$action =$GLOBALS['webaction'];
  81. extract($vals);
  82. $color = $_REQUEST['color'];
  83. $format = $_REQUEST['format'];
  84. $parser = get_parser_characters($format);
  85. $rows='';
  86. $items = $instances;
  87. // if($_REQUEST['num_per_page']!='' && $_REQUEST['current_page']!='')
  88. // {
  89. // $start = (($_REQUEST['current_page']-1)*$_REQUEST['num_per_page']);
  90. // $end=($_REQUEST['num_per_page']*$_REQUEST['current_page']);
  91. // }
  92. // else {
  93. // $start = 0;
  94. // $end= count($items);
  95. // }
  96. #if(is_array($matched_resource))
  97. for($i=$start; $i<$end; $i++)
  98. {
  99. $item_id = $items[$i]['item_id'];
  100. if(!is_array($items[$i]['stats'])){
  101. $row ='';
  102. $s3ql = compact('db', 'user_id');
  103. $s3ql['select'] = '*';
  104. $s3ql['from'] = 'statements';
  105. $s3ql['where']['item_id'] =$item_id;
  106. $all_values = S3QLaction($s3ql);
  107. }
  108. else {
  109. $all_values = $items[$i]['stats'];
  110. }
  111. #echo '<pre>';print_r($all_values);exit;
  112. if(is_array($all_values) && !empty($all_values))
  113. {#replace values with filelinks and find the notes for the buttons
  114. $all_values = include_button_notes($all_values, $project_id, $db);
  115. $all_values = Values2Links($all_values);
  116. }
  117. if(count($all_values) =='0')
  118. {
  119. if($color == 'on') #This is to print each line in a different color - if line is multiple of 2, print it in AliceBlue
  120. {
  121. if($total%2==0)
  122. $row =sprintf("%s\n", ' <tr bgcolor="AliceBlue">');
  123. else
  124. $row =sprintf("%s\n", $parser['tr']);
  125. #$all_values = include_button_notes($all_values, $project_id, $db);
  126. #In case color is on, it means we are trying to achieve one of the interactive, user-friendly interfaces. Therefore, resource should come in the format of a button. Need to be dealt with care in case
  127. $resource_id_button = '<input type="button" size="10" value="'.str_pad($item_id, 6, '0', STR_PAD_LEFT).'" onClick="window.open(\''.$action['item'].'&item_id='.$item_id.'\', \'_blank\', \'width=700, height=600, location=no, titlebar=no, scrollbars=yes, resizable=yes\')">';
  128. }
  129. else
  130. $resource_id_button = $item_id;
  131. $subrow ='';
  132. $subrow .= sprintf("%s", $parser['td'].$resource_id_button.$parser['end_td']);
  133. $subrow .= sprintf("%s", $parser['td']. $items[$i]['notes'].$parser['end_td']);
  134. #Moving on to the data on the rules, since there are no values on this row, print only opening and ending the cell
  135. if (is_array($rules))
  136. foreach($rules as $j=>$value)
  137. $subrow .= sprintf("%s", $parser['td'].$parser['end_td']);
  138. $subrow .=sprintf("%s", $parser['end_tr']);
  139. $row .=$subrow;
  140. $total +=1;
  141. }
  142. else #Where there are actual values on the row
  143. {
  144. $n=(get_max_num_values($all_values, $rules)==0)?1:get_max_num_values($all_values, $rules);
  145. #$n will determine the number of lines for this item
  146. $total=0;
  147. $row ='';
  148. for($m=0; $m<$n; $m++)
  149. { //$row ='';
  150. if($_REQUEST['color']=='on')
  151. {
  152. #if($total%2==0)
  153. #$row .=sprintf("%s\n", '<tr bgcolor="AliceBlue">');}
  154. #else
  155. #$row .=sprintf("%s\n", $parser['tr']);
  156. $resource_id_button = '<input type="button" size="10" value="'.str_pad($item_id, 6, '0', STR_PAD_LEFT).'" onClick="window.open(\''.$action['item'].'&item_id='.$item_id.'\', \'_blank\', \'width=700, height=600, location=no, titlebar=no, scrollbars=yes, resizable=yes\')">';
  157. }
  158. else
  159. {
  160. $resource_id_button =$item_id;
  161. }
  162. $row .=sprintf("%s", $parser['tr']);
  163. $subrow ='';
  164. $subrow .= sprintf("%s", $parser['td'].$resource_id_button.$parser['end_td']);
  165. $subrow .= sprintf("%s", $parser['td'].$items[$i]['notes'].$parser['end_td']);
  166. #if($item_id=='3783'){ echo $row ; exit;}
  167. //print_r($rules);
  168. if (is_array($rules))
  169. foreach($rules as $j=>$value)
  170. {
  171. $rule_id = $rules[$j]['rule_id'];
  172. $values = get_value_by_rule($all_values, $rule_id);
  173. if($format=='html' && $color=='on')
  174. {
  175. $value = viewStatementValue($values[$m]);
  176. }
  177. else
  178. $value = $values[$m]['value'];
  179. if($value!='')
  180. {
  181. $subrow .= sprintf("%s", $parser['td'].$value.$parser['end_td']);
  182. }
  183. else
  184. $subrow .= sprintf("%s", $parser['td'].$parser['end_td']);
  185. }
  186. $subrow .=sprintf($parser['end_tr']);
  187. $row .=$subrow;
  188. }
  189. $rows .= $row;
  190. }
  191. }
  192. $rows .=sprintf($parser['end_table']);
  193. return $rows;
  194. }
  195. function get_max_num_values($all_values, $rules)
  196. {
  197. $num_values = array();
  198. if (is_array($rules))
  199. foreach($rules as $i => $value)
  200. {
  201. array_push($num_values, count(get_value_by_rule($all_values, $rules[$i]['rule_id'])));
  202. }
  203. #echo max($num_values)." ";
  204. if (count($num_values)>=1)
  205. return max($num_values);
  206. }
  207. function get_value_by_rule($all_values, $rule_id)
  208. {
  209. $values = Array();
  210. if (is_array($all_values))
  211. {foreach($all_values as $i=>$value)
  212. {
  213. if($all_values[$i]['rule_id'] == $rule_id)
  214. {
  215. array_push($values, $all_values[$i]);
  216. }
  217. }
  218. }
  219. return $values;
  220. }
  221. ?>