Hello, I'm looking up odk collect forms to php tu me some pointers you could help with the code you uploaded does not work
···
On Tuesday, July 3, 2012 12:39:32 PM UTC-5, fleen wrote:
> Hi.
>
> ODK has been good to me, so I thought I'd post this on the chance it may save someone else some time.
>
> There's a lot of ugliness in this code, but it works.
>
> Thanks for all the work,
>
> Fergus
>
>
> <?php
>
>
> /**
> * Cakephp Controller to receive forms from ODK Collect
> * @author Fergus Leen (fergus at fleetingmeeting.com)
> * @date June 2012
> * Based on odk_server.py by alchemist at google.com (Sean Askay)
> *
> *
> */
>
> App :: import('Controller', 'Xform');
> /**
> * NOTE: Extends controller so bypasses Auth
> *
> */
> class XformserverController extends Controller {
>
> var $name = 'Xformserver';
>
> /**Simple cake model to hold data*/
> var $uses = array (
> 'Xform'
> );
>
> var $helpers = array (
> 'Html',
> 'Ajax',
> 'Javascript',
> 'Session'
> );
>
> var $logfile = 'xformserver';
>
> //override auth stuff. We rely on htaccess for this.
> var $components = array (
> 'RequestHandler'
> );
>
> function beforeFilter() {
>
> }
>
> /*used for testing*/
> function index() {
>
> }
>
> /**
> * This function takes an xml file uploaded from Locality Collect and:
> * Calculates the uid from the flat number, housenumber and postcode
> * saves the device ID, uid, form type and related property_id to the db.
> *
> * TODO: Null checks
> *
> */
> function addFromCollect($file) {
> //load xml from file
> App :: import('Xml');
> $xml_obj = & new XML($file);
> //set empty namespace
> $xml_obj->children[0]->attributes['xmlns'] = '';
> $parsed_xml = Set :: reverse($xml_obj);
> // set up the data and save. Customise this for your own needs.
> //debug($parsed_xml);
> foreach ($parsed_xml as $form) {
> $this->Xform->create();
> $data['Xform']['form_type'] = $form['id'];
> $data['Xform']['device_id'] = $form['deviceid'];
> $data['Xform']['xform_filename'] = $file;
> $data['Xform']['xml'] = $xml_obj->toString();
> $data['Xform']['flat_no'] = ($form['GroupAddress']['address_flat'] != null ? $form['GroupAddress']['address_flat'] : '');
> $data['Xform']['building_no'] = $form['GroupAddress']['address_building'];
> $data['Xform']['street'] = $form['GroupAddress']['address_street'];
> $data['Xform']['city'] = $form['GroupAddress']['address_town'];
> $data['Xform']['postcode'] = $form['GroupAddress']['post_code'];
> $data['Xform']['property_uid'] = $this->buildPropertyUID($form['GroupAddress']['address_flat'], $form['GroupAddress']['address_building'], $form['GroupAddress']['post_code']);
> if ($this->Xform->save($data)) {
> //$this->Session->setFlash(__('The form has been saved', true));
>
> } else {
> $this->log('Unable to add to db ' . print_r($form, true) . ' ' . print_r($data, true) . ' ' . $file, $this->logfile);
>
> }
>
> }
> }
>
> function buildPropertyUID($flat, $building, $postcode) {
> if (empty ($flat)) {
> $flat = "";
> }
> return (str_pad($flat, 4, '0', STR_PAD_LEFT) . str_pad($building, 4, '0', STR_PAD_LEFT) . strtoupper(str_replace(' ', '', $postcode)));
>
> }
>
> /**
> * formlist.ctp view looks like this:
> *
> <?php
> foreach ($forms as $form){
> echo "$form\n";
> }
> ?>
>
> */
> function formList() {
> Configure :: write('debug', 0);
> $forms_dir = WWW_ROOT . "xforms/";
> $dir = new Folder($forms_dir);
> $files = $dir->read(true, true);
> $this->layout = 'xml';
> $this->set('forms', $files['1']);
> }
>
> /**
> * This function accepts a form from Collect
> It must be able to bypass authentication
> *
> **/
> function submission() {
> Configure :: write('debug', 0);
> $this->log("Connection from " . $this->RequestHandler->getClientIP(), $this->logfile);
> $target_path = WWW_ROOT . "uploads/";
> $error = false;
> $this->layout = null;
> $this->log(file_get_contents("php://input"), $this->logfile);
> if (empty ($_FILES)) {
> $this->header('HTTP/1.1 204 No Content');
> die();
> }
>
> foreach ($_FILES as $file) {
> $this->log('found ' . print_r($file, true), $this->logfile);
> $final_path = $target_path . basename($file['name']);
> if (move_uploaded_file($file['tmp_name'], $final_path)) {
> $file = new File($final_path);
> if (strcmp($file->ext(), 'xml') == 0)
> $this->addFromCollect($final_path);
> } else {
> $this->log("There was an error uploading the file " . basename($file['name']), $this->logfile);
> $error = true;
> }
> }
>
> if (!$error) {
> $this->header('HTTP/1.1 201 Created');
> } else {
> $this->header('HTTP/1.1 204 No Content');
>
> }
> die();
> }
>
> }
> ?>