• Support will be limited until May 8th, as I will be out of the office travelling. Thank you for your patience and understanding.

Not planned Implementation of Views increaser

Status
Not open for further replies.

Sbenny

Customer
Here's the code of the item I created, basically it'll add views to a thread defined by the user. Maybe the possibility to define how many views to increase, instead of a static number (see number 100 at the very end of the simple php script), in order to create different items for different amount of views.

It comes very handy if some nodes are sorted by views, making it challenging for users to use the Views increaser item as much as possible in order to push their thread (or a thread they like) at the top.

Hope you'll consider it for next update, as I'd like to keep using your official code, instead of having to make manual edits every time, thank you :)

Code:
<?php

namespace DBTech\Shop\ItemType;

/**
* Class Views
*
* @package DBTech\Shop\ItemType
*/
class HackViews extends AbstractHandler implements ConfigurableInterface
{
    protected $defaultUserConfig = [
        'contentid' => 0,
    ];
  
  
    /**
     * @param $context
     *
     * @return array
     */
    protected function getDefaultTemplateParams($context)
    {
        $params = parent::getDefaultTemplateParams($context);
      
        switch ($context)
        {
            case 'user_config_view':
                $params['thread'] = $this->em()->find('XF:Thread', $this->purchase->configuration['contentid']);
                break;
        }
      
        return $params;
    }
  
    /**
     * @return string
     */
    public function getAdminConfigTemplate()
    {
        return '';
    }
  
    /**
     * @param array $input
     *
     * @return array
     */
    public function filterUserConfig(array $input = [])
    {
        return $this->app()->inputFilterer()->filterArray($input, [
            'contentid' => 'str',
        ]);
    }
  
    /**
     * @param array $configuration
     * @param null $errors
     *
     * @return bool
     */
    public function validateUserConfig(array &$configuration = [], &$errors = null)
    {
        if (empty($configuration['contentid']))
        {
            $errors = \XF::phraseDeferred('please_complete_required_fields');
            return false;
        }
      
        if (is_numeric($configuration['contentid']))
        {
            $thread = $this->em()->find('XF:Thread', $configuration['contentid']);
            if (!$thread)
            {
                $errors = \XF::phraseDeferred('no_thread_could_be_found_with_id_x', ['thread_id' => $configuration['contentid']]);
                return false;
            }
        }
        else
        {
            $threadRepo = $this->app()->repository('XF:Thread');
            $thread = $threadRepo->getThreadFromUrl($configuration['contentid'], null, $errors);
            if (!$thread)
            {
                return false;
            }
          
            $configuration['contentid'] = $thread->thread_id;
        }
      
        return true;
    }
  
    /**
     * @return string
     */
    public function getConfigurationForConversation()
    {
        /** @var \XF\Entity\Thread $thread */
        $thread = $this->em()->find('XF:Thread', $this->purchase->configuration['contentid']);
        if (!$thread)
        {
            return '';
        }
      
        return \XF::phrase('dbtech_shop_configuration_notice_increase_views', [
            'thread_url' => $this->app()->router('public')->buildLink('full:threads', $thread),
            'thread' => new \XF\PreEscaped($thread->title)
        ]);
    }
  
    /**
     * @param bool $wasConfigured
     *
     * @throws \XF\PrintableException
     */
    protected function afterConfiguration($wasConfigured = false)
    {
        /** @var \XF\Entity\Thread $thread */
        $thread = $this->em()->find('XF:Thread', $this->purchase->configuration['contentid']);
        if (!$thread)
        {
            return;
        }
      
        $thread->view_count += 100; // turning 100 into a variable would make sense I guess
        $thread->->save();
    }
}


Let me know what you think :)

Hope it helps.
 
Upvote 0
This suggestion has been closed. Votes are no longer accepted.
Hello @Sbenny,

Thank you for your suggestion for DragonByte Shop. Your request will be reviewed by a member of our team shortly.

Unless there are any problems preventing these features from being added to the product, this thread will not receive another reply until it is time to review logged feature requests for implementation.

We appreciate you taking the time to help us improve our products!


- DragonByte Tech Staff
 
You don't need to modify the code for this, you can create a new add-on and associate your item handler with this add-on instead.

This is the recommended way of customising the mod with new item types.

You could even release it as an add-on over @ XenForo.com :)
 
I was thinking about contributing in order to make your great add-on even more complete with this simple modification, but if you think you're not interested or you have no time right now, I'll definitely look into following your suggestion.
 
Don't get me wrong, I'm grateful for the contribution :) I am quite busy these days with custom work as well as a major eCommerce update, so making it into a separate add-on might be the best way to get it into people's hands quicker.

It could also end up helping more than adding it to the core; it'll show others how easy it can be to add their own item types with the new system :)
 
Hello @Sbenny,

As we have not heard back from you, your ticket regarding DragonByte Shop has now been closed.

If your ticket has not been resolved, please feel free to start a new support ticket and link back to this ticket.

If you have time, please leave a review on XenForo.com's Resource Manager.

Thank you.


- DragonByte Technologies, Ltd.
 
Status
Not open for further replies.
Back
Top