简体   繁体   中英

cakephp foreach loop condition

i have this code in my admin_index view

<?php foreach ($users as $user): ?>
<tr>
    <?php if ( $user['User']['account_type']=='admin' ): ?>
    <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
    <td class="actions">
        <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
        <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
    </td>
    <?php else: ?>
    <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
    <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
    <td class="actions">
        <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
        <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
        <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?>
    </td>
</tr>
<?php endif; ?>
<?php endforeach; ?>

what i'm trying to do is get the rows to print the actions td cell differently based on the account type *ergo if the account type is 'user' it will print a delete button and if 'admin, does not allow deleting.. now thing is one admin is a designated super user by the 'super_user' boolean column and i'm trying to integrate that into the if condition where if the currently logged in admin is the designated super user his own account row will not have the delete button similar to the code above but also be able to delete other admins.. and if the currently logged in user is not a super user the above code will be shown and the other admin cannot view the super users profile

tried calling auth and session in the if statement with

this>auth/session->user('ID')

that didn't really go well

update

    <?php foreach ($users as $user): ?>
<tr>
    <?php if ( $this->Session->read('User.super_user')=== 1 ): ?>
        <?php if ( $this->Session->read('User.ID')===$user['User']['ID']): ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
            </td>
        <?php else: ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
                <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?>
            </td>
        <?php endif; ?>
    <?php else: ?>

        <?php if ($this->Session->read('User.ID')=== $user['User']['ID']): ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
            </td>
        <?php elseif ($user['User']['super_user'] ===1): ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo "no altering allowed";?>
            </td>
        <?php else: ?>
            <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
            <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
                <?php echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); ?>
            </td>
        <?php endif; ?>
<?php endif; ?>
    </tr>

my issue now is from my first layer of if statements. its automatically ignoring my condition of checking if the session's current super_user is set to 1. it always goes with the else statement... dunno what's going on

Accessing the logged user

In your controller:

$iAmsuperAdmin = (bool)$this->Auth->user('super_user');
$myId = (int)$this->Auth->user('ID');
$this->set('iAmsuperAdmin', $iAmsuperAdmin);
$this->set('myID', $myID);

In the view:

<?php foreach ($users as $user): ?>
    <?php 
    $canDelete = false;

    // admin users should be able to delete
    if ($user['User']['account_type'] == 'admin') {
        $canDelete = true;
    }

    // if I am the super-admin, I should not be able to delete myself
    if ($user['User']['account_type'] == 'admin' && $iAmSuperAdmin === true && $myID == $user['User']['ID']) {
        $canDelete = false;
    }
    ?>
    <tr>
    <?php  ?>
        <td><?php echo h($user['User']['ID']); ?>&nbsp;</td>
        <td><?php echo h($user['User']['username']); ?>&nbsp;</td>
        <td><?php echo h($user['User']['account_type']); ?>&nbsp;</td>
        <td class="actions">
            <?php echo $this->Html->link(__('View'), array('action' => 'admin_view', $user['User']['ID'])); ?>
            <?php echo $this->Html->link(__('Edit'), array('action' => 'admin_edit', $user['User']['ID'])); ?>
            <?php if ($canDelete === true) { echo $this->Form->postLink(__('Delete'), array('action' => 'admin_delete', $user['User']['ID']), array(), __('Are you sure you want to delete # %s?', $user['User']['ID'])); } ?>
        </td>
        </tr>
<?php endforeach; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM