ATK /recordlist/class.atkcustomrecordlist.inc

Language PHP Lines 247
MD5 Hash 9e13a27b12055cccdd59628bfc74e842
Repository https://github.com/ibuildingsnl/ATK.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php

  /**
   * This file is part of the Achievo ATK distribution.
   * Detailed copyright and licensing information can be found
   * in the doc/COPYRIGHT and doc/LICENSE files which should be
   * included in the distribution.
   *
   * @package atk
   * @subpackage recordlist
   *
   * @copyright (c)2000-2004 Ibuildings.nl BV
   * @license http://www.achievo.org/atk/licensing ATK Open Source License
   *
   * @version $Revision: 6323 $
   * $Id$
   */

  /** @internal include base class */
  atkimport("atk.recordlist.atkrecordlist");

  /**
   * Custom recordlist renderer.
   *
   * @author Paul Verhoef <paul@ibuildings.nl>
   * @package atk
   * @subpackage recordlist
   *
   */
  class atkCustomRecordList extends atkRecordList
  {

  	var $m_exportcsv = true;
  	protected $m_mode;

    /**
     * Creates a special Recordlist that can be used for exporting to files or to make it printable
     * @param atkNode $node       The node to use as definition for the columns.
     * @param array $recordset    The records to render
     * @param String $sol         String to use at start of each row
     * @param String $sof         String to use at start of each field
     * @param String $eof         String to use at end of each field
     * @param String $eol         String to use at end of each row
     * @param int $type           0=Render rows in simple html tabl; 1= raw export
     * @param string $compression        Compression technique (bzip / gzip)
     * @param array $suppressList List of attributes from $node that should be ignored
     * @param array $outputparams Key-Value parameters for output. Currently existing:
     *                               filename - the name of the file (without extension .csv)
     * @param String $mode	      The mode that is passed to attributes' display() method
     *                            (for overrides). Defaults to 'list'.
     * @param Boolean $titlerow   Should titlerow be rendered or not
     * @param Boolean $decode     Should data be decoded or not (for exports)
     * @param String $fsep        String to use between fields
     * @param String $rfeplace   String for replacing line feeds in recordset field values (null = do not replace)
    */
    function render(&$node, $recordset, $sol, $sof, $eof, $eol, $type="0", $compression="", $suppressList="",
                    $outputparams=array(), $mode="list", $titlerow=true, $decode=false, $fsep="", $rfeplace=null)
    {
      $this->setNode($node);
      $this->m_mode = $mode;
      // example      html         csv

      // $sol     = '<tr>'         or  ''
      // $sof     = '<td>'         or  '"'
      // $eof     = '</td>'        or  '"'
      // $eol     = '</tr>'        or  '\r\n'
      // $fsep    = ''             or  ';'
      //$empty  om lege tabelvelden op te vullen;

      // stuff for the totals row..
      $totalisable = false;
      $totals = array();
      if ($type=="0")
      {
        $empty = "&nbsp;";
      }
      if ($type=="1")
      {
        $output="";
        $empty="";
      }

      if ($titlerow)
      {
        $output .= $sol;

        // display a headerrow with titles.
        // Since we are looping the attriblist anyway, we also check if there
        // are totalisable collumns.
        foreach (array_keys($this->m_node->m_attribList) as $attribname)
        {
          $p_attrib = &$this->m_node->m_attribList[$attribname];
          $musthide=(is_array($suppressList)&&count($suppressList)>0&&in_array($attribname,$suppressList));
          if (!$this->isHidden($p_attrib) && !$musthide)
          {
            $output.=$sof.$this->eolreplace(atktext($p_attrib->fieldName(), $this->m_node->m_module, $this->m_node->m_type), $rfeplace).$eof.$fsep;

            // the totalisable check..
            if ($p_attrib->hasFlag(AF_TOTAL))
            {
              $totalisable = true;
            }
          }
        }

        $output.=$eol;
      }

      // Display the values
      for ($i = 0, $_i = count($recordset); $i < $_i; $i++)
      {
        $output.=$sol;
        foreach (array_keys($this->m_node->m_attribList) as $attribname)
        {
          $p_attrib = &$this->m_node->m_attribList[$attribname];
          $musthide=(is_array($suppressList)&&count($suppressList)>0&&in_array($attribname,$suppressList));
          
          if (!$this->isHidden($p_attrib) && !$musthide)
          {
            // An <attributename>_display function may be provided in a derived
            // class to display an attribute.
            $funcname = $p_attrib->m_name."_display";

            if (method_exists($this->m_node,$funcname))
            {
              $value=$this->eolreplace($this->m_node->$funcname($recordset[$i], $this->m_mode), $rfeplace);
            }
            else
            {
              // otherwise, the display function of the particular attribute
              // is called.
              $value = $this->eolreplace($p_attrib->display($recordset[$i], $this->m_mode), $rfeplace);
            }
            if (atkGetCharset() != "" && $decode) $value = atk_html_entity_decode(atk_htmlentities($value, ENT_NOQUOTES), ENT_NOQUOTES);
            $output.=$sof.($value==""?$empty:$value).$eof.$fsep;

            // Calculate totals..
            if ($p_attrib->hasFlag(AF_TOTAL))
            {
              $totals[$attribname] = $p_attrib->sum($totals[$attribname], $recordset[$i]);
            }
          }
        }

        $output.=$eol;
      }

      // totalrow..
      if ($totalisable)
      {
        $totalRow = $sol;

        // Third loop.. this time for the totals row.
        foreach (array_keys($this->m_node->m_attribList) as $attribname)
        {
          $p_attrib = &$this->m_node->m_attribList[$attribname];
        $musthide=(is_array($suppressList)&&count($suppressList)>0&&in_array($attribname,$suppressList));
          if (!$this->isHidden($p_attrib) && !$musthide)
          {
            if ($p_attrib->hasFlag(AF_TOTAL))
            {
              $value = $this->eolreplace($p_attrib->display($totals[$attribname], $this->m_mode), $rfeplace);
              $totalRow.=$sof.($value=="" ? $empty : $value).$eof.$fsep;

            }
            else
            {
              $totalRow.= $sof.$empty.$eof.$fsep;
            }
          }
        }

        $totalRow .= $eol;

        $output .= $totalRow;

      }

      // html requires table tags
      if ($type == "0")
      {
        $output = '<table border="1" cellspacing="0" cellpadding="2">'.$output."</table>";
      }

      atkdebug(atk_html_entity_decode($output));

      // To a File
      if (!array_key_exists("filename", $outputparams))
        $outputparams["filename"] = "achievo";

      if ($this->m_exportcsv)
      {
        $ext = ($type == "0" ? "html" : "csv");
        $exporter = &atknew("atk.utils.atkfileexport");       
        $exporter->export($output, $outputparams["filename"], $ext, $ext, $compression);       
      }
      else
      {
      	return $output;
      }
    }
    
    /**
     * Is this attribute hidden?
     *
     * @param atkAttribute $attribute
     * @return bool Boolean to indicate if attribute is hidden or not
     */
    protected function isHidden(atkAttribute $attribute)
    {
      if ($attribute->hasFlag(AF_HIDE)) return true;
      if ($attribute->hasFlag(AF_HIDE_SELECT) && $this->m_node->m_action==='select') return true;
      if ($attribute->hasFlag(AF_HIDE_LIST) && ($this->m_node->m_action==='export' || $this->m_mode==='export')) return true;
      return false;
    }

    /**
     * Set exporting csv to file
     *
     * @param bool $export
     */
    function setExportingCSVToFile($export=true)
    {
    	if (is_bool($export)) $this->m_exportcsv = $export;
    }

    /**
     * Replace any eol character(s) by something else
     *
     * @param String $string        The string to process
     * @param String $replacement   The replacement string for '\r\n', '\n' and/or '\r'
     */
    function eolreplace($string, $replacement)
    {
      if (!is_null($replacement))
      {
        $string = str_replace("\r\n", $replacement, $string); // prevent double replacement in the next lines!
        $string = str_replace("\n", $replacement, $string);
        $string = str_replace("\r", $replacement, $string);
      }

      return $string;
    }
  }

?>
Back to Top