0
\$\begingroup\$

Is this correct? Or am I overloading the responsibilities of the view?

<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>Estado</th>
                <th>Prioridad</th>
                <th>Responsable</th>
                <th>Categoría</th>
                <th>Solicitud</th>
                <th>Creado</th>
                <th>Cerrado</th>
            </tr>
    </thead>
        <tbody>
            <?php 
                 $class = '';
                 $label = '';
                 foreach ($solicitud as $key => $value) {
                     if( $value['priority'] > 0 && $value['priority'] < 4 ) $class = 'badge-important';
                     if( $value['priority'] > 4 && $value['priority'] < 7 ) $class = 'badge-info';
                     if( $value['priority'] > 7 ) $class = 'badge-warning';

                     if( $value['id'] == 1 ) $label = 'label-info';
                     if( $value['id'] == 2 ) $label = 'label-success';
                     if( $value['id'] == 3 ) $label = 'label-danger';

                    echo '<tr>';
                    echo '  <td><span class="label label-sm '.$label.'">'.ucfirst($value['status']).'</span></td>';
                    echo '   <td><span class="badge '.$class.' ">'.$value['priority'].'</span></td>';
                    echo '   <td>'.$value['sname'].' '.$value['last_name'].'</td>';
                    echo '   <td>'.$value['name'].'</td>';
                    echo '   <td>'.$value['message'].'</td>';
                    echo '   <td>'.$value['created'].'</td>';
                    echo '<td>'.(( $value['closed'] )  ? $value['closed'] : 'pendiente').'</td>';
                    echo '</tr>';

                 } ?>
    </tbody>
</table>
\$\endgroup\$
1
\$\begingroup\$

These 0, 4 and 7 means nothing to any code reader, nor 1, 2 and 3 for the label.

Move this logic in the controller and consider using ENUMs in your controller class i.e.:

const BADGE_IMPORTANT = 0;
const BADGE_INFO = 4;
const BADGE_WARNING = 7;

This way it will be more readable for the one who will read and change it later, also it will became less hard changable and more reusable.

Also, try to not echo HTML, I mean, use PHP blocks only when you have PHP in it. HTML should be outside:

<?php foreach ($solicitud as $key => $value): ?>
          <tr>
              <td><span class="label label-sm<?=$label;?>"><?=ucfirst($value['status']);?></span></td>
              <td><span class="badge <?=$class;?>"><?=$value['priority'];?></span></td>
              <td><?=$value['sname'].$value['last_name'];?></td>
              <td><?=$value['name'];?></td>
               ....
          </tr>
<?php endforeach; ?>
\$\endgroup\$
0
\$\begingroup\$

In my opinion, there is way too much logic in your view.

Personally I would manipulate the data in the controller so that $value has a label and a class attribute, and then just loop over the data in the view.

This way view and controller are decoupled and, if you think in terms of testability, you can easily unit-test the controller so that the data always has the required fields.

(Actually, I would do the data manipulation in a model.)

Hope this helps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.