it-swarm-id.com

Memanggil model dari kesalahan tampilan: Kesalahan fatal: Memanggil ke fungsi yang tidak ditentukan

Saya baru mulai dengan pengembangan komponen Joomla dan MVC secara umum. Saya juga tidak terlalu kuat di PHP tapi saya mencoba membangun komponen yang mendapat json dan menampilkannya sebagai daftar akordeon jQuery.

Saya mulai dengan membuat skrip PHP yang berfungsi baik dan berfungsi baik).

Sekarang saya pada bagian komponen dan MVC tersandung saya. Saya mengadaptasi komponen hello world di tutorial dokumen yang ditemukan di sini: http://docs.joomla.org/Developing_a_Model-View-Controller_Component/3.1/Introduction

Tetapi ketika saya menginstal komponen dan pergi ke komponen saya, saya mendapatkan:

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

Jadi pandangan saya tidak dapat menggunakan fungsi yang saya definisikan dalam model saya. Saya harus kehilangan sesuatu karena saya pikir controller menetapkan model ke tampilan secara otomatis.

Berikut ini beberapa sumber, (maaf saya tidak membuat 8 spasi pada setiap baris secara manual)

situs\model\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;
}
}

situs\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 (yang kosong)

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

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

situs\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();

situs\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>

Adakah wawasan mengapa saya tidak bisa mendapatkan fungsi model saya? Saya juga menyambut baik dengan menunjukkan kesalahan dan kelemahan keamanan lainnya.

EDIT: fungsi idMask dihapus di posting saya karena konten sensitif. Mungkin bukan itu masalahnya.

EDIT: Saya memiliki pemuatan komponen sekarang dan telah memperbarui kode tapi saya mengalami masalah dengan array. taskStyler harus mengembalikan array yang akan diset ke $completed_tasks[i].

completed_tasks[0][0] Benar tetapi completed_tasks[0][1] Berisi larik yang terdiri dari apa yang seharusnya completed_tasks[0][1-12].

Ini adalah versi saya dari fungsi yang relevan saat ini:

Panggilan getTasks dan kompilasi berbagai tugas yang sedang berlangsung

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;
}

Memanggil getTasks dan mengkompilasi berbagai tugas yang selesai

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 keriting dengan server API yang mengembalikan berbagai tugas (dikenal berfungsi dengan sempurna)

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);
}

Mengembalikan array yang mewakili bidang data tugas dengan kunci dan beberapa nilai yang diedit untuk dibaca, dan tag html, kelas dan id ditambahkan.

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

Masalahnya ada di pandangan Anda.

Anda dapat menggunakan $this->get('project'), yang akan memanggil metode getProject() model Anda. Namun itu tidak memungkinkan untuk memberikan argumen.

Jadi dalam kasus Anda, Anda harus mengambil model terlebih dahulu dan memanggil metode di atasnya:

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

Untuk meneruskan params ke fungsi model Anda, tambahkan di bawah ini ke tugas tampilan tampilan Anda

$ model = $ this-> getModel ();

Lalu awalan getProject () dan yang lainnya dengan

$ model->

Kemudian Anda dapat mengirimkan params ke metode.

Selamat Joomla! Ng

0
Mathew Lenning