it-swarm-es.tech

Error de modelo de llamada desde vista: error fatal: llamada a función indefinida

Estoy empezando con el desarrollo de componentes de Joomla y MVC en general. Tampoco soy increíblemente fuerte en PHP pero estoy tratando de construir un componente que obtenga json y lo muestre como una lista de acordeones jQuery.

Comencé creando un script PHP script que hace exactamente eso y funciona bien.

Ahora estoy en la parte componente y el mvc me está tropezando. Adapte el componente hello world en el tutorial de documentación que se encuentra aquí: http://docs.joomla.org/Developing_a_Model-View-Controller_Component/3.1/Introduction

Pero cuando instalo el componente y voy a mi componente obtengo:

Fatal error: Call to undefined function getProject() in /[path to joomla]/components/com_insightly/views/insightly/view.html.php on line 23

Por lo tanto, mi vista no puede usar una función que definí en mi modelo. Debo estar perdiendo algo porque pensé que el controlador asignó el modelo a la vista automáticamente.

Aquí hay alguna fuente, (lo siento, no sangría 8 espacios en cada línea manualmente)

sitio\modelos\insightly.php

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');


class InsightlyModelInsightly extends JModelItem
{

public function getProject($user_id)
{

    $project_url = 'https://api.insight.ly/v2/projects/'.$user_id;
    $project = array();
    $html_tag = null;
    $html_class = null;
    $html_id = null;

    $curl = curl_init($project_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Authorization: Basic NotGonnaShareThatNowAmI')
    );
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $curl_response = curl_exec($curl);
    if ($curl_response){
        $decoded = json_decode($curl_response, true);
        $i = 0;
        foreach ($decoded as $key=>$value){
            // Eliminates Empty Fields and Saves to Multi-Dimensional Array
            if ($value){
                $value = id_mask($value);
            }
            switch($key)
            {
                case "PROJECT_NAME":
                    $key = null;
                    $html_tag = "h1";
                    break;
                case "STATUS":
                    $key = "Status";
                    $html_tag = "li";
                    break;
                case "PROJECT_DETAILS":
                    $key = "Description";
                    $html_tag = "li";
                    break;
                case "RESPONSIBLE_USER_ID":
                    $key = "Project Manager";
                    $html_tag = "li";
                    break;
                case "CATEGORY_ID":
                    $key = "Category";
                    $html_tag = "li";
                    break;
                default:
                    $value = null;
            }
            if ($value){
                $field = [$key, $value, $html_tag, $html_class, $html_id];
                $project[$i] = $field;
                $i++;
            }
        }

        return $project;
    }
    else {
        die('An error occurred retrieving Projects. Additional info: ' . curl_error($curl));
    }
    curl_close($curl);
}

public function getActiveTasks($user_id){

    $project_tasks = getTasks($user_id);
    $tasks = loopAndFind($project_tasks, 'STATUS', 'IN PROGRESS');
    $i = 0;
    foreach ($tasks as $key=>$value){
        $active_tasks[$i] = taskStyler($key, $value);
        $i++;
    }

    return $active_tasks;
}

public function getCompletedTasks($user_id){

    $project_tasks = getTasks($user_id);
    $tasks = loopAndFind($project_tasks, 'STATUS', 'COMPLETED');
    $i = 0;
    foreach ($tasks as $key=>$value){
        $completed_tasks[$i] = taskStyler($key, $value);
        $i++;
    }
    return $completed_tasks;
}

function getTasks($user_id){

    $task_url = 'https://api.insight.ly/v2/tasks';

    $curl = curl_init($task_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Authorization: Basic NotGonnaShareThatNowAmI')
    );
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $curl_response = curl_exec($curl);
    if ($curl_response){
        $decoded = json_decode($curl_response, true);
        $project_tasks = loopAndFind($decoded, 'PROJECT_ID', $user_id);
        return $project_tasks;
    }
    else {
        die('An Error occurred retrieving Tasks. Additional info: ' . curl_error($curl));
    }
    curl_close($curl);
}

//Search Tool for Multi-Dimensional Arrays
function loopAndFind($array, $index, $search){
    $returnArray = array();
    foreach($array as $k=>$v){
        if($v[$index] == $search){
            $returnArray[] = $v;
        }
    }
    return $returnArray;
}

//Formats dates as d/m/Y and removes the time
function date_formatter($value){
    date_default_timezone_set('America/New_York');
    $date = date_create($value);
    $value = date_format($date, "m/d/Y");
    return $value;
}

//Returns array with ALLCAPS corrected and html tags
function taskStyler ($key, $value){
    $html_tag = "li";
    $html_class = null;
    $html_id = null;

    switch ($key)
    {
        case "Title":
            $key = "Task";
            $html_tag = "h3";
            break;
        case "DUE_DATE":
            $key = "Due Date";
            $value = date_formatter($value);
            break;
        case "COMPLETED_DATE_UTC":
            $key = "Date Completed";
            $value = date_formatter($value);
            break;
        case "DETAILS":
            $key = "Details";
            $value = strip_tags($value);
            break;
        case "STATUS":
            $key = "Status";
            break;
        case "PERCENT_COMPLETE":
            $key = "Percent Complete";
            $value = $value . "%";
            break;
        case "START_DATE":
            $key = "Start Date";
            $value = date_formatter($value);
            break;
        case "RESPONSIBLE_USER_ID":
            $key = "Assigned to";
            $value = id_mask($value);
            break;
        default:
            $value = null;
    }
    if ($value){
        $task = [$key, $value, $html_tag, $html_class, $html_id];
    }
    else {
        $task = null;
    }
    return $task;
}
}

site\views\insightly\view.html.php

// import Joomla view library
jimport('joomla.application.component.view');

/**
* HTML View class for the Insightly Component
*/

class InsightlyViewInsightly extends JViewLegacy
{

// Overwriting JView display method
function display($tpl = null)
{
    $user =& JFactory::getUser();
    $user_id = $user->get( 'id' );
    $user_id= "765727";     //Set for Development

    // Assign data to the view
    $this->project = getProject($user_id);
    $this->active_tasks = getActiveTasks($user_id);
    $this->completed_tasks = getCompletedTasks($user_id);


    parent::display($tpl);
}
}

controller.php (que está vacío)

// import Joomla controller library
jimport('joomla.application.component.controller');

/**
* Insightly Component Controller
*/
class InsightlyController extends JControllerLegacy
{
}

sitio\insightly.php

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import joomla controller library
jimport('joomla.application.component.controller');

// Get an instance of the controller prefixed by Insightly
$controller = JControllerLegacy::getInstance('Insightly');

// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

// Redirect if set by the controller
$controller->redirect();

site\views\insightly\tmpl\default.php

<article id='project'>
<?php
$iterations = count($this->project);
for ($i = 0; $i <= $iterations; $i++) {
    echo('<' . $this->project[2]);
    if (isset($this->project[3])) {
        echo('class ="' . $this->project[3]);
    }
    if (isset($this->project[4])) {
        echo('class ="' . $this->project[4]);
    }
    echo('>');
    echo($this->project[0]);
    echo(' : ');
    echo($this->project[1]);
    echo('</' . $this->project[2] . '>');
}

?>
<div id='tasks'>
    <h2>Active Tasks</h2>
    <section class='tasks'>
        <?php
        $number_of_tasks = count($this->active_tasks);
        for ($i = 0; $i <= $number_of_tasks; $i++) {
            $task_iterations = count($this->active_tasks[$i]);
            for ($i = 0; $i <= $task_iterations; $i++) {
                echo('<' . $this->active_tasks[$i][2]);
                if (isset($this->active_tasks[$i][3])) {
                    echo('class ="' . $this->active_tasks[$i][3]);
                }
                if (isset($this->active_tasks[$i][4])) {
                    echo('class ="' . $this->active_tasks[$i][4]);
                }
                echo('>');
                echo($this->active_tasks[$i][0]);
                echo(' : ');
                echo($this->active_tasks[$i][1]);
                echo('</' . $this->active_tasks[$i][2] . '>');
            }
        }
        ?>
    </section>
    <h2>Completed Tasks</h2>
    <section class='tasks'>
        <?php
        $number_of_tasks = count($this->completed_tasks);
        for ($i = 0; $i <= $number_of_tasks; $i++) {
            $task_iterations = count($this->completed_tasks[$i]);
            for ($i = 0; $i <= $task_iterations; $i++) {
                echo('<' . $this->completed_tasks[$i][2]);
                if (isset($this->completed_tasks[$i][3])) {
                    echo('class ="' . $this->completed_tasks[$i][3]);
                }
                if (isset($this->completed_tasks[$i][4])) {
                    echo('class ="' . $this->completed_tasks[$i][4]);
                }
                echo('>');
                echo($this->completed_tasks[$i][0]);
                echo(' : ');
                echo($this->completed_tasks[$i][1]);
                echo('</' . $this->completed_tasks[$i][2] . '>');
            }
        }
        ?>
    </section>
</div>
</article>

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
    $(function () {
        $(".tasks").accordion({ collapsible: true, active: false });
    });
</script>

¿Alguna idea de por qué no puedo acceder a la función de mis modelos? También agradezco que se señalen otros errores graves y fallas de seguridad.

EDITAR: la función idMask se eliminó en mi publicación debido a contenido confidencial. Probablemente no sea el problema.

EDITAR: Tengo el componente cargando ahora y he actualizado el código, pero tengo problemas con las matrices. taskStyler debería devolver una matriz que se establecerá en $completed_tasks[i].

completed_tasks[0][0] es correcto pero completed_tasks[0][1] contiene una matriz compuesta de lo que debería sercompleted_tasks[0][1-12].

Esta es mi versión actual de las funciones relevantes:

Llama a getTasks y compila una variedad de tareas que están en progreso

public function getActiveTasks($user_id){

    $project_tasks = $this->getTasks($user_id);
    $tasks = $this->loopAndFind($project_tasks, 'STATUS', 'IN PROGRESS');
    $i = 0;
    foreach ($tasks as $key=>$value){
            $active_tasks[$i] = $this->taskStyler($key, $value);
            $i++;
    }
    return $active_tasks;
}

Llama a getTasks y compila una variedad de tareas que se han completado

public function getCompletedTasks($user_id){

    $project_tasks = $this->getTasks($user_id);
    $tasks = $this->loopAndFind($project_tasks, 'STATUS', 'COMPLETED');
    $i = 0;
    foreach ($tasks as $key=>$value){
            $completed_tasks[$i] = $this->taskStyler($key, $value);
            $i++;
    }
    return $completed_tasks;
}

Sesión de curl con servidor API que devuelve una variedad de tareas (se sabe que funciona perfectamente)

public function getTasks($user_id){

    $task_url = 'https://api.insight.ly/v2/tasks';

    $curl = curl_init($task_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Authorization: Basic Dontlookatmypassword')
    );
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $curl_response = curl_exec($curl);
    if ($curl_response){
        $decoded = json_decode($curl_response, true);
        $project_tasks = $this->loopAndFind($decoded, 'PROJECT_ID', $user_id);
        return $project_tasks;
    }
    else {
        die('An Error occurred retrieving Tasks. Additional info: ' . curl_error($curl));
    }
    curl_close($curl);
}

Devuelve una matriz que representa un campo de datos de una tarea con las claves y algunos valores editados para facilitar la lectura, y se agregan etiquetas html, clases e identificadores.

public function taskStyler ($key, $value){
    $html_tag = "li";
    $html_class = null;
    $html_id = null;

    switch ($key)
    {
        case "Title":
            $key = "Task";
            $html_tag = "h3";
            break;
        case "DUE_DATE":
            $key = "Due Date";
            $value = $this->dateFormatter($value);
            break;
        case "COMPLETED_DATE_UTC":
            $key = "Date Completed";
            $value = $this->dateFormatter($value);
            break;
        case "DETAILS":
            $key = "Details";
            $value = strip_tags($value);
            break;
        case "STATUS":
            $key = "Status";
            break;
        case "PERCENT_COMPLETE":
            $key = "Percent Complete";
            $value = $value . "&#37;";
            break;
        case "START_DATE":
            $key = "Start Date";
            $value = $this->dateFormatter($value);
            break;
        case "RESPONSIBLE_USER_ID":
            $key = "Assigned to";
            $value = $this->idMask($value);
            break;
        default:
            $value = null;
    }
    if ($value) {
        $task = [$key, $value, $html_tag, $html_class, $html_id];
    }
    else{ $task = null;
    }
    return $task;
}
2
John

El problema está en tu opinión.

Puede usar $this->get('project'), que llamaría al método getProject() de su modelo. Sin embargo, no permite pasar una discusión.

Por lo tanto, en su caso, primero debe buscar el modelo y llamar al método:

$model   = $this->getModel();
$project = $model->getProject($user_id);
2
Bakual

Para pasar parámetros a las funciones de su modelo, agregue lo siguiente a su tarea de visualización de vista

$ modelo = $ this-> getModel ();

Luego prefijo getProject () y los demás con

$ modelo->

Luego puede pasar parámetros a los métodos.

Feliz Joomla! Ng

0
Mathew Lenning