Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Method.Report.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /*
8  * @begin-method-ignore
9  * this part will be deleted when construct document class until end-method-ignore
10 */
11 /**
12  * document to present a report on one family document
13  *
14  * @author Anakeen 2000
15  * @version $Id: Method.Report.php,v 1.20 2008/11/20 09:34:20 eric Exp $
16  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
17  * @package FDL
18  * @subpackage GED
19  *
20  */
21 class _REPORT extends _DSEARCH
22 {
23  /*
24  * @end-method-ignore
25  */
26  var $defaultedit = "FREEDOM:EDITREPORT";
27  var $defaultview = "FREEDOM:VIEWREPORT";
28 
29  var $cviews = array(
30  "FREEDOM:VIEWREPORT",
31  "FREEDOM:VIEWMINIREPORT:T"
32  );
33  var $eviews = array(
34  "FREEDOM:EDITREPORT"
35  );
36  /**
37  * public because use in RSS
38  *
39  */
40  public function _getInternals()
41  {
42  return array(
43  "title" => _("doctitle") ,
44  "revdate" => _("revdate") ,
45  "revision" => _("revision") ,
46  "state" => _("state") ,
47  "owner" => _("owner")
48  );
49  }
50  /**
51  * Compute the values fo the edit display
52  *
53  */
54  public function editreport()
55  {
56  global $action;
57  $action->parent->AddJsRef($action->GetParam("CORE_JSURL") . "/selectbox.js");
58  $action->parent->AddJsRef($action->GetParam("CORE_PUBURL") . "/FREEDOM/Layout/editreport.js");
59 
60  $rfamid = GetHttpVars("sfamid", $this->getValue("SE_FAMID", 1));
61  $rdoc = createDoc($this->dbaccess, $rfamid, false);
62  $lattr = $rdoc->GetNormalAttributes();
63  $tcolumn1 = array();
64  $tcolumn2 = array();
65  while (list($k, $v) = each($lattr)) {
66  // if ($v->visibility=="H") continue;
67  $tcolumn1[$v->id] = array(
68  "aid" => $v->id,
69  "alabel" => $v->getLabel()
70  );
71  }
72  $tinternals = $this->_getInternals();
73  // reconstruct internals for layout
74  reset($tinternals);
75  while (list($k, $v) = each($tinternals)) {
76  $tinternals[$k] = array(
77  "aid" => $k,
78  "alabel" => $v
79  );
80  }
81  // display selected column
82  $tcols = $this->getTValue("REP_IDCOLS");
83 
84  foreach ($tcols as $k => $v) {
85  if (isset($tcolumn1[$v])) {
86  $tcolumn2[$v] = $tcolumn1[$v];
87  unset($tcolumn1[$v]);
88  }
89  if (isset($tinternals[$v])) {
90  $tcolumn2[$v] = $tinternals[$v];
91  unset($tinternals[$v]);
92  }
93  }
94 
95  $this->lay->setBlockData("COLUMN1", $tcolumn1);
96  $this->lay->setBlockData("INTERNALS", $tinternals);
97 
98  $this->lay->setBlockData("COLUMN2", $tcolumn2);
99  }
100  /**
101  * Compute the values for the view display
102  *
103  * @param string $target window target name for hyperlink destination
104  * @param bool $ulink if false hyperlink are not generated
105  * @param bool $abstract if true only abstract attribute are generated
106  */
107  public function viewreport($target = "_self", $ulink = true, $abstract = false)
108  {
109  global $action;
110  $this->viewattr($target, $ulink, $abstract);
111  $this->viewprop($target, $ulink, $abstract);
112  $action->parent->AddCssRef("FREEDOM:viewreport.css", true);
113  $action->parent->AddJsRef($action->GetParam("CORE_PUBURL") . "/FREEDOM/Layout/sorttable.js");
114  // --------------------------
115  // display headers column
116  $rfamid = $this->getValue("SE_FAMID", 1);
117  $rdoc = createDoc($this->dbaccess, $rfamid, false);
118  $lattr = $rdoc->GetNormalAttributes();
119  $tcolumn1 = array();
120  $tcolumn2 = array();
121  while (list($k, $v) = each($lattr)) {
122  // if ($v->visibility=="H") continue;
123  $tcolumn1[$v->id] = array(
124  "colid" => $v->id,
125  "collabel" => $v->getLabel() ,
126  "rightfornumber" => ($v->type == "money") ? "right" : "left"
127  );
128  }
129 
130  $tinternals = $this->_getInternals();
131  foreach ($tinternals as $k => $v) {
132  $tcolumn1[$k] = array(
133  "colid" => $k,
134  "collabel" => $v,
135  "rightfornumber" => "left"
136  );
137  }
138 
139  $tcols = $this->getTValue("REP_IDCOLS");
140  foreach ($tcols as $k => & $vcol) {
141  if ($vcol) {
142  $tcolumn2[$vcol] = $tcolumn1[$vcol];
143  }
144  }
145  $this->lay->setBlockData("COLS", $tcolumn2);
146  $this->lay->set("HASCOLS", count($tcolumn2) > 0);
147  include_once ("FDL/Lib.Dir.php");
148 
149  $this->lay->set("reportstyle", $this->getValue("REP_STYLE", "perso"));
150  $this->lay->set("isperso", ($this->getValue("REP_STYLE", "perso") == "perso"));
151  if ($this->isParameterizable() && $ulink) {
152  $this->lay->setBlockData("PARAMS", array(
153  array(
154  "zou"
155  )
156  ));
157  }
158  // --------------------------
159  // display body
160  $limit = $this->getValue("REP_LIMIT", "ALL");
161  $order = $this->getValue("REP_IDSORT", "title");
162  $oa = $rdoc->getAttribute($order);
163  if ($oa) {
164  if (($oa->type == "docid") && ($oa->getOption("doctitle") != "")) {
165  $order = $oa->getOption("doctitle");
166  if ($order == 'auto') $order = $oa->id . '_title';
167  }
168  }
169  $order.= " " . $this->getValue("REP_ORDERSORT");
170  $tdoc = getChildDoc($this->dbaccess, $this->initid, 0, $limit, array() , $this->userid, "TABLE", $rfamid, false, $order);
171  $trodd = false;
172  $tcolor = $this->getTValue("REP_COLORS");
173  $trow = array();
174  while (list($k, $v) = each($tdoc)) {
175  $rdoc->Affect($v);
176  $trow[$k] = array(
177  "CELLS" => "row$k",
178  "docid" => $rdoc->id,
179  "troddoreven" => $trodd ? "trodd" : "treven"
180  );
181  $trodd = !$trodd;
182  $tdodd = false;
183  $tcell = array();
184  reset($tcolumn2);
185  reset($tcolor);
186 
187  foreach ($tcolumn2 as $kc => $vc) {
188  if ($v[$kc] == "") $tcell[$kc] = array(
189  "cellval" => ""
190  );
191  else {
192  switch ($kc) {
193  case "revdate":
194  $cval = strftime("%d/%m/%Y %T", $v[$kc]);
195  // $cval = strftime ("%x %T",$v[$kc]);
196  break;
197 
198  case "state":
199  $cval = _($v[$kc]);
200  break;
201 
202  case "title":
203  if ($ulink) {
204  $trid = $v["id"];
205  $v[$kc] = $rdoc->getHTMLTitle();
206  $trlink = getparam("CORE_STANDURL") . "&app=FDL&action=FDL_CARD&id=$trid";
207  $cval = "<A target=\"rdoc" . $v["id"] . "\" onmousedown=\"document.noselect=true;\" ";
208  $cval.= "onclick=\"subwindowm(200,600,'rdoc$trid','$trlink')\" oncontextmenu=\"popdoc(event,'$trlink');return false;\">";
209  $cval.= $v[$kc] . "</a>";
210  } else {
211  $cval = $v[$kc];
212  }
213  break;
214 
215  default:
216  $cval = $rdoc->getHtmlValue($lattr[$kc], $v[$kc], $target, $ulink);
217  if ($lattr[$kc]->type == "image") $cval = "<img width=\"40px\" src=\"$cval\">";
218  }
219  $tcell[$kc] = array(
220  "cellval" => $cval,
221  "rawval" => $v[$kc]
222  );
223  }
224  $tcell[$kc]["bgcell"] = current($tcolor);
225  next($tcolor);
226  $tcell[$kc]["tdoddoreven"] = $tdodd ? "tdodd" : "tdeven";
227  $tcell[$kc]["rightfornumber"] = ($lattr[$kc]->type == "money") ? "right" : "left";
228  $tdodd = !$tdodd;
229  }
230  $this->lay->setBlockData("row$k", $tcell);
231  }
232  $this->lay->setBlockData("ROWS", $trow);
233  // ---------------------
234  // footer
235  $tfoots = $this->getTValue("REP_FOOTS");
236 
237  foreach ($tfoots as $k => $v) {
238  switch ($v) {
239  case "CARD":
240  $val = count($trow);
241  break;
242 
243  case "MOY":
244  case "SUM":
245  reset($trow);
246  $val = 0;
247  foreach ($trow as $kr => $vr) {
248  $ctr = $this->lay->getBlockData($vr["CELLS"]);
249  $val+= $ctr[$tcols[$k]]["rawval"];
250  }
251  if ($v == "MOY") $val = $val / count($trow);
252  $val = $rdoc->getHtmlValue($lattr[$tcols[$k]], $val, $target, $ulink);
253  break;
254 
255  default:
256  $val = "-";
257  }
258  $tlfoots[] = array(
259  "footval" => $val,
260  "rightfornumber" => $tcolumn2[$tcols[$k]]["rightfornumber"]
261  );
262  }
263  $this->lay->setBlockData("TFOOT", $tlfoots);
264  $this->lay->set("TITLE", $this->getHTMLTitle());
265  }
266  /**
267  * Generate data struct to csv export of a report
268  *
269  * @param boolean $refresh true to refresh the doc before export
270  * @param boolean $isPivotExport if is pivot true
271  * @param string $pivotElement id of the pivot element
272  *
273  * @return array
274  */
275  public function generateCSVReportStruct($isPivotExport = false, $pivotId = "id", $separator = ".", $dateFormat = "US", $refresh = true)
276  {
277  require_once 'WHAT/Class.twoDimensionalArray.php';
278  require_once 'FDL/Class.SearchDoc.php';
279 
280  $famId = $this->getValue("se_famid", 1);
281  $limit = $this->getValue("rep_limit", "ALL");
282  $order = $this->getValue("rep_idsort", "title");
283 
284  $search = new SearchDoc($this->dbaccess, $famId);
285  $search->dirid = $this->initid;
286  $search->slice = $limit;
287  $search->orderby = $order;
288  $search->setObjectReturn();
289  $search->search();
290 
291  $famDoc = createDoc($this->dbaccess, $famId, false);
292  $tcols = $this->getTValue("rep_idcols");
293 
294  if ($isPivotExport) {
295  return $this->generatePivotCSV($search, $tcols, $famDoc, $pivotId, $refresh, $separator, $dateFormat);
296  } else {
297  return $this->generateBasicCSV($search, $tcols, $famDoc, $refresh, $separator, $dateFormat);
298  }
299  }
300 
301  protected function generatePivotCSV(SearchDoc $search, Array $columns, Doc $famDoc, $pivotId, $refresh, $separator, $dateFormat)
302  {
303  $convertFormat = array(
304  "dateFormat" => $dateFormat,
305  'decimalSeparator' => $separator
306  );
307 
308  $pivotColumnName = uniqid();
309 
310  $singleAttributes = array();
311  $multipleAttributes = array();
312  $resultSingleArray = array();
313  $resultMultipleArray = array();
314 
315  $internals = $this->_getInternals();
316  //Generate column organisation
317  $resultSingleArray[$pivotColumnName] = array();
318 
319  foreach ($columns as $currentColumnID) {
320  $attributeObject = $famDoc->getAttribute($currentColumnID);
321  if (!$attributeObject) {
322  $singleAttributes[] = $currentColumnID;
323  $resultSingleArray[$currentColumnID] = array();
324  } elseif ($attributeObject->isMultiple()) {
325  if ($attributeObject->getOption('multiple') == "yes" && !$attributeObject->inArray()) {
326  $multipleAttributes[$currentColumnID] = array();
327  $multipleAttributes[$currentColumnID][] = $currentColumnID;
328  $resultMultipleArray[$currentColumnID] = array();
329  $resultMultipleArray[$currentColumnID][$pivotColumnName] = array();
330  $resultMultipleArray[$currentColumnID][$currentColumnID] = array();
331  } else {
332  $arrayID = $attributeObject->fieldSet->id;
333  if (!isset($multipleAttributes[$arrayID])) {
334  $multipleAttributes[$arrayID] = array();
335  $resultMultipleArray[$arrayID] = array();
336  $resultMultipleArray[$arrayID][$pivotColumnName] = array();
337  }
338  $multipleAttributes[$arrayID][] = $currentColumnID;
339  $resultMultipleArray[$arrayID][$currentColumnID] = array();
340  }
341  } else {
342  $singleAttributes[] = $currentColumnID;
343  $resultSingleArray[$currentColumnID] = array();
344  }
345  }
346  //Get Value
347  while ($currentDoc = $search->nextDoc()) {
348  if ($refresh) {
349  $currentDoc->refresh();
350  }
351  $pivotAttribute = $famDoc->getAttribute($pivotId);
352  $pivotValue = $pivotAttribute ? $pivotAttribute->getTextualValue($currentDoc, -1, $convertFormat) : $this->convertInternalElement($pivotId, $currentDoc);
353  $resultSingleArray[$pivotColumnName][] = $pivotValue;
354  foreach ($singleAttributes as $currentColumnID) {
355  $currentAttribute = $famDoc->getAttribute($currentColumnID);
356  $resultSingleArray[$currentColumnID][] = $currentAttribute ? $currentAttribute->getTextualValue($currentDoc, -1, $convertFormat) : $this->convertInternalElement($currentColumnID, $currentDoc);
357  }
358  foreach ($multipleAttributes as $currentKey => $currentArrayID) {
359  foreach ($currentArrayID as $currentColumnID) {
360  $currentAttribute = $famDoc->getAttribute($currentColumnID);
361  $nbElement = count($currentDoc->getTValue($currentColumnID));
362  for ($i = 0; $i < $nbElement; $i++) {
363  $resultMultipleArray[$currentKey][$currentColumnID][] = $currentAttribute->getTextualValue($currentDoc, $i, $convertFormat);
364  }
365  }
366  for ($i = 0; $i < $nbElement; $i++) {
367  $resultMultipleArray[$currentKey][$pivotColumnName][] = $pivotValue;
368  }
369  }
370  }
371  //Generate result array
372  $firstRow = array();
373  $twoDimStruct = new TwoDimensionStruct();
374  //Generate first line
375  $firstRow[] = _("REPORT_pivot");
376  $twoDimStruct->addColumn($resultSingleArray[$pivotColumnName]);
377 
378  foreach ($singleAttributes as $currentColumnID) {
379  $currentAttribute = $famDoc->getAttribute($currentColumnID);
380  $firstRow[] = $currentAttribute ? $currentAttribute->getLabel() : $internals[$currentColumnID];
381  $twoDimStruct->addColumn($resultSingleArray[$currentColumnID]);
382  }
383  //Generate content
384  foreach ($multipleAttributes as $currentKey => $currentArrayID) {
385  $firstRow[] = "";
386  $emptyArray = array(
387  ""
388  );
389  $twoDimStruct->addColumn($emptyArray);
390  $firstRow[] = _("REPORT_pivot");
391  $twoDimStruct->addColumn($resultMultipleArray[$currentKey][$pivotColumnName]);
392  foreach ($currentArrayID as $currentColumnID) {
393  $currentAttribute = $famDoc->getAttribute($currentColumnID);
394  $firstRow[] = $currentAttribute ? $currentAttribute->getLabel() : $internals[$currentColumnID];
395  $twoDimStruct->addColumn($resultMultipleArray[$currentKey][$currentColumnID]);
396  }
397  }
398 
399  if ($twoDimStruct->insertRow(0, $firstRow, true) == null) {
400  var_export($twoDimStruct->getLastErrorMessage());
401  }
402 
403  return $twoDimStruct->getArray();
404  }
405  /**
406  * Generate a basic CSV export
407  *
408  * @param SearchDoc $search the result of the report
409  * @param array $columns an array of id
410  * @param Doc $famDoc the associated family doc
411  *
412  * @return array
413  */
414  protected function generateBasicCSV(SearchDoc $search, Array $columns, Doc $famDoc, $refresh, $separator, $dateFormat)
415  {
416  $convertFormat = array(
417  "dateFormat" => $dateFormat,
418  'decimalSeparator' => $separator
419  );
420 
421  $twoDimStruct = new TwoDimensionStruct();
422  $firstRow = array();
423  $internals = $this->_getInternals();
424  foreach ($columns as $currentColumn) {
425  $currentAttribute = $famDoc->getAttribute($currentColumn);
426  $firstRow[] = $currentAttribute ? $currentAttribute->getLabel() : $internals[$currentColumn];
427  }
428  $twoDimStruct->addRow($firstRow);
429  while ($currentDoc = $search->nextDoc()) {
430  if ($refresh) {
431  $currentDoc->refresh();
432  }
433  $currentRow = array();
434  foreach ($columns as $currentColumn) {
435  $currentAttribute = $famDoc->getAttribute($currentColumn);
436  $currentRow[] = $currentAttribute ? $currentAttribute->getTextualValue($currentDoc, -1, $convertFormat) : $this->convertInternalElement($currentColumn, $currentDoc);
437  }
438  $twoDimStruct->addRow($currentRow);
439  }
440  return $twoDimStruct->getArray();
441  }
442 
443  protected function convertInternalElement($internalName, Doc $doc)
444  {
445  switch ($internalName) {
446  case "revdate":
447  return strftime("%x %T", $doc->getValue($internalName));
448  case "state":
449  $stateValue = $doc->getstate();
450  if (empty($stateValue)) {
451  return "";
452  }
453  return _($stateValue);
454  case "title":
455  return $doc->getHTMLTitle();
456  case "id":
457  return $doc->id;
458  case "owner":
459  return $doc->owner;
460  default:
461  return $doc->getValue($internalName);
462  }
463  }
464  /**
465  * Compute the values for the mini view display (portal)
466  *
467  * @param string $target window target name for hyperlink destination
468  * @param bool $ulink if false hyperlink are not generated
469  * @param bool $abstract if true only abstract attribute are generated
470  */
471  public function viewminireport($target = "_self", $ulink = true, $abstract = false)
472  {
473  return $this->viewreport($target, $ulink, $abstract);
474  }
475  /**
476  * @begin-method-ignore
477  * this part will be deleted when construct document class until end-method-ignore
478  */
479 }
480 /*
481  * @end-method-ignore
482 */
483 ?>
← centre documentaire © anakeen - published under CC License - Dynacase