0

I want to pass value of id in new variable v_id so that if we are displaying news in frontend value of v_id can be shown in front end Essentially the role which id do, can be performed by v_id Pls guide

<?php
/**
 * @version    CVS: 1.0.0
 * @package    Com_Latestnews
 * @author     RM <[email protected]>
 * @copyright  RM
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Latestnews\Component\Latestnews\Site\Model;
// No direct access.
defined('_JEXEC') or die;

use \Joomla\CMS\Factory;
use \Joomla\Utilities\ArrayHelper;
use \Joomla\CMS\Language\Text;
use \Joomla\CMS\Table\Table;
use \Joomla\CMS\MVC\Model\ItemModel;
use \Joomla\CMS\Helper\TagsHelper;
use \Joomla\CMS\Object\CMSObject;
use \Joomla\CMS\User\UserFactoryInterface;
use \Latestnews\Component\Latestnews\Site\Helper\LatestnewsHelper;

/**
 * Latestnews model.
 *
 * @since  1.0.0
 */
class DetailModel extends ItemModel
{
    public $_item;

    

    

    /**
     * Method to auto-populate the model state.
     *
     * Note. Calling getState in this method will result in recursion.
     *
     * @return  void
     *
     * @since   1.0.0
     *
     * @throws Exception
     */
    protected function populateState()
    {
        $app  = Factory::getApplication('com_latestnews');
        $user = $app->getIdentity();

        // Check published state
        if ((!$user->authorise('core.edit.state', 'com_latestnews')) && (!$user->authorise('core.edit', 'com_latestnews')))
        {
            $this->setState('filter.published', 1);
            $this->setState('filter.archived', 2);
        }

        // Load state from the request userState on edit or from the passed variable on default
        if (Factory::getApplication()->input->get('layout') == 'edit')
        {
            $id = Factory::getApplication()->getUserState('com_latestnews.edit.detail.id');
        }
        else
        {
            $id = Factory::getApplication()->input->get('id');
            Factory::getApplication()->setUserState('com_latestnews.edit.detail.id', $id);
        }

        $this->setState('detail.id', $id);

        // Load the parameters.
        $params       = $app->getParams();
        $params_array = $params->toArray();

        if (isset($params_array['item_id']))
        {
            $this->setState('detail.id', $params_array['item_id']);
        }

        $this->setState('params', $params);
    }

    /**
     * Method to get an object.
     *
     * @param   integer $id The id of the object to get.
     *
     * @return  mixed    Object on success, false on failure.
     *
     * @throws Exception
     */
    public function getItem($id = null)
    {
        if ($this->_item === null)
        {
            $this->_item = false;

            if (empty($id))
            {
                $id = $this->getState('detail.id');
            }

            // Get a level row instance.
            $table = $this->getTable();

            // Attempt to load the row.
            if ($table && $table->load($id))
            {
                

                // Check published state.
                if ($published = $this->getState('filter.published'))
                {
                    if (isset($table->state) && $table->state != $published)
                    {
                        throw new \Exception(Text::_('COM_LATESTNEWS_ITEM_NOT_LOADED'), 403);
                    }
                }

                // Convert the Table to a clean CMSObject.
                $properties  = $table->getProperties(1);
                $this->_item = ArrayHelper::toObject($properties, CMSObject::class);

                
            }

            if (empty($this->_item))
            {
                throw new \Exception(Text::_('COM_LATESTNEWS_ITEM_NOT_LOADED'), 404);
            }
        }

        

        return $this->_item;
    }
    


    /**
     * Get an instance of Table class
     *
     * @param   string $type   Name of the Table class to get an instance of.
     * @param   string $prefix Prefix for the table class name. Optional.
     * @param   array  $config Array of configuration values for the Table object. Optional.
     *
     * @return  Table|bool Table if success, false on failure.
     */
    public function getTable($type = 'Detail', $prefix = 'Administrator', $config = array())
    {
        return parent::getTable($type, $prefix, $config);
    }

    /**
     * Get the id of an item by alias
     *
     * @param   string $alias Item alias
     *
     * @return  mixed
     */
    public function getItemIdByAlias($alias)
    {
        $table      = $this->getTable();
        $properties = $table->getProperties();
        $result     = null;
        $aliasKey   = null;
        if (method_exists($this, 'getAliasFieldNameByView'))
        {
            $aliasKey   = $this->getAliasFieldNameByView('detail');
        }
        

        if (key_exists('alias', $properties))
        {
            $table->load(array('alias' => $alias));
            $result = $table->id;
        }
        elseif (isset($aliasKey) && key_exists($aliasKey, $properties))
        {
            $table->load(array($aliasKey => $alias));
            $result = $table->id;
        }
        
            return $result;
        
    }

    /**
     * Method to check in an item.
     *
     * @param   integer $id The id of the row to check out.
     *
     * @return  boolean True on success, false on failure.
     *
     * @since   1.0.0
     */
    public function checkin($id = null)
    {
        // Get the id.
        $id = (!empty($id)) ? $id : (int) $this->getState('detail.id');
                
        if ($id)
        {
            // Initialise the table
            $table = $this->getTable();

            // Attempt to check the row in.
            if (method_exists($table, 'checkin'))
            {
                if (!$table->checkin($id))
                {
                    return false;
                }
            }
        }

        return true;
        
    }

    /**
     * Method to check out an item for editing.
     *
     * @param   integer $id The id of the row to check out.
     *
     * @return  boolean True on success, false on failure.
     *
     * @since   1.0.0
     */
    public function checkout($id = null)
    {
        // Get the user id.
        $id = (!empty($id)) ? $id : (int) $this->getState('detail.id');

                
        if ($id)
        {
            // Initialise the table
            $table = $this->getTable();

            // Get the current user object.
            $user = Factory::getApplication()->getIdentity();

            // Attempt to check the row out.
            if (method_exists($table, 'checkout'))
            {
                if (!$table->checkout($user->get('id'), $id))
                {
                    return false;
                }
            }
        }

        return true;
                
    }

    /**
     * Publish the element
     *
     * @param   int $id    Item id
     * @param   int $state Publish state
     *
     * @return  boolean
     */
    public function publish($id, $state)
    {
        $table = $this->getTable();
                
        $table->load($id);
        $table->state = $state;

        return $table->store();
                
    }

    /**
     * Method to delete an item
     *
     * @param   int $id Element id
     *
     * @return  bool
     */
    public function delete($id)
    {
        $table = $this->getTable();

        
            return $table->delete($id);
        
    }

    
}
1
  • (Not resolving advice, but a refinement) $id = (!empty($id)) ? $id : (int) $this->getState('detail.id'); is functionally equivalent to $id = $id ?: (int) $this->getState('detail.id'); because $id is unconditionally declared in the method signature with a default value of null. Personally, I'd probably write $id ??= (int) $this->getState('detail.id'); I do not know how to resolve your question. Dec 29, 2022 at 7:46

1 Answer 1

2

If I understand well your question and code, you should change:

Factory::getApplication()->input->get('id');

To this:

Factory::getApplication()->input->get('v_id', 0, 'int');

Check Joomla\Input\Input is the one used to manage retrieving data from the application environment

1
  • thanks, indeed worked like charm.
    – Ruchika
    Jan 1, 2023 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.