<?php

if (isset($_GET['id_list'])) {
    $idList = $_GET['id_list'];
    $objList = new OPF_myList($idList);
    $sql = $objList->getVar('sql');
    /**
     * Where rules
     */
    $sqlWhere = '';
    $arWrRls = $objList->getVar('arrayWhereRules');
    if (count($arWrRls)) {
        if (stripos($sql, 'WHERE') !== false) {
            $sqlWhere = ' AND (';
        } else {
            $sqlWhere = ' WHERE (1=1 AND ';
        }
        $rules = '';
        foreach ($arWrRls as $id => $rule) {
            $rules .= $rule . ' ';
        }
        $sqlWhere .= substr($rules, 3) . ')';
    }
    /**
     * Order Method
     */
    $sqlOrder = '';
    $arOrMtd = $objList->getVar('arrayOrdMethod');
    $arAlInQy = $objList->getVar('arrayAliasSetInQuery');
    if ($arOrMtd !== false) {
        foreach ($arOrMtd as $column => $method) {
 /**
  * Aplicar una regla antes definida
  * @ignore
  * @param $datForm	Datos de form
  * @param $idList	Id de la lista
  * @return string
  */
 public function MYLIST_applyRuleQuery($datForm, $idList)
 {
     // Tildes / Case Sensitive
     $kwNa = $kwCs = '';
     $someValues = false;
     $someNullValues = false;
     $sqlRule = '';
     $myList = new OPF_myList($idList);
     $engineDb = $myList->getVar('engineDb');
     if (isset($this->myDinamicListCSkW[$engineDb])) {
         $kwCs = $this->myDinamicListCSkW[$engineDb];
     }
     if (isset($this->myDinamicListAcentkW[$engineDb])) {
         $kwNa = $this->myDinamicListAcentkW[$engineDb];
     }
     $arAlInQry = $myList->getVar('arrayAliasSetInQuery');
     $numRules = $myList->getVar('numRuleQuery');
     for ($i = 1; $i <= $numRules; ++$i) {
         if (isset($datForm['field_' . $i])) {
             $val = trim($datForm['value_' . $i]);
             if (strlen($val)) {
                 $someValues = true;
                 if (in_array($datForm['relation_' . $i], array('in', 'notin'))) {
                     $nVal = '(';
                     $vals = explode(',', $val);
                     foreach ($vals as $nVals) {
                         if (!is_numeric($nVals)) {
                             $nVals = '\'' . $nVals . '\'';
                         }
                         $nVal .= $nVals . ',';
                     }
                     $val = substr($nVal, 0, -1) . ')';
                 } else {
                     if (in_array($datForm['relation_' . $i], array('like', 'notlike'))) {
                         $val = '\'' . $val . '\'';
                     } else {
                         if (!is_numeric($val)) {
                             $val = '\'' . $val . '\'';
                         }
                     }
                 }
                 $fieldQuery = '"' . $datForm['field_' . $i] . '"';
                 if (isset($arAlInQry[$datForm['field_' . $i]])) {
                     $fieldQuery = $arAlInQry[$datForm['field_' . $i]];
                 }
                 $sqlRule = $datForm['logic_' . $i] . ' ';
                 if (!is_numeric($val)) {
                     if (!$datForm['case_sensitive_' . $i]) {
                         $sqlRule .= $kwNa . '(' . $kwCs . '(' . $fieldQuery . '))';
                     } else {
                         $sqlRule .= $fieldQuery;
                     }
                 } else {
                     $sqlRule .= $fieldQuery;
                 }
                 $sqlRule .= ' ' . '' . $this->myDinamicListRel[$datForm['relation_' . $i]] . ' ';
                 if (!is_numeric($val)) {
                     if (!$datForm['case_sensitive_' . $i]) {
                         $sqlRule .= $kwNa . '(' . $kwCs . '(' . $val . '))';
                     } else {
                         $sqlRule .= $val;
                     }
                 } else {
                     $sqlRule .= $val;
                 }
                 $myList->setVar('arrayWhereRules', $sqlRule, $i);
                 $this->assign('value_' . $i, 'className', 'caja');
                 $this->assign('status_' . $idList . '_' . $i, 'className', 'rule_apply');
             } else {
                 $someNullValues = true;
                 $this->assign('value_' . $i, 'className', 'caja_required');
             }
         }
     }
     if ($someValues) {
         $myList->setVar('maxNumPage', 0);
         $myList->setVar('currentPage', 0);
         $this->assign($idList, 'innerHTML', $myList->getList());
         if ($myList->isSuccessfulProcess()) {
             if ($myList->getNumRowsAffected()) {
                 $this->notificationWindow(MSG_QUERY_FORM_OK, 3, 'ok');
             } else {
                 $this->notificationWindow(MSG_QUERY_FORM_NOROWS, 3, 'info');
             }
         } else {
             $this->notificationWindow(MSG_QUERY_FORM_BAD, 3, 'error');
             $myList->unSetVar('arrayWhereRules', $i);
         }
         $js = 'clearRowsMarked();' . "\n";
         $this->script($js);
     } else {
         $this->notificationWindow(MSG_APPLY_RULES_ALL_VALUES_NULL, 3, 'warning');
     }
     return $this->response;
 }
 /**
  * Contruye el resultado segun el tipo de archivo
  * @access private
  * @return string
  */
 private function buildResult()
 {
     $fromListExport = false;
     if (count($this->arrFieldHiden)) {
         $fromListExport = true;
     }
     $out = '';
     $widthList = 0;
     if ($this->idList) {
         $myList = new OPF_myList($this->idList);
         $this->width = $myList->getVar('arrayWidthsCols');
         $widthList = $myList->getVar('width');
         $numFldsAftd = $myList->getVar('numFldsAftd');
         $widthDefa = intval($widthList / $numFldsAftd);
     }
     switch ($this->format) {
         case 'pdf':
             $swTl = 0;
             $ori = 'P';
             if ($widthList > 900) {
                 $ori = 'L';
             }
             $this->objPDF = new PDF_MC_Table($ori);
             $this->objPDF->SetLineWidth(0.1);
             $this->objPDF->AddPage();
             $this->Header($ori);
             foreach ($this->resSql as $row) {
                 // Titles
                 if (!$swTl) {
                     $this->objPDF->SetFont('Arial', '', 10);
                     $intTitl = 1;
                     foreach ($row as $key => $val) {
                         if (in_array($intTitl, $this->arrFieldHiden) || $fromListExport == false) {
                             if (isset($this->width[$key])) {
                                 $widthCol = $this->width[$key] + 40;
                             } else {
                                 $widthCol = $widthDefa + 40;
                             }
                             $this->objPDF->Cell($widthCol / 6, 5, ucwords(strtolower(utf8_decode($key))), 1, 0, 'C', false);
                         }
                         ++$intTitl;
                     }
                     $this->objPDF->Ln();
                     $swTl = 1;
                     $this->objPDF->SetFont('Arial', '', 7);
                 }
                 $intTitl = 1;
                 $estaFila = array();
                 $estaFilaAnchos = array();
                 $estaFilaAligns = array();
                 foreach ($row as $key => $val) {
                     if (in_array($intTitl, $this->arrFieldHiden) || $fromListExport == false) {
                         if (isset($this->width[$key])) {
                             $widthCol = $this->width[$key] + 40;
                         } else {
                             $widthCol = $widthDefa + 40;
                         }
                         $align = 'L';
                         if (is_numeric($val)) {
                             $align = 'R';
                         }
                         $estaFila[] = utf8_decode($val);
                         $estaFilaAligns[] = $widthCol / 6;
                         $estaFilaAnchos[] = $widthCol / 6;
                     }
                     ++$intTitl;
                 }
                 $this->objPDF->SetAligns($estaFilaAligns);
                 $this->objPDF->SetWidths($estaFilaAnchos);
                 $this->objPDF->Row($estaFila);
             }
             $out .= $this->objPDF->Output('', 'S');
             break;
         default:
             $swTl = 0;
             if ($this->format == 'html') {
                 $out .= '<html>';
                 $out .= '<head><style type="text/css">td{font-family: Arial, Helvetica, sans-serif;font-size: 13px;}</style>';
                 $out .= '</head><body>';
                 $out .= '<table border="0" cellspacing="0" cellpadding="0"><tr><td width="30%">&nbsp;</td><td width="70%">' . REPORT_TITLE . $_SERVER['HTTP_REFERER'] . '</td></tr>';
                 $out .= '<tr><td bgcolor="#000000" colspan="2">';
                 $out .= '<table border="0" cellspacing="1" cellpadding="0" width="100%">';
             } else {
                 $out .= '<table border="1">';
             }
             $bg = '';
             if ($this->format == 'html') {
                 $bg = 'bgcolor="#FFFFFF"';
             }
             foreach ($this->resSql as $row) {
                 // Titles
                 if (!$swTl) {
                     $out .= '<tr>';
                     $intTitl = 1;
                     foreach ($row as $key => $val) {
                         if (in_array($intTitl, $this->arrFieldHiden) || $fromListExport == false) {
                             $widthCol = '';
                             if (isset($arWidth[$key])) {
                                 $widthCol = 'width="' . $arWidth[$key] . '"';
                             }
                             $out .= '<td ' . $widthCol . ' align="center" ' . $bg . '>';
                             $out .= ucwords(strtolower(utf8_decode($key)));
                             $out .= '</td>';
                         }
                         ++$intTitl;
                     }
                     $out .= '</tr>';
                     $swTl = 1;
                 }
                 $out .= '<tr>';
                 $intTitl = 1;
                 foreach ($row as $key => $val) {
                     if (in_array($intTitl, $this->arrFieldHiden) || $fromListExport == false) {
                         $align = 'left';
                         if (is_numeric($val)) {
                             $align = 'right';
                         }
                         $out .= '<td ' . $bg . ' align="' . $align . '">';
                         $out .= utf8_decode($val);
                         $out .= '</td>';
                     }
                     ++$intTitl;
                 }
                 $out .= '</tr>';
             }
             $out .= '</table>';
             if ($this->format == 'html') {
                 $out .= '</td></tr></table>';
                 $out .= '</body></html>';
             }
             break;
     }
     $this->resText = $out;
 }