Issues with Route in Task View

I created a plugin that registers contacts made within a task. The plugin loads the form and saves the data into the database.

The issue is that I am unable to configure the plugin’s route to load the list of contacts saved in the database within the task view.

Below is the Plugin.php file.

<?php

namespace Kanboard\Plugin\ContactInfo;

use Kanboard\Core\Plugin\Base;
use Kanboard\Core\Translator;

class Plugin extends Base
{
    public function initialize()
    {
        // $this->route->addRoute('/contact-info', 'ContactInfoController', 'show', 'ContactInfo');
        $this->route->addRoute('/task/:task_id/contact-info', 'ContactInfoController', 'show', 'ContactInfo');
        
        // Hook para adicionar o formulário de contato
        // $this->template->hook->attach('template:task:sidebar:information', 'ContactInfo:task/contact_info', array());
        
        // Hook para listar os contatos
        $this->template->hook->attach('template:task:details:bottom', 'ContactInfo:task/contact_info');
        $this->template->hook->attach('template:task:details:bottom', 'ContactInfo:task/contact_info_list');
        
    }

    public function getClasses()
    {
        return array(
            'Plugin\ContactInfo\Controller' => array(
                'ContactInfoController',
            ),
            'Plugin\ContactInfo\Model' => array(
                'ContactInfoModel',
            ),
        );
    }

    public function getPluginName()
    {
        return 'ContactInfo';
    }

    public function getPluginDescription()
    {
        return t('Plugin para gerenciar informações de contato nas tarefas.');
    }

    public function getPluginAuthor()
    {
        return 'Seu Nome';
    }

    public function getPluginVersion()
    {
        return '1.0.0';
    }

    public function getPluginHomepage()
    {
        return 'http://seusite.com';
    }
}

kanboard/
└── plugins/
    └── ContactInfo/
        ├── Controller/
        │   └── ContactInfoController.php
        ├── Model/
        │   └── ContactInfoModel.php
        ├── Template/
        │   └── task/
        │       └── contact_info.php
        ├── Plugin.php
        └── Schema.php

Acessing manualy onthe link:
http://localhost/Kanban4/kanboard-1.2.39/?controller=ContactInfoController&action=show&task_id=3&plugin=ContactInfo

the function show() is called, but only inside of:
http://localhost/Kanban4/kanboard-1.2.39/?controller=TaskViewController&action=show&task_id=3

does not show and starts the “ELSE” of contact_info_list.php “Nothing found”

It’s difficult to say anything useful, without seeing the entire code.

Sorry, this is my first plugin and I thought the main one was Plugin.php
The code: ContactInfoController.php

<?php

namespace Kanboard\Plugin\ContactInfo\Controller;

use Kanboard\Controller\BaseController;

class ContactInfoController extends BaseController
{
    public function save()
    {
        
        $task_id = $this->request->getIntegerParam('task_id');
        $content = $this->request->getValues();
        $content = $content['contact_info'];

        if (!empty($content)) {
            if ($this->contactInfoModel->create($task_id, $content)) {
                $this->flash->success(t('Contact information added successfully.'));
            } else {
                $this->flash->failure(t('Failed to add contact information.'));
            }
        } else {
            $this->flash->failure(t('Content cannot be empty.'));
        }

        $this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('task_id' => $task_id)));
        
        
    }


    public function show()
    {
        echo "====<pre>"; 
        print_r('asdasd');
        echo "</pre>"; 
        exit;
        
        // Obtém o task_id da URL
        $task_id = $this->request->getIntegerParam('task_id');
        
        // Obtém as informações da tarefa
        $task = $this->taskFinderModel->getById($task_id);
        
        if (!$task) {
            // throw new \Kanboard\Core\Error\PageNotFoundException();
        }

        // Obtém as informações de contato associadas à tarefa
        $contactInfos = $this->contactInfoModel->getAll($task_id);

        // Exibe os dados na página de visualização
        $this->response->html($this->template->render('task/contact_info', array(
            'task' => $task,
            'contactInfos' => $contactInfos
        )));
    }
   
}

The code: ContactInfoModel.php

<?php

namespace Kanboard\Plugin\ContactInfo\Model;

use Kanboard\Core\Base;

class ContactInfoModel extends Base
{
    const TABLE = 'contact_infos';

    public function create($task_id, $content)
    {
        return $this->db->table(self::TABLE)->insert(array(
            'task_id' => $task_id,
            'content' => $content,
        ));
    }

    public function getAll($task_id)
    {
        $results = $this->db->table(self::TABLE)
                            ->eq('task_id', $task_id)  // Filtro pela tarefa
                            ->findAll();  // Recupera todos os contatos associados
        
        return $results;
    }

    public function getAllContacts()
{
    return $this->db->table(self::TABLE)->findAll();
}

public function listAll()
{
    
    $contactInfos = $this->contactInfoModel->getAllContacts();
    error_log('All contact infos: ' . print_r($contactInfos, true));
}    
}

The code: contact_info_list.php

<?php if (!empty($contactInfos)): ?>
    <h3><?= t('Contact Information') ?></h3>
    <ul>
        <?php foreach ($contactInfos as $info): ?>
            <li><?= $this->text->e($info['content']) ?></li>
        <?php endforeach ?>
    </ul>
<?php else: ?>
    <p><?= t('Nothing found') ?></p>
<?php endif ?>

The code: contact_info.php

<?php  if (!empty($task['id'])): ?>
    <div class="panel">
        <h3><?= t('Registro de contatos:') ?></h3>
        <form method="post" action="<?= $this->url->href('ContactInfoController', 'save', array('task_id' => $task['id'], 'plugin' => 'ContactInfo')) ?>" autocomplete="off">
            <?= $this->form->csrf() ?>
            <?= $this->form->text('contact_info', array(), array(), array('required')) ?>
            <button type="submit" class="btn btn-blue"><?= t('Save') ?></button>
        </form>
        <ul>
            <?php if (!empty($contactInfos)): ?>
                <?php foreach ($contactInfos as $info): ?>
                    <li><?= $this->text->e($info['content']) ?></li>
                <?php endforeach ?>
            <?php endif ?>
        </ul>
    </div>
<?php endif ?>

in ContactInfoController.php the “echo” is just for me to see if the show() function was called

I’m sorry, but I still don’t understand the logic of your plugin.
It’s relatively weird, I think it is not legit or useful to add a form to the task detail page.
You should invoke it from the sidebar.

I hope I’m not too harsh, but
may I suggest you get familiar with the Kanboard internals regarding plugin development first. There are many plugins around to study how things can be done in a compatible way.

Then please publish your work on any git platform, so that others really can support you with your issues. Studying code within the forum is not a pleasure.

Sorry, for now, I’m unable to help further.

Good morning my friend,

You were not rude, and I appreciate the time you took to try to help me. First of all, I want you to know that I searched the documentation to understand how the plugin works, but what I was looking for wasn’t mentioned there. That’s why I sought help here in the forum.

My intention was to do something different from the usual. A very popular saying in my country goes: “For those with strong minds, the impossible is just a matter of opinion.”

As you can see in the image below, I managed to introduce what I wanted within the task view. This way, I make it easier for the user to register and view the contacts made without having to leave the view or go to another screen for that.

Once again, I appreciate the time you took to try to help me. Big hug.

1 Like