/** * Creates a special Recordlist that can be used for exporting to files or to make it printable. * * @param Node $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 bool $titlerow Should titlerow be rendered or not * @param bool $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) * * @return string|null */ public function render($node, $recordset, $sol, $sof, $eof, $eol, $type = 0, $compression = '', $suppressList = '', $outputparams = [], $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.. $output = ''; $empty = ''; if ($type == '0') { $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($p_attrib->label(), $rfeplace) . $eof . $fsep; } } if ($fsep) { // remove separator at the end of line $output = substr($output, 0, -strlen($fsep)); } $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 (Tools::atkGetCharset() != '' && $decode) { $value = Tools::atk_html_entity_decode(htmlentities($value, ENT_NOQUOTES), ENT_NOQUOTES); } $output .= $sof . ($value == '' ? $empty : $value) . $eof . $fsep; } } if ($fsep) { // remove separator at the end of line $output = substr($output, 0, -strlen($fsep)); } $output .= $eol; } // html requires table tags if ($type == '0') { $output = '<table border="1" cellspacing="0" cellpadding="2">' . $output . '</table>'; } Tools::atkdebug(Tools::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 = new FileExport(); $exporter->export($output, $outputparams['filename'], $ext, $ext, $compression); } else { return $output; } return; }
/** * Get the validated records. * * @param string $file The import csv file * * @return array with importerrors and validatedrecs */ public function getValidatedRecords($file) { $enclosure = $this->m_postvars['enclosure']; $delimiter = $this->m_postvars['delimiter']; $columncount = $this->m_postvars['columncount']; $skipfirstrow = $this->m_postvars['skipfirstrow']; $allFields = $this->m_postvars['allFields']; $col_map = $this->m_postvars['col_map']; $allFieldsValues = $this->getAllFieldsValues($col_map); $initial_values = $this->m_importNode->initial_values(); $validatedrecs = []; $validatedrecs['add'] = []; $validatedrecs['update'] = []; $importerrors = []; $importerrors[0] = []; $importerrors[0] = array_merge($importerrors[0], $this->checkImport($col_map, $initial_values)); $allfielderror = $this->checkAllFields($allFields, $allFieldsValues); if ($allfielderror) { $importerrors[0][] = $allfielderror; } if (count($importerrors[0]) > 0) { // don't start importing if even the minimum requirements haven't been met return array('importerrors' => &$importerrors, 'validatedrecs' => array()); } static $mb_converting_exists = null; if (!isset($mb_converting_exists)) { $mb_converting_exists = function_exists('mb_convert_encoding'); Tools::atkdebug('Checking function_exists("mb_convert_encoding")'); } static $atkCharset = null; if (!isset($atkCharset)) { $atkCharset = Tools::atkGetCharset(); Tools::atkdebug('setting atkcharset static!'); } //copy the csv in a record and add it to the db $fp = fopen($file, 'r'); if ($skipfirstrow == '1') { $line = fgets($fp); } for ($line = fgets($fp), $counter = 1; $line !== false; $line = fgets($fp), $counter++) { Tools::atkdebug("Validating record nr. {$counter}"); //if we have an empty line, pass it if (trim($line) == '') { continue; } //large import are a problem for the maximum execution time, so we want to set for each //loop of the for-loop an maximum execution time set_time_limit(60); Tools::atkdebug('set_time_limit(60)'); if ($atkCharset != '' && $mb_converting_exists) { $line = mb_convert_encoding($line, $atkCharset); } $data = $this->fgetcsvfromline($line, $columncount, $delimiter, $enclosure); $rec = $initial_values; for ($i = 0, $_i = count($col_map); $i < $_i; ++$i) { if ($col_map[$i] != '-') { if (!in_array($col_map[$i], $allFields)) { // column is mapped $value = $this->_getAttributeValue($col_map[$i], $allFields, $data[$i], $importerrors, $counter, $rec); } else { //this is the allField $value = $allFieldsValues[$col_map[$i]]; } $this->addToRecord($rec, $col_map[$i], $value); } } $this->validateRecord($rec, $validatedrecs, $importerrors, $counter); } // close file @fclose($fp); return array('importerrors' => &$importerrors, 'validatedrecs' => &$validatedrecs); }
/** * Re-render / refresh the attribute with the given name. * * @param string $name attribute name */ public function refreshAttribute($name) { if ($this->isInitial()) { return; } $offset = count($this->getNode()->getPage()->getLoadScripts()); $error = []; $editArray = array('fields' => array()); $this->m_node->getAttribute($name)->addToEditArray($this->getMode(), $editArray, $this->getRecord(), $error, $this->getFieldPrefix()); $scriptCode = ''; foreach ($editArray['fields'] as $field) { $element = str_replace('.', '_', $this->getNode()->atkNodeUri() . '_' . $field['id']); $value = Json::encode(Tools::atk_iconv(Tools::atkGetCharset(), 'UTF-8', $field['html'])); // Json::encode excepts string in UTF-8 $scriptCode .= "if (\$('{$element}')) { \$('{$element}').update({$value}); } "; } $this->getNode()->getPage()->register_loadscript($scriptCode, $offset); }
/** * Partial method to refresh the add/edit field for this attribute. * * @param string $mode add/edit mode * * @return string HTML the output needed to refresh the attribute. */ public function partial_refresh($mode) { $record = $this->m_ownerInstance->updateRecord(); $fieldprefix = $this->m_ownerInstance->m_postvars['atkfp']; $arr = array('fields' => array()); $defaults =& $record; $error = []; $this->addToEditArray($mode, $arr, $defaults, $error, $fieldprefix); $script = ''; foreach ($arr['fields'] as $field) { //Json::encode expect string in in ASCII or UTF-8 format, so convert data to UTF-8 $value = Tools::atk_iconv(Tools::atkGetCharset(), 'UTF-8', $field['html']); $script .= "\$('" . str_replace('.', '_', $this->m_ownerInstance->atkNodeUri() . '_' . $field['id']) . "').update(" . Json::encode($value) . ");\r\n"; } return '<script type="text/javascript">' . $script . '</script>'; }