Skip to content

Commit 2419e44

Browse files
committed
Add basic code for getting form attributes and group #1
Added /forms/:form_id/attributes Added /forms/:form_id/groups Reduces code duplication by reusing attribute() and group() fns
1 parent b5f68cb commit 2419e44

File tree

3 files changed

+144
-27
lines changed

3 files changed

+144
-27
lines changed

application/classes/Controller/Api/Forms.php

+2-24
Original file line numberDiff line numberDiff line change
@@ -207,29 +207,7 @@ public function form($form = NULL)
207207

208208
foreach ($form->form_groups->find_all() as $group)
209209
{
210-
$attributes = array();
211-
foreach ($group->form_attributes->find_all() as $attribute)
212-
{
213-
$attributes[] = array(
214-
'id' => $attribute->id,
215-
'key' => $attribute->key,
216-
'label' => $attribute->label,
217-
'input' => $attribute->input,
218-
'type' => $attribute->type,
219-
'required' => ($attribute->required) ? TRUE : FALSE,
220-
'default' => $attribute->default,
221-
'unique' => ($attribute->unique) ? TRUE : FALSE,
222-
'priority' => $attribute->priority,
223-
'options' => json_decode($attribute->options)
224-
);
225-
}
226-
227-
$response['groups'][] = array(
228-
'id' => $group->id,
229-
'label' => $group->label,
230-
'priority' => $group->priority,
231-
'attributes' => $attributes
232-
);
210+
$response['groups'][] = Controller_API_Forms_Groups::group($group);
233211
}
234212
}
235213
else
@@ -243,4 +221,4 @@ public function form($form = NULL)
243221

244222
return $response;
245223
}
246-
}
224+
}

application/classes/Controller/Api/Forms/Attributes.php

+58-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,69 @@
1717
class Controller_Api_Forms_Attributes extends Ushahidi_Api {
1818

1919
/**
20-
* Retrieve An attribute
20+
* Retrieve an attribute
2121
*
22-
* GET /api/forms/:id/attributes/:id
22+
* GET /api/forms/:form_id/attributes/:id
2323
*
2424
* @return void
2525
*/
2626
public function action_get_index_collection()
2727
{
28-
28+
$form_id = $this->request->param('form_id');
29+
$results = array();
30+
31+
$attributes = ORM::factory('form_attribute')
32+
->order_by('id', 'ASC')
33+
->where('form_id', '=', $form_id)
34+
->find_all();
35+
36+
$count = $attributes->count();
37+
38+
foreach ($attributes as $attribute)
39+
{
40+
$results[] = $this->attribute($attribute);
41+
}
42+
43+
// Respond with attributes
44+
$this->_response_payload = array(
45+
'count' => $count,
46+
'results' => $results
47+
);
48+
}
49+
50+
/**
51+
* Retrieve a single attribute
52+
*
53+
* @param $attribute object - attribute model
54+
* @return array $response
55+
*/
56+
public static function attribute($attribute = NULL)
57+
{
58+
$response = array();
59+
if ( $attribute->loaded() )
60+
{
61+
$response = array(
62+
'id' => $attribute->id,
63+
'key' => $attribute->key,
64+
'label' => $attribute->label,
65+
'input' => $attribute->input,
66+
'type' => $attribute->type,
67+
'required' => ($attribute->required) ? TRUE : FALSE,
68+
'default' => $attribute->default,
69+
'unique' => ($attribute->unique) ? TRUE : FALSE,
70+
'priority' => $attribute->priority,
71+
'options' => json_decode($attribute->options)
72+
);
73+
}
74+
else
75+
{
76+
$response = array(
77+
'errors' => array(
78+
'Attribute does not exist'
79+
)
80+
);
81+
}
82+
83+
return $response;
2984
}
3085
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php defined('SYSPATH') OR die('No direct access allowed.');
2+
3+
/**
4+
* Ushahidi API Forms Groups Controller
5+
*
6+
* PHP version 5
7+
* LICENSE: This source file is subject to GPLv3 license
8+
* that is available through the world-wide-web at the following URI:
9+
* http://www.gnu.org/copyleft/gpl.html
10+
* @author Ushahidi Team <[email protected]>
11+
* @package Ushahidi - http://source.ushahididev.com
12+
* @subpackage Controllers
13+
* @copyright Ushahidi - http://www.ushahidi.com
14+
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
15+
*/
16+
17+
class Controller_API_Forms_Groups extends Ushahidi_API {
18+
19+
/**
20+
* Retrieve a group
21+
*
22+
* GET /api/forms/:form_id/groups/:id
23+
*
24+
* @return void
25+
*/
26+
public function action_get_index_collection()
27+
{
28+
$form_id = $this->request->param('form_id');
29+
$results = array();
30+
31+
$groups = ORM::factory('form_group')
32+
->order_by('id', 'ASC')
33+
->where('form_id', '=', $form_id)
34+
->find_all();
35+
36+
$count = $groups->count();
37+
38+
foreach ($groups as $group)
39+
{
40+
$results[] = $this->group($group);
41+
}
42+
43+
// Respond with groups
44+
$this->_response_payload = array(
45+
'count' => $count,
46+
'results' => $results
47+
);
48+
}
49+
50+
/**
51+
* Retrieve a single group, along with all its attributes
52+
*
53+
* @param $group object - group model
54+
* @return array $response
55+
*/
56+
public static function group($group = NULL)
57+
{
58+
$response = array();
59+
if ( $group->loaded() )
60+
{
61+
$response = array(
62+
'id' => $group->id,
63+
'label' => $group->label,
64+
'priority' => $group->priority,
65+
'attributes' => array()
66+
);
67+
68+
foreach ($group->form_attributes->find_all() as $attribute)
69+
{
70+
$response['attributes'][] = Controller_API_Forms_Attributes::attribute($attribute);
71+
}
72+
}
73+
else
74+
{
75+
$response = array(
76+
'errors' => array(
77+
'Group does not exist'
78+
)
79+
);
80+
}
81+
82+
return $response;
83+
}
84+
}

0 commit comments

Comments
 (0)