/** * Construye la lista dinamica * @param $showQueryForm Mostrar formulario de consulta * @param $showFirstRule Mostrar primera regla de formulario de consulta * @return unknown_type */ private function buildList($showQueryForm, $showFirstRule) { $buf = ''; if ($this->sqlORobject) { if (is_object($this->sqlORobject)) { // Objeto $this->objConn = $this->sqlORobject; $sql = $this->sql = ''; $countFields = count($fields = get_class_vars($table = get_class($this->sqlORobject))); $subSqlF = ''; $iCounter = 1; foreach ($fields as $field => $value) { $subSqlF .= $field; $this->setRealNameInQuery($field, $field); if ($iCounter < $countFields) { $subSqlF .= ', '; } ++$iCounter; } $sql = 'SELECT ' . $subSqlF . ' FROM ' . $table; $this->sql = $sql; } else { // Cadena $this->objConn = new OPF_myActiveRecord(); $sql = $this->sql = $this->sqlORobject; } if ($this->objConn->isSuccessfulConnect()) { $this->resSql = $this->objConn->query($this->sql . '' . $this->getSqlPartOrderBy() . '' . $this->getSqlPartLimit()); } $this->engineDb = $this->objConn->getEngine(); } else { $this->restVarsSess(); $this->objConn = new OPF_myActiveRecord(); $sql = $this->sql . '' . $this->getSqlPartWhere() . '' . $this->getSqlPartOrderBy() . '' . $this->getSqlPartLimit(); if ($this->objConn->isSuccessfulConnect()) { $this->resSql = $this->objConn->query($sql); } } if ($this->objConn->getErrorLog(true)) { $this->successFul = false; } # Numero de campos afectados $this->numFldsAftd = $this->objConn->getNumFieldsAffected(); # Numero de registro afectados $this->numAffectedRows = $this->objConn->getNumRowsAffected(); /** * Calcular el ancho de cada columna si no hay definido * un ancho especifico definido antes por el usuario. */ $widByCol = 0; $totWid = $this->width; $return = $bufHead = $cadParam = ''; $buf .= '<div id="' . $this->idList . '" name="' . $this->idList . '">' . "\n"; if ($this->useSqlDebug) { $buf .= '<div style="text-align:left" class="form_cont_filter"><b>Sql: </b>' . $sql . '<br>'; $buf .= '<b>Registros: </b>' . $this->numAffectedRows . '</div><br>'; } if (!$this->successFul) { $buf .= $this->objConn->getErrorLog(true); } else { $i = 0; if ($this->numAffectedRows) { $buf .= "\n" . '<table border="0" width="' . $this->width . '' . $this->formatWidthList . '" cellspacing="0" cellpadding="0" align="center"><tr><td class="list">' . "\n"; foreach ($this->arrayWidthsCols as $col => $wid) { $totWid -= $wid; } if ($totWid) { $widByCol = $totWid / ($this->numFldsAftd - count($this->arrayWidthsCols)); } $sw = false; $buf .= "\n" . '<table border="0" width="100%" cellspacing="' . $this->borderCellSize . '" cellpadding="0" id="table_' . $this->idList . '">' . "\n"; $i = 0; $classTd = 'td_default'; $rows = $this->resSql; foreach ($rows as $row) { if ($this->useDistBetwRows) { if ($i % 2) { $classTd = 'td_default'; } else { $classTd = 'td_middle'; } } # Titulos de las columnas if (!$sw) { $this->arrayFieldsOnQuery = array(); $arrColOrd = array(); $sw = true; $bufHead .= '<tr>' . "\n" . "\t"; $arrayOrdNum = array(); $cOrd = 1; foreach ($this->arrayOrdNum as $nom) { $arrayOrdNum[$nom] = $cOrd; ++$cOrd; } $this->firsKey = true; foreach ($row as $key => $val) { if (!is_numeric($key)) { $key = $key; $this->arrayFieldsOnQuery[] = $key; $widCol = 0; if (isset($this->arrayWidthsCols[$key])) { $widCol = $this->arrayWidthsCols[$key]; } else { $widCol = $widByCol; } $orderBy = ''; $numOrder = ' '; if ($this->firsKey) { $htmlGlobal = '{htmlGlobal}'; $this->firsKey = false; } else { $htmlGlobal = ' '; } if ($this->useOrderMethod && !isset($this->arrayEventOnColumn[$key])) { $orderBy = ''; if (isset($this->arrayOrdMethod[$key])) { $orderBy = $this->arrayOrdMethod[$key]; } $styleName = 'cell_title'; if ($orderBy) { $cadParam .= '2,'; $styleName = 'cell_title_selected'; $arrColOrd[] = $key; $numOrder = $arrayOrdNum[$key]; } else { $cadParam .= '1,'; } $bufHead .= '<td class="' . $styleName . '" width="' . $widCol . '" align="center">'; $bufHead .= '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center"><tr><td width="20px">' . $htmlGlobal . '</td><td width="" style="text-align:center">'; $bufHead .= '<a class="column_title" href="javascript:;" onClick="MYLIST_moveTo(\'' . $this->idList . '\',\'' . $key . '\')">' . ucwords($key) . '</a>'; $classOrd = 'num_ord_ref'; switch ($orderBy) { case 'ASC': $classOrd = 'num_ord_ref_asc'; break; case 'DESC': $classOrd = 'num_ord_ref_desc'; break; } $bufHead .= '</td><td width="20px" class="' . $classOrd . '">' . $numOrder . '</td></tr></table>'; $bufHead .= '</td>'; } else { $cadParam .= '1,'; $bufHead .= '<td width="' . $widCol . '" align="center" class="cell_title">'; $bufHead .= '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center"><tr><td width="20px">' . $htmlGlobal . '</td><td align="center">'; $bufHead .= '<font class="column_title">' . ucwords($key) . '</font>'; $bufHead .= '</td><td width="20px"> </td></tr></table>'; $bufHead .= '</td>'; } } } $bufHead .= '</tr>' . "\n"; $bufHead = str_replace('{htmlGlobal}', $this->returnCheckBox($this->numFldsAftd, $cadParam), $bufHead); $buf .= '{bufHead}'; } $nmCheck = ''; if ($this->globalEventOnColumn) { $alsGbl = $this->globalEventOnColumn; if ($row->{$alsGbl}) { $nmCheck = $this->idList . '_' . $row->{$alsGbl}; } } $buf .= '<tr '; $buf .= 'id="tr_' . $this->idList . '_' . $i . '" '; $buf .= 'onDblClick="markRow(this, \'' . $classTd . '\',\'' . substr($cadParam, 0, -1) . '\', ' . $this->numFldsAftd . ', \'' . $nmCheck . '\')" '; $buf .= 'onmouseover="onRow(this, ' . $this->numFldsAftd . ')" '; $buf .= 'onmouseout="outRow(this, \'' . $classTd . '\',\'' . substr($cadParam, 0, -1) . '\', ' . $this->numFldsAftd . ')" '; $buf .= '>' . "\n" . "\t"; $firsVal = true; foreach ($row as $key => $val) { if (!is_numeric($key)) { if (!$val) { $Value = ' '; } $buf .= '<td class="'; if (in_array($key, $arrColOrd)) { $class = 'cell_content_selected'; } else { $class = $classTd; } $buf .= $class . '">'; if ($firsVal && $this->globalEventOnColumn) { $nmCheck = $this->idList . '_' . $row->{$alsGbl}; if ($row->{$alsGbl}) { $this->objForm->addEvent($nmCheck, 'onclick', 'check_onlist', array($nmCheck)); $buf .= '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center"><tr><td width="20px">' . $this->objForm->getCheckBox($nmCheck) . '</td><td class="' . $class . '_checkbox">'; } else { $buf .= '<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center"><tr><td width="20px"></td><td class="' . $class . '_checkbox">'; } } if (isset($this->arrayEventOnColumn[$key])) { list($event, $strMsg) = explode('::', $this->arrayEventOnColumn[$key]); if ($strMsg) { $strMsg = ' onClick="return confirm(\'' . $strMsg . '\')"'; } if ($val) { $buf .= '<a href="javascript:void(' . $event . '(\'' . $val . '\',\'' . $this->idList . '\'))"' . $strMsg . '>' . ucwords($key) . '</a>'; } } else { if (isset($this->externalMethods[$key])) { $strNameMethod = $this->externalMethods[$key]['strNameMethod']; $buf .= $this->externalMethods[$key]['objClass']->{$strNameMethod}($val) . ' '; } else { $buf .= $val . ' '; } } if ($firsVal && $this->globalEventOnColumn) { $buf .= '</td><td width="20px"> </td></tr></table>'; $firsVal = false; } $buf .= '</td>'; } } $buf .= "\n" . '</tr>' . "\n"; ++$i; } $buf .= '</tbody>' . "\n"; $buf .= '</table>' . "\n"; $buf .= '</td></tr></table>' . "\n"; } else { //TODO: No hay registros afectados } } $buf .= '<div style="width:' . $this->width . '' . $this->formatWidthList . '" id="pag_' . $this->idList . '" name="pag_' . $this->idList . '">' . "\n"; $buf .= '<table cellspacing="0" cellpadding="0" border="0" align="center" width="100%"><tr><td align="left" width="33%">'; if ($this->globalEventOnColumn && $this->successFul) { $idSelect = 'global_event_' . $this->idList; $this->objForm->name = $this->idList; $this->objForm->selectStringFirstLabelOption = LABEL_FIRST_OPT_SELECT_GLOBAL_ACTION; $this->objForm->styleTypeHelp = 2; $this->objForm->addEvent($idSelect, 'onchange', 'execGlobalEventOnList', $this->idList, $idSelect); $this->objForm->addHelp($idSelect, LABEL_HELP_SELECT_GLOBAL_ACTION); $buf .= $this->objForm->getSelect($idSelect, array_flip($this->globalEventsName)); } else { $buf .= ' '; } $buf .= '</td><td width="34%" align="center">'; # Usar paginacion if ($this->usePagination && $this->successFul) { $arrBut = array('_ini_page' => array(' -- ', 'beg', 'button'), '_back_page' => array(' -1 ', 'bac', 'button'), '_goto_page' => array($this->currentPage + 1, 'goto', 'field'), '_chg_pag' => array(array(1 => $this->recordsPerPage * 1, 2 => $this->recordsPerPage * 2, 3 => $this->recordsPerPage * 3, 4 => $this->recordsPerPage * 4, 5 => $this->recordsPerPage * 5), 'amp_pag', 'select'), '_next_page' => array(' +1 ', 'nex', 'button'), '_end_page' => array(' ++ ', 'end', 'button')); $objMyForm = new OPF_myForm(); $objMyForm->selectUseFirstValue = false; $objMyForm->setParamTypeOnEvent('field'); $objMyForm->styleTypeHelp = 2; $objMyForm->addHelp($this->idList . '_end_page', ' ' . GOTO_LAST_PAGE . ' '); $objMyForm->addHelp($this->idList . '_ini_page', ' ' . GOTO_FIRST_PAGE . ' '); $objMyForm->addHelp($this->idList . '_next_page', ' ' . GOTO_NEXT_PAGE . ' '); $objMyForm->addHelp($this->idList . '_back_page', ' ' . GOTO_BACK_PAGE . ' '); $buf .= '<table cellspacing="0" cellpadding="0" border="0" align="center" width="250"><tr>'; if ($this->currentPage == 0) { $objMyForm->addDisabled($this->idList . '_ini_page'); $objMyForm->addDisabled($this->idList . '_back_page'); } if ($i < $this->recordsPerPage * $this->recordsPerPageForm) { $objMyForm->addDisabled($this->idList . '_next_page'); $objMyForm->addDisabled($this->idList . '_end_page'); } if ($this->currentPage == $this->maxNumPage) { $objMyForm->addDisabled($this->idList . '_end_page'); } foreach ($arrBut as $id => $attr) { $buf .= '<td>'; switch ($attr[2]) { case 'button': $objMyForm->addEvent($this->idList . $id, 'onclick', 'MYLIST_page', $this->idList, $attr[1]); $buf .= $objMyForm->getButton($this->idList . $id, '', '' . '../mylist/' . $id . '.gif'); break; case 'field': $objMyForm->addHelp($this->idList . $id, ' ' . LABEL_HELP_PAGACT_FORM . ' <b>' . $attr[0] . '</b>' . ' '); $buf .= $objMyForm->getText($this->idList . $id, $attr[0], 1, NULL, true); break; case 'select': $objMyForm->addHelp($this->idList . $id, ' ' . LABEL_HELP_CHPAG_SELECT_FORM . ' '); $objMyForm->addEvent($this->idList . $id, 'onchange', 'MYLIST_chPag', $this->idList); $buf .= $objMyForm->getSelect($this->idList . $id, $attr[0], $this->recordsPerPageForm); break; } $buf .= '</td>'; } $buf .= '</tr></table>'; } $buf .= '</td><td width="33%" align="right" class="texto_formularios"> </td></tr></table></div>' . "\n"; $buf .= '' . '</div>' . "\n"; $this->bufHtml = str_replace('{bufHead}', $bufHead, $buf); if ($showQueryForm && $this->successFul) { $this->bufHtml = $this->buildQueryForm($showFirstRule) . $this->bufHtml; } # Registramos las variables que se han usado $this->regAttClass(get_class_vars(get_class($this))); }
/** * Exportar datos desde una consulta. * * Contructor de la clase que incia tambien el proceso para la generacion de la consulta sql a exportar. * *<code> * *Ejemplo 1: Salida a un archivo físico: * *<?php * * $myExport = new OPF_myExportData('SELECT fiel1, field2, field3 FROM table','pdf','/var/www/html/project/report/report.pdf'); * * # Cuando generamos un archivo en formato PDF es prescindible ajustar el tamaño de las columnas pues no es posible calcular su valor automáticamente. * * $myExport->setWidth('fiel1',100); * * $myExport->setWidth('field2',150); * * $myExport->setWidth('field3',200); * * $myExport->getResult(); * *?> * *Ejemplo 2: Salida a un archivo descargable: * *<?php * * $myExport = new OPF_myExportData('SELECT * FROM table','xls'); * * $xls = $myExport->getResult(); * * header ('Content-type: application/x-msexcel'); * * header ('Content-Disposition: attachment; filename="file.xls"'); * * header ('Content-Length: '.strlen($xls)); * * if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")){ * * header('Pragma: private'); * * header('Cache-control: private, must-revalidate'); * } * * echo $xls; * *?> * *</code> * @param string $sql Consulta SQL * @param string $format Formato de archivo (pdf,xls,html) * @param string $filePath Guardar en archivo fisico * @param string $id_list Nombre de lista dinamica * @return boolean */ public function __construct($sql, $format = 'html', $filePath = '', $idList = '') { $numArg = func_num_args(); if ($numArg == 5) { $arrFieldHiden = func_get_arg(4); $this->arrFieldHiden = explode(',', $arrFieldHiden); } $success = true; $this->format = $format; $this->filePath = $filePath; if (in_array($format, $this->arValidFormat)) { $this->myAct = new OPF_myActiveRecord(); $this->resSql = $this->myAct->query($sql); $this->error = $this->myAct->getErrorLog(false); } else { $success = false; $this->error = MYEXPORT_ERROR_INVALID_FORMAT; } if ($idList) { $this->idList = $idList; } return $success; }