<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Event_Model_Event extends Core_Model_Item_Abstract
{
  protected $_owner_type = 'user';
  
  public function getAttachmentClass() {
    return " feed_attachments_content_item ";
  }

  public function membership()
  {
    return new Engine_ProxyObject($this, Engine_Api::_()->getDbtable('membership', 'event'));
  }

  public function _postInsert()
  {
    parent::_postInsert();
    // Create auth stuff
    $context = Engine_Api::_()->authorization()->context;
    $context->setAllowed($this, 'everyone', 'view', true);
    $context->setAllowed($this, 'registered', 'comment', true);
    $viewer = Engine_Api::_()->user()->getViewer();
  }


  public function setPhoto($photo)
  {
    if( $photo instanceof Zend_Form_Element_File ) {
      $file = $photo->getFileName();
    } else if( is_array($photo) && !empty($photo['tmp_name']) ) {
      $file = $photo['tmp_name'];
    } else if( is_string($photo) && file_exists($photo) ) {
      $file = $photo;
    } else {
      throw new Event_Model_Exception('invalid argument passed to setPhoto');
    }

    $name = basename($file);
    $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
    $params = array(
     'parent_id' => $this->getIdentity(),
     'parent_type'=>'event'
    );
    
    // Save
    $storage = Engine_Api::_()->storage();
    
    // Resize image (main)
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(720, 720)
      ->write($path.'/m_'.$name)
      ->destroy();

    // Resize image (profile)
    $image = Engine_Image::factory();
    $image->open($file)
      ->resize(400, 400)
      ->write($path.'/p_'.$name)
      ->destroy();

    // Store
    $iMain = $storage->create($path.'/m_'.$name, $params);
    $iProfile = $storage->create($path.'/p_'.$name, $params);

    $iMain->bridge($iProfile, 'thumb.profile');

    // Remove temp files
    @unlink($path.'/p_'.$name);
    @unlink($path.'/m_'.$name);

    // Update row
    $this->modified_date = date('Y-m-d H:i:s');
    $this->photo_id = $iMain->file_id;
    $this->save();

    // Add to album
    $viewer = Engine_Api::_()->user()->getViewer();
    $photoTable = Engine_Api::_()->getItemTable('event_photo');
    $eventAlbum = $this->getSingletonAlbum();
    $photoItem = $photoTable->createRow();
    $photoItem->setFromArray(array(
      'event_id' => $this->getIdentity(),
      'album_id' => $eventAlbum->getIdentity(),
      'user_id' => $viewer->getIdentity(),
      'file_id' => $iMain->getIdentity(),
      'collection_id' => $eventAlbum->getIdentity(),
      'user_id' =>$viewer->getIdentity(),
    ));
    $photoItem->save();

    return $this;
  }

  public function getDescription()
  {
    // @todo decide how we want to handle multibyte string functions
    $tmpBody = strip_tags($this->description);
    return ( Engine_String::strlen($tmpBody) > 255 ? Engine_String::substr($tmpBody, 0, 255) . '...' : $tmpBody );
  }

  /**
   * Gets an absolute URL to the page to view this item
   *
   * @return string
   */
  public function getHref($params = array())
  {
    $params = array_merge(array(
      'route' => 'event_profile',
      'reset' => true,
      'id' => $this->getIdentity(),
      'slug' => $this->getSlug(),
    ), $params);
    $route = $params['route'];
    $reset = $params['reset'];
    unset($params['route']);
    unset($params['reset']);
    return Zend_Controller_Front::getInstance()->getRouter()
      ->assemble($params, $route, $reset);
  }
  
	public function getParent($recurseType = null) {
		return $this->getOwner('user');
	}

  protected function _delete()
  {
    if( $this->_disableHooks ) return;

    // Delete all memberships
    $this->membership()->removeAllMembers();


    // Delete all albums
    $albumTable = Engine_Api::_()->getItemTable('event_album');
    $albumSelect = $albumTable->select()->where('event_id = ?', $this->getIdentity());
    foreach( $albumTable->fetchAll($albumSelect) as $eventAlbum ) {
      $eventAlbum->delete();
    }

    // Delete all topics
    $topicTable = Engine_Api::_()->getItemTable('event_topic');
    $topicSelect = $topicTable->select()->where('event_id = ?', $this->getIdentity());
    foreach( $topicTable->fetchAll($topicSelect) as $eventTopic ) {
      $eventTopic->delete();
    }
    
    parent::_delete();
  }


  public function getSingletonAlbum()
  {
    $table = Engine_Api::_()->getItemTable('event_album');
    $select = $table->select()
      ->where('event_id = ?', $this->getIdentity())
      ->order('album_id ASC')
      ->limit(1);

    $album = $table->fetchRow($select);

    if( null === $album )
   {
      $album = $table->createRow();
      $album->setFromArray(array(
        'event_id' => $this->getIdentity()
      ));
      $album->save();
    }

    return $album;
  }

  public function categoryName()
  {
    $categoryTable = Engine_Api::_()->getDbtable('categories', 'event');
    return $categoryTable->select()
        ->from($categoryTable, 'title')
        ->where('category_id = ?', $this->category_id)
        ->limit(1)
        ->query()
        ->fetchColumn();
  }

  public function getAttendingCount()
  {
    return $this->membership()->getMemberCount(true, Array('rsvp' => 2));
  }

  public function getMaybeCount()
  {
    return $this->membership()->getMemberCount(true, Array('rsvp' => 1));
  }

  public function getNotAttendingCount()
  {
    return $this->membership()->getMemberCount(true, Array('rsvp' => 0));
  }

  public function getAwaitingReplyCount()
  {
    return $this->membership()->getMemberCount(false, Array('rsvp' => 3));
  }

  /**
   * Gets a proxy object for the likes handler
   *
   * @return Engine_ProxyObject
   **/
  public function likes()
  {
    return new Engine_ProxyObject($this, Engine_Api::_()->getDbtable('likes', 'core'));
  }

  /**
   * Gets a proxy object for the comment handler
   *
   * @return Engine_ProxyObject
   **/
  public function comments()
  {
    return new Engine_ProxyObject($this, Engine_Api::_()->getDbtable('comments', 'core'));
  }
  public function follow()
  {
      return true;
  }
  public function getParentItem(){
    if(!$this->parent_type || !$this->parent_id || $this->parent_type == "user")
      return false;
    return Engine_Api::_()->getItem($this->parent_type,$this->parent_id) ?? false;
  }
  public function getCategoryItem()
  {
      if(!$this->category_id)
        return false;
      return Engine_Api::_()->getItem('event_category',$this->category_id);
  }
}
