Advertisement
Guest User

Untitled

a guest
Mar 20th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.69 KB | None | 0 0
  1. <?php
  2.  
  3. Yii::import("zii.widgets.grid.CGridView");
  4. Yii::import("application.widgets.DataColumn");
  5.  
  6. class GridView extends CGridView {
  7.  
  8.     public $navegacao = true;
  9.     public $paginacaoDefault = true;
  10.     public $url = null;
  11.     public $attributeToUpdate = null;
  12.     public $fieldToFocus = null;
  13.     public $attributeToSend = array();
  14.     protected $rowSetKeys = array();
  15.     public $rowIdPrefix = 'gr_';
  16.  
  17.     public function init() {
  18.         $this->summaryText = '';
  19.         $this->enableSorting = true;
  20.  
  21.         $this->emptyText = $this->emptyText === '' ? 'Nenhum resultado encontrado.' : $this->emptyText;
  22.  
  23.         if ($this->paginacaoDefault) {
  24.             $this->pager = array(
  25.                 'cssFile' => Yii::app()->baseUrl . '/css/gridView.css',
  26.                 'class' => 'CLinkPager',
  27.                 'maxButtonCount' => 10,
  28.                 'nextPageLabel' => yii::t('core', 'Próximo'),
  29.                 'prevPageLabel' => yii::t('core', 'Anterior'),
  30.                 'firstPageLabel' => yii::t('core', 'Primeiro'),
  31.                 'lastPageLabel' => yii::t('core', 'Último'),
  32.                 'header' => '',
  33.             );
  34.         }
  35.  
  36.         $this->cssFile = Yii::app()->baseUrl . '/css/gridView.css';
  37.  
  38.         if ($this->navegacao) {
  39.             array_push($this->columns, array(
  40.                 'class' => 'CButtonColumn',
  41.                 'viewButtonImageUrl' => Yii::app()->baseUrl . '/images/grid/ico_visualizar.png',
  42.                 'updateButtonImageUrl' => Yii::app()->baseUrl . '/images/grid/ico_editar.png',
  43.                 'deleteButtonImageUrl' => Yii::app()->baseUrl . '/images/grid/ico_excluir.png',
  44.                 'viewButtonUrl' => 'Yii::app()->getController()->createUrl("visualizar", array("id" => $data["id"]))',
  45.                 'deleteButtonUrl' => 'Yii::app()->getController()->createUrl("deletar", array("id" =>  $data["id"]))',
  46.                 'updateButtonUrl' => 'Yii::app()->getController()->createUrl("editar", array("id" =>  $data["id"]))',
  47.             ));
  48.         }
  49.  
  50.         if ($this->url != null) {
  51.             $script = 'function(id) {';
  52.  
  53.             foreach ($this->attributeToSend as $att => $value) {
  54.                 if ($att == '0') {
  55.                     $script .= 'var ' . $value . ' = $("#' . $value . '").val();';
  56.                 } else {
  57.                     $script .= 'var ' . $att . ' = $("#' . $value . '").val();';
  58.                 }
  59.             }
  60.  
  61.             $script .= '
  62.            var paramId = jQuery.fn.yiiGridView.getSelection(id);
  63.            console.log(paramId);
  64.            if(paramId.length>0) {
  65.                $.ajax({
  66.                    url: "' . $this->url . '",
  67.                    data: "id=" + paramId';
  68.             foreach ($this->attributeToSend as $att => $value) {
  69.                 if ($att == '0') {
  70.                     $script .= '+ "&' . $value . '=" + ' . $value . '';
  71.                 } else {
  72.                     $script .= '+ "&' . $att . '=' . $value . '"';
  73.                 }
  74.             }
  75.  
  76.             $script .= ', success: function(data) {
  77.                            $("#' . $this->attributeToUpdate . '").html(data);';
  78.  
  79.             if (isset($this->fieldToFocus)) {
  80.                 $script .= '
  81.                            $("#' . $this->fieldToFocus . '").focus();';
  82.             }
  83.  
  84.             $script .= '
  85.                        },
  86.                        error: function() {
  87.                            alert("Requisição Inválida!");
  88.                        }
  89.                    });
  90.                }
  91.            }';
  92.  
  93.             $this->selectionChanged = $script;
  94.         }
  95.  
  96. //        if ($this->dataProvider->getKeys()) {
  97.             $this->rowSetKeys = $this->dataProvider->getKeys();
  98. //        } else {
  99. //            throw new CException("Seu model " . $this->dataProvider->getId() . " está sem chave primária!");
  100. //        }
  101.  
  102.         parent::init();
  103.     }
  104.  
  105.     public function renderTableFooter() {
  106.         $hasFilter = $this->filter !== null && $this->filterPosition === self::FILTER_POS_FOOTER;
  107.         $hasFooter = $this->getHasFooter();
  108.         if ($hasFilter || $hasFooter) {
  109.             echo "<tfoot>\n";
  110.             if ($hasFooter) {
  111.                 $totalDeColunas = 0;
  112.  
  113.                 //verifica o total de colunas em todas as colunas
  114.                 foreach ($this->columns as $column) {
  115.                     if (isset($column->multipleFooter)) {
  116.                         if (sizeof($column->multipleFooter) > $totalDeColunas) {
  117.                             $totalDeColunas = sizeof($column->multipleFooter);
  118.                         }
  119.                     }
  120.                 }
  121.  
  122.                 if ($totalDeColunas >= 1) {
  123.                     for ($cont = 0; $cont < $totalDeColunas; $cont++) {
  124.                         echo "<tr>\n";
  125.  
  126.                         foreach ($this->columns as $column) {
  127.                             if (isset($column->multipleFooter)) {
  128.                                 echo $column->renderMultiFooterCell($cont);
  129.                             } else {
  130.                                 //printando na primeira linha
  131.                                 if ($cont == 0) {
  132.                                     $column->renderFooterCell();
  133.                                 } else {
  134.                                     echo "<td>" . $this->blankDisplay . "</td>\n";
  135.                                 }
  136.                             }
  137.                         }
  138.  
  139.                         echo "</tr>\n";
  140.                     }
  141.                 } else {
  142.                     echo "<tr>\n";
  143.                     foreach ($this->columns as $column)
  144.                         $column->renderFooterCell();
  145.                     echo "</tr>\n";
  146.                 }
  147.             }
  148.             if ($hasFilter)
  149.                 $this->renderFilter();
  150.             echo "</tfoot>\n";
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Mesma coisa do CGridView, a diferença é a $column['class']
  156.      */
  157.     protected function initColumns() {
  158.         if ($this->columns === array()) {
  159.             if ($this->dataProvider instanceof CActiveDataProvider)
  160.                 $this->columns = $this->dataProvider->model->attributeNames();
  161.             else if ($this->dataProvider instanceof IDataProvider) {
  162.                 // use the keys of the first row of data as the default columns
  163.                 $data = $this->dataProvider->getData();
  164.                 if (isset($data[0]) && is_array($data[0]))
  165.                     $this->columns = array_keys($data[0]);
  166.             }
  167.         }
  168.         $id = $this->getId();
  169.         foreach ($this->columns as $i => $column) {
  170.             if (is_string($column))
  171.                 $column = $this->createDataColumn($column);
  172.             else {
  173.                 if (!isset($column['class'])) {
  174.                     //Alteração realizada aqui!
  175.                     $column['class'] = 'application.widgets.DataColumn';
  176.                 }
  177.                 $column = Yii::createComponent($column, $this);
  178.             }
  179.             if (!$column->visible) {
  180.                 unset($this->columns[$i]);
  181.                 continue;
  182.             }
  183.             if ($column->id === null)
  184.                 $column->id = $id . '_c' . $i;
  185.             $this->columns[$i] = $column;
  186.         }
  187.  
  188.         foreach ($this->columns as $column)
  189.             $column->init();
  190.     }
  191.  
  192.     /**
  193.      * Mesma coisa do CGridView, a diferença é a DataColumn
  194.      */
  195.     protected function createDataColumn($text) {
  196.         if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $text, $matches))
  197.             throw new CException(Yii::t('zii', 'The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
  198.         $column = new DataColumn($this);
  199.         $column->name = $matches[1];
  200.         if (isset($matches[3]) && $matches[3] !== '')
  201.             $column->type = $matches[3];
  202.         if (isset($matches[5]))
  203.             $column->header = $matches[5];
  204.         return $column;
  205.     }
  206.  
  207.     /**
  208.      * Adicionando o RowId nos TR
  209.      */
  210.     public function renderTableRow($row) {
  211.         $rowId = $this->rowIdPrefix . $this->rowSetKeys[$row];
  212.  
  213.         if ($this->rowCssClassExpression !== null) {
  214.             $data = $this->dataProvider->data[$row];
  215.             echo '<tr class="' . $this->evaluateExpression($this->rowCssClassExpression, array('row' => $row, 'data' => $data)) . '" id="' . $rowId . '">';
  216.         } else if (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0)
  217.             echo '<tr class="' . $this->rowCssClass[$row % $n] . '" id="' . $rowId . '">';
  218.         else
  219.             echo '<tr id="' . $rowId . '">';
  220.         foreach ($this->columns as $column)
  221.             $column->renderDataCell($row);
  222.         echo "</tr>\n";
  223.     }
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement