<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>

<?php if ($this->viewer->getIdentity()) { ?>
  <div class="user_cover_buttons">
    <ul>
      <?php
      $label = $this->translate("Edit My Profile");
      if (!$this->viewer->isSelf($this->subject)) {
        $label = $this->translate("Edit User Profile");
      }
      $auth = $this->subject->isSuperAdmin() ? $this->viewer->isSuperAdmin($this->subject) : 1;
      if ($this->subject->authorization()->isAllowed($this->viewer, 'edit') && $auth) {
      ?>
        <li>
          <a  class="btn btn-primary" href="<?php echo $this->url(array('controller' => 'edit', 'action' => 'profile', 'id' => ($this->viewer->getGuid(false) == $this->subject->getGuid(false) ? null : $this->subject->getIdentity())), 'user_extended', true)  ?>">
            <i class="icon_user_edit"></i>
            <span> <?php echo $this->translate($label); ?> </span>
          </a>
        </li>
      <?php } ?>
      <?php
      $messageAllowed = true;
      if (!$this->viewer->getIdentity() || $this->viewer->getGuid(false) === $this->subject->getGuid(false)) {
        $messageAllowed = false;
      } else {
        // Get setting?
        $this->viewerPermission = Engine_Api::_()->authorization()->getPermission($this->viewer->level_id, 'messages', 'create');
        if (Authorization_Api_Core::LEVEL_DISALLOW === $this->viewerPermission) {
          $messageAllowed = false;
        } else {
          $this->subjectPermission = Engine_Api::_()->authorization()->getPermission($this->subject->level_id, 'messages', 'create');
          if (Authorization_Api_Core::LEVEL_DISALLOW === $this->subjectPermission) {
            $messageAllowed = false;
          } else {
            $messageAuth = Engine_Api::_()->authorization()->getPermission($this->viewer->level_id, 'messages', 'auth');
            if ($messageAuth == 'none') {
              $messageAllowed = false;
            } else if ($messageAuth == 'friends') {
              $friendship_status = $this->subject->membership()->getRow($this->viewer);
              if (!$friendship_status || $friendship_status->active == 0) {
                $messageAllowed = false;
              }
            }
          }
        }
      }
      if ($messageAllowed) {
        if(Engine_Api::_()->getDbTable('modules', 'core')->isModuleEnabled('echat')) { ?>
          <li>
            <a class="btn btn-primary echat_profile_echatmessages send_message_<?php echo $this->subject->getIdentity(); ?>" href="javascript:;">
              <i class="icon_message"></i>
              <span> <?php echo $this->translate('Send Message'); ?> </span>
            </a>
          </li>
      <?php }else{
      ?>
        <li>
          <a class="btn btn-primary smoothbox" href="<?php echo $this->url(array('action' => 'compose', 'to' => $this->subject->getIdentity(), 'format' => 'smoothbox'), 'messages_general', true) ?>">
            <i class="icon_message"></i>
            <span> <?php echo $this->translate('Send Message'); ?> </span>
          </a>
        </li>
      <?php }
      }
      ?>

      <?php
      $allowFriendhship = true;
      if (!$this->viewer->getIdentity() || $this->viewer->getGuid(false) === $this->subject->getGuid(false)) {
        $allowFriendhship = false;
      } else {
        // No blocked
        if ($this->viewer->isBlockedBy($this->subject)) {
          $allowFriendhship = false;
        }
        // Check if friendship is allowed in the network
        $eligible = (int) $settings->getSetting('user.friends.eligible', 2);
        if (!$eligible) {
          $allowFriendhship = false;
        }
        // check admin level setting if you can befriend people in your network
        else if ($eligible == 1) {
          $networkMembershipTable = Engine_Api::_()->getDbtable('membership', 'network');
          $networkMembershipName = $networkMembershipTable->info('name');
          $select = new Zend_Db_Select($networkMembershipTable->getAdapter());
          $select
            ->from($networkMembershipName, 'user_id')
            ->join($networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null)
            ->where("`{$networkMembershipName}`.user_id = ?", $this->viewer->getIdentity())
            ->where("`{$networkMembershipName}_2`.user_id = ?", $this->subject->getIdentity());
          $data = $select->query()->fetch();
          if (empty($data)) {
            $allowFriendhship = false;
          }
        }
        
        if ($allowFriendhship) {
          // Two-way mode
          $row = $this->viewer->membership()->getRow($this->subject);
          if (null === $row) { ?>
						<li>
							<a href="<?php echo $this->url(array('controller' => 'friends', 'action' => 'add', 'user_id' => $this->subject->getIdentity()), 'user_extended', true) ?>" class="btn btn-primary smoothbox icon_friend_add">
								<span> <?php echo $this->translate("Add Friend"); ?> </span>
							</a>
						</li>
          <?php } else if ($row->user_approved == 0) { ?>
						<li>
							<a href="<?php echo $this->url(array('controller' => 'friends', 'action' => 'cancel', 'user_id' => $this->subject->getIdentity()), 'user_extended', true) ?>" class="btn btn-alt smoothbox icon_friend_remove">
								<span> <?php echo $this->translate("Cancel Request"); ?> </span>
							</a>
						</li>
          <?php } else if ($row->resource_approved == 0) { ?>


          <li class="dropdown respond_dropdown">
            <a class="btn btn-primary" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              <?php echo $this->translate("Respond"); ?>
            </a>
            <ul class="dropdown-menu">
              <li>
                <a href="<?php echo $this->url(array('controller' => 'friends', 'action' => 'confirm', 'user_id' => $this->subject->getIdentity()), 'user_extended', true) ?>" class="dropdown-item smoothbox icon_friend_accept">
                  <span> <?php echo $this->translate("Approve Friend Request"); ?> </span>
                </a>
              </li>
              <li>
                <a href="<?php echo $this->url(array('controller' => 'friends', 'action' => 'reject', 'user_id' => $this->subject->getIdentity()), 'user_extended', true) ?>" class="dropdown-item smoothbox icon_friend_reject">
                  <span> <?php echo $this->translate("Ignore Friend Request"); ?> </span>
                </a>
              </li>
            </ul>
          </li>
					<?php } else { ?>
            <li class="dropdown">
              <a class="btn btn-alt icon_friend" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                <span> <?php echo $this->translate("Friends"); ?> </span>
              </a>
              <ul class="dropdown-menu">
                <li>
                <a href="<?php echo $this->url(array('controller' => 'friends', 'action' => 'remove', 'user_id' => $this->subject->getIdentity()), 'user_extended', true) ?>" class="dropdown-item smoothbox icon_friend_remove">
                  <span> <?php echo $this->translate("Remove Friend"); ?> </span>
                </a>
              </li>
              </ul>
            </li>
					<?php } ?>
        <?php } ?>
      <?php } ?>
		
      <?php if ($settings->getSetting('core.followenable', 1)  && !Engine_Api::_()->user()->getViewer()->isSelf($this->subject)) { ?>
        <?php $getFollowUserStatus = Engine_Api::_()->getDbTable('follows', 'user')->getFollowUserStatus($this->subject->user_id); ?>
        <li>
          <?php echo $this->partial('_followmembers.tpl', 'user', array('subject' => $this->subject)); ?>
        </li>
      <?php } ?>
      <li class="dropdown">
        <a href="javascript:void(0)" class="btn btn-alt" data-bs-toggle="dropdown" aria-expanded="false">
          <i class="icon_option_menu"></i>
        </a>
        <ul class="dropdown-menu dropdown-menu-end">
          <?php foreach ($this->userNavigation as $link) : ?>
            <li>
              <a class="<?php echo  'dropdown-item' . ($link->getClass() ? ' ' . $link->getClass() : '') . (!empty($link->get('icon')) ? $link->get('icon') : ''); ?>" href='<?php echo $link->getHref() ?>' aria-label="<?php echo $this->translate($link->getlabel()) ?>">
                <?php echo $this->translate($link->getlabel()) ?>
              </a>
            </li>
          <?php endforeach; ?>
        </ul>
      </li>
    </ul>
  </div>
<?php } ?>

<?php if ($settings->getSetting('core.followenable', 1) && Engine_Api::_()->user()->getViewer()->getIdentity()  && !Engine_Api::_()->user()->getViewer()->isSelf($this->subject)) { ?>
  <?php $getFollowUserStatus = Engine_Api::_()->getDbTable('follows', 'user')->getFollowUserStatus($this->subject->user_id); ?>
  <?php
  $follow_verification = false;
  if (!empty($settings->getSetting('core.allowuserverfication', 0)) && $this->viewer->follow_verification) {
    $follow_verification = true;
  } elseif (empty($settings->getSetting('core.allowuserverfication', 0)) && empty($settings->getSetting('core.autofollow', 0))) {
    $follow_verification = true;
  }
  ?>
  <?php if ($follow_verification && $getFollowUserStatus && $this->viewer->getIdentity() == $getFollowUserStatus->user_id && $getFollowUserStatus->user_approved == 0) { ?>
    <div class="user_cover_follow_request" id="coverphoto_follow_inner">
      <ul class="user_cover_follow_request_buttons">
        <li class="coverphoto_follow_text"><?php echo $this->translate("%s wants to follow you.", $this->subject->getTitle()); ?></li>
        <li>
          <a id="user_follow_accept_<?php echo $getFollowUserStatus->follow_id; ?>" href='javascript:;' data-action="accept" data-follow_id="<?php echo $getFollowUserStatus->follow_id; ?>" data-url='<?php echo $this->subject->getIdentity(); ?>' class='btn btn-success user_follow follow_accept_btn user_follow_<?php echo $this->subject->getIdentity(); ?>'>
            <i class='fa fa-check'></i>
            <span><?php echo $this->translate('Confirm'); ?></span>
          </a>
        </li>
        <li>
          <a id="user_follow_reject_<?php echo $getFollowUserStatus->follow_id; ?>" href='javascript:;' data-action="reject" data-follow_id="<?php echo $getFollowUserStatus->follow_id; ?>" data-url='<?php echo $this->subject->getIdentity(); ?>' class='btn btn-danger user_follow follow_reject_btn user_follow_<?php echo $this->subject->getIdentity(); ?>'>
            <i class='fa fa-times'></i>
            <span><?php echo $this->translate('Delete'); ?></span>
          </a>
        </li>
      </ul>
    </div>
  <?php } ?>
<?php } ?>
