0

I am trying to make a plugin for octobercms that gets infomation from 2 xml files and I was wondering if the builder plugin was suitable to generate the files necessary to complete this task if so would it be the models file I would need alter so that I could to connect to the xlm file to get the required data

I was thinking along the line of this

    <?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 file

<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

This is the client data file

<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>
user2033464
  • 143
  • 1
  • 10
  • If I understand you correctly you would want to put `"../storage/testdata.xml"` in a model so it would be like: `simplexml_load_file(Xmlloadfile::find(1));` Could we get more information on what exactly the XML offers? Why don't put the XML data into a model as a text field or individual fields? – Pettis Brandon May 23 '19 at 23:18
  • Its an old login sytem that my friend still uses but wants me to intergrate it into octobercms the first file is user login details the second file is their data it currently works in php so I though I would try it out in octobercms – user2033464 May 24 '19 at 10:29
  • I have added the example xml files do you have any examples on how I would put this data into a model text fields – user2033464 May 24 '19 at 12:02

4 Answers4

1

I was going to just add a comment but this was easier and I am all for the easy route,

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

    };

}
?>

this should do the job

Artful_dodger
  • 698
  • 2
  • 7
  • 26
0

If you would like a model-like interface to interact with an XML file your best bet would be to utilize the included Halcyon (cousin of Eloquent) library. See https://github.com/octobercms/october/blob/wip%2Fhalcyon-db-datasource/modules/cms/classes/Meta.php for an example of a Halcyon model that stores and loads data inside of YAML files. You can use that as a starting point to figure out how you could do the same for XML files.

LukeTowers
  • 1,262
  • 7
  • 14
  • is there any tutorials as I am finding it hard to debug Octobercms – user2033464 May 24 '19 at 10:53
  • This would be considered a fairly advanced thing to do, so there aren't any tutorials for this specific application, but the Slack is super active so you could probably find some help there. – LukeTowers May 27 '19 at 03:01
0

I have been looking at doing something like this myself but haven't had the time but have found an interesting piece of code you might like to try rather than trying to reinvent the wheel a guy called Ante Laca created a script called xmldb you can find it by searching his name or here is a link https://github.com/alaca/xmldb to the file on github it has full CRUD support from what I understand let me know how you get on

Artful_dodger
  • 698
  • 2
  • 7
  • 26
  • Thank you Artful_dodger I have just downloaded this and will give it a go this evening any thoughts on how I should structure this to work in octobercms builder plugin – user2033464 Jun 05 '19 at 17:20
0

I have been looking at this again and I have done this in the past for a clients website

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

                  }
           }

    };

}
?>

and placed this in the code section of the page and the to access this using twig simply do this

{# 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>

you'll need to use the User plugin and amend it to suit your needs i have used the refno_con to iterate over the clients details in your example file hope that this helps

Artful_dodger
  • 698
  • 2
  • 7
  • 26
  • Thank you Artful_dodger for reviewing my quetsion I have tried out you suggestion but noted that there is an error and that is the logged in user name changes but the user details do not I think the error lies with $users = User::whereIsActivated(true)->get(); will need to investigate further – user2033464 Jul 29 '20 at 16:44
  • Oh yes sorry user2033464I have used the wrong method here, this just checks to see if all users are activated let me look into it and I get back – Artful_dodger Jul 30 '20 at 09:19