Octobercms插件可以连接到xml之类的平面文件吗

时间:2019-05-23 13:36:52

标签: octobercms octobercms-plugins

我正在尝试为octobercms创建一个插件,该插件从2个xml文件中获取信息,并且我想知道构建器插件是否适合生成完成此任务所需的文件,如果是的话,我是否需要修改模型文件这样我就可以连接到xlm文件以获取所需的数据

我一直在考虑这一点

    <?php namespace Xml\Xmldata\Models;
use Backend\Models\User;
use Cms\Classes\Page;
use File;
use Flash;
use Hash;
use Markdown;
use Model;
use October\Rain\Support\ValidationException;
use Storage;
use Str;
use System\Classes\PluginManager;
use System\Models\File as FileModel;
use Xml\Xmldata\Xmlloadfile;
/**
 * XML File Model
 */
class Xmldata extends Model
{
$xmldata = simplexml_load_file("../storage/testdata.xml")
$keypairdata1    = "";
$keypairdata2 = "";

for ($i = 0; $i < count($xmldata); $i++){
    $keypairdata1    = $xmldata->testdata[$i]->keypairdata1;
    $keypairdata2 = $xmldata->testdata[$i]->keypairdata2;
}

testdata xml文件

<MYData>
    <login_details>
        <unique_ref>1-61</unique_ref>
        <login_name>tomme</login_name>
        <login>me</login>
        <password>me</password>
        <file1>Test</file1>
        <file2/>
        <file3/>
        <file4/>
    </login_details>
</MYData

这是客户端数据文件

<Mydata>
    <client-data>
        <refno_con>63</refno_con>
        <details>Picture No 14</details>
        <stat_date>2011-10-04</stat_date>
        <val_amount>460.00</val_amount>
        <stat_file>Z:\DATA\\documents\Lanscape.jpg</stat_file>
        <unique_ref>1-63</unique_ref>
    </client-data>  
</Mydata>

4 个答案:

答案 0 :(得分:1)

我只想添加一条评论,但这很容易,我全都支持简单的路线,

 <?
   use Rainlab\User\Models\User;
   function onStart() {
    // Anonymous Class only working on PHP7
    $this['code'] = new class {
      public function data() {
              $path = themes_path('path to clients in themes directory /xml/data.xml');
              $numPerPage = 12; // max. number of items to display per page
              $xml = simplexml_load_file($path);
              $refno_con = Auth::getUser()->refno_con;// you will need to add this field to your users table to reflect the values in you clients data file
              $data = $xml->xpath('/Mydata/client-data[refno_con="' . $refno_con . '"]');
              $details = json_decode(json_encode($data), TRUE);
              krsort($details); // sorts an associative array in descending order, according to the key
              //ksort($details); //sorts an associative array in ascending order, according to the key
              $pagedArray = array_chunk($details, $numPerPage, true);
              $nthPage = $pagedArray['1'];
              return $nthPage;
           }

    };

}
?>

这应该可以完成工作

答案 1 :(得分:0)

如果您希望类似模型的界面与XML文件进行交互,那么最好的选择是利用随附的Halcyon(Eloquent表兄弟)库。请参见https://github.com/octobercms/october/blob/wip%2Fhalcyon-db-datasource/modules/cms/classes/Meta.php,以获取用于在YAML文件中存储和加载数据的Halcyon模型的示例。您可以以此为起点,弄清楚如何对XML文件执行相同的操作。

答案 2 :(得分:0)

我一直在考虑自己做这样的事情,但没有时间,但是找到了一个有趣的代码段,您可能想要尝试而不是重新发明轮子,一个叫Ante Laca的人创建了一个名为xmldb的脚本。您可以通过搜索他的名字来找到它,或者这是指向github上文件的链接https://github.com/alaca/xmldb,据我了解,该文件具有完整的CRUD支持,让我知道您的使用方法

答案 3 :(得分:0)

我一直在研究这个问题,过去我曾在一个客户网站上这样做

<?php
use Rainlab\User\Models\User;


function onStart() {
    // Anonymous Class only working on PHP7
    $this['code'] = new class {
            public function data() {
              $numPerPage = 12; // max. number of items to display per page
              $data = range(1, 150); // data array to be paginated
              $num_results = count($data);
              $xml = simplexml_load_file("http://to your xml file");
              $users = User::whereIsActivated(true)->get();
                  foreach ($users as $user) {
                       $refno_con = $user->refno_con;
                       $data = $xml->xpath('/Mydata/client-data[refno_con="' . $refno_con . '"]');
                       $details = json_decode(json_encode($data), TRUE);

                        krsort($details); // sorts an associative array in descending order, according to the key
                      //ksort($details); //sorts an associative array in ascending order, according to the key

                       $pagedArray = array_chunk($details, $numPerPage, true);
                       $nthPage = $pagedArray['1'];
                       return $nthPage;

                  }
           }

    };

}
?>

并将其放置在页面的代码部分中,只需使用树枝访问

{# in twig Markup #}
    <ul>
        {% for item in code.data %}
            <li>{{ item.details }}</li>
            <li>{{ item.stat_date }}</li>
            <li>{{ item.refno_con }}</li>
        {% endfor %}
    </ul>

您将需要使用User插件并对其进行修改以适合您的需求,我已经使用refno_con遍历示例文件中的客户端详细信息,希望对您有所帮助