<?php
/*
 * COmanage Match Action Menu
 *
 * Portions licensed to the University Corporation for Advanced Internet
 * Development, Inc. ("UCAID") under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @link          http://www.internet2.edu/comanage COmanage Project
 * @package       match
 * @since         COmanage Match v1.0.0
 * @license       Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 */

$actionsCount = count($vv_actions);
$actionsCountClass = $actionsCount > 0 ? ' actions-count-' . $actionsCount : '';
$actionsMenuClass = 'field-actions-menu dropdown dropleft' . $actionsCountClass;
$actionsMenuUid = md5($vv_attr_mdl . (!empty($vv_attr_id) ? $vv_attr_id : ''));
?>

<div id="action-menu_<?= $actionsMenuUid; ?>"
     class="<?= $actionsMenuClass; ?>">
  <?php
  $linkparams = array(
    'id' => 'action-menu-content_' . $actionsMenuUid,
    'class' => 'nospin action-menu-toggle',
    'escape' => false,
    'data-bs-toggle' => 'dropdown',
    'aria-haspopup' => true,
    'aria-expanded' => false,
    'title' => __('match.fd.action')
  );
  print $this->Html->link(
    '<span class="material-icons">settings</span>',
    'javascript:void(0);',
    $linkparams
  );

  // Sort the actions
  usort($vv_actions, function ($item1, $item2) {
    if ($item1['order'] == $item2['order']) return 0;
    return $item1['order'] < $item2['order'] ? -1 : 1;
  });
    
  ?>
  <ul id="action-list_<?= $actionsMenuUid; ?>" class="dropdown-menu nospin">
    <?php foreach($vv_actions as $action): ?>
      <li class="action-list-item">
        <?php $actionCssClass = (!empty($action['class'])) ? "dropdown-item " . $action['class'] : "dropdown-item"; ?>
        <?php if(empty($action['onclick'])): ?>
          <a class="<?= $actionCssClass; ?>" href="<?= $action['url']; ?>">
            <?php if(!empty($action['icon'])): ?>
              <em class="material-icons"><?= $action['icon']; ?></em>
            <?php endif; ?>
            <?= $action['label']; ?>
          </a>
        <?php else: ?>
          <?php
            // Build a link to a confirm dialog box. The confirm button of the dialog
            // will trigger the postButton built below the menu link.
            $actionUid = 'action-' . md5($action['onclick']['dg_url']);
            $dg_onclick = 'javascript:js_confirm_generic(\''
            . $action['onclick']['dg_bd_txt'] . '\',\''            // dialog body text
            . $actionUid . '\',\''                                 // ID of postButton element to click on confirm
            . $action['onclick']['dg_conf_btn'] . '\',\''          // dialog confirm button
            . $action['onclick']['dg_cancel_btn'] . '\',\''        // dialog cancel button
            . $action['onclick']['dg_title'] . '\',[\''            // dialog title
            . $action['onclick']['dg_bd_txt_repl_str']             // dialog body text replacement strings
            . '\']);';
          ?>
          <a class="<?= $actionCssClass; ?>" href="#" onclick="<?= $dg_onclick; ?>"  
             data-bs-toggle="modal" data-bs-target="#dialog">
            <?php if(!empty($action['icon'])): ?>
              <em class="material-icons"><?= $action['icon']; ?></em>
            <?php endif; ?>
            <?= $action['label']; ?>
          </a>
          <?php // build the postButton element that will be clicked by the modal dialog: ?>
          <?= $this->Form->postButton($action['onclick']['dg_conf_btn'], 
              $action['onclick']['dg_post_btn_array'],['id' => $actionUid, 'class'   => 'hidden']); ?>
        <?php endif; ?>
      </li>
    <?php endforeach;?>
  </ul>
</div>