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

class Core_Widget_SavecollectionIconController extends Engine_Content_Widget_Abstract
{
    public function indexAction()
    {
        // Check if feature is enabled
        $api = Engine_Api::_()->getApi('savecollection', 'core');
        if (!$api->isEnabled()) {
            return $this->setNoRender();
        }
        
        $subject = Engine_Api::_()->core()->hasSubject() ? Engine_Api::_()->core()->getSubject() : null;
         if (!$subject) {
            return $this->setNoRender();
        }

        // Check if viewer is authenticated
        $viewer = Engine_Api::_()->user()->getViewer();
        if (!$viewer || !$viewer->getIdentity()) {
            return $this->setNoRender();
        }

        // Get widget params
        $contentType = $subject->getType();
        $contentId = (int) $subject->getIdentity();

        if (empty($contentType) || $contentId <= 0) {
            return $this->setNoRender();
        }

        // Check if item is already saved
        $isSaved = $api->getSavedItem($viewer, $contentType, $contentId) !== null;

        $this->view->contentType = $contentType;
        $this->view->contentId = $contentId;
        $this->view->isSaved = $isSaved;
    }
}
