Mac Web Based Till System – Keep It In The Family

19 Jan
2011

Sometimes, I need to keep the static site that runs off the G4 updated with live data.

Most notably with web orders and any products that are updated on the live CMS. So, to begin with, I built a bespoke function that would spit out data and a process that would ingest this data. But when I had to build another one, it got me thinking that there must be a better way. And it turns out there is.

Using CakePHP’s Model attribute _schema, I was able to build two controller methods – a getter and a setter with only a few lines of code.

The Getter

In your app_controller.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function get_something($model_name, $since_id=null){
    $this->layout='xml';
    $model_name=ucwords($model_name);
    $this->loadModel($model_name);
    $conditions = array();
    if($since_id){
        $conditions[$model_name.'.id >']=$since_id;
    }
    $things = $this->{$model_name}->find('all', array('conditions'=>$conditions));
    $this->set('things', $things);
    $this->set('model_name', $model_name);
    $this->set('model_schema',$this->{$model_name}->_schema);
    $this->render('/elements/get_something');
}

And your element get_something.ctp

1
2
3
4
5
6
7
8
9
<? header("Content-Type: application/xml; charset=utf-8"); echo '<';?>?xml version="1.0" encoding="utf-8"?><somethingxml>
<? foreach($things as $k=>$thing){ ?>
    <something>
    <? foreach($model_schema as $fieldname=>$attributes){ ?>
        <<?=$fieldname?>><?=$thing[$model_name][$fieldname]?></<?=$fieldname?>>
    <? } ?>
    </something>
<? } ?>
</somethingxml>

The Setter

Again, in your app_controller.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function receive_something($model_name, $should_exit=true){
    $this->layout='xml';
    $model_name=ucwords($model_name);
    $this->loadModel($model_name);
    $latest_thing = $this->{$model_name}->find('first', array('fields'=>'id', 'order'=>'id DESC', 'recursive'=>-1));
    $latest_id = $latest_thing[$model_name]['id'];
    $xml = exec("curl http://yourwebsitedomain.com/somemodelname/get_something/".$model_name.'/'.$latest_id, $return_string);
    $xml = str_replace('\\n','',implode('\n',$xml));
    $xml = $this->xmlparser->superParse($xml);
    $xml = $this->xmlparser->xmlExtract($xml, 'something');
    foreach($xml as $k=>$order){
        $this->{$model_name}->create();
        foreach($this->{$model_name}->_schema as $fieldname=>$attributes){
            $this->data[$model_name][$fieldname] = @$order[$fieldname];
        }
        $this->{$model_name}->save($this->data, false);
    }
   
    if($should_exit===true){
        exit();
    } else {
        return true;
    }
}

(No view required for this one)

And finally, you need an XML parser. the one I have referenced here is one we made in house. You can download it here.

What we just did

We created a getter that gets the information you want and returns it in XML form. Then we made a setter. The setter goes and CURLs the address on your server where the getter lives. It finds the last ID of your local table and requests any items added to the table since then from the getter. It then iterates through the XML and sets data based on the model’s _schema attribute.

Dead easy

Comment Form

top