Skip to content

Commit bad9c9b

Browse files
raphaeltravissMateu Aguiló Bosch
authored and
Mateu Aguiló Bosch
committed
Issue #196: Added data provider for variables.
Also adds support for drupal_write_record on DB query.
1 parent 0aaa911 commit bad9c9b

15 files changed

+717
-165
lines changed

modules/restful_example/plugins/restful/db_query/variable/1.0/RestfulQueryVariable.class.php

-24
This file was deleted.

modules/restful_example/plugins/restful/db_query/variable/1.0/variable__1_0.inc

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains RestfulEntityBase.
6+
*/
7+
8+
class RestFulWatchdogResource extends \RestfulDataProviderDbQuery implements \RestfulDataProviderDbQueryInterface {
9+
public function publicFieldsInfo() {
10+
11+
$public_fields['log_id'] = array(
12+
'property' => 'wid',
13+
);
14+
15+
$public_fields['log_type'] = array(
16+
'property' => 'type',
17+
);
18+
19+
$public_fields['log_text'] = array(
20+
'property' => 'message',
21+
);
22+
23+
$public_fields['log_variables'] = array(
24+
'property' => 'variables',
25+
);
26+
27+
$public_fields['log_level'] = array(
28+
'property' => 'severity',
29+
);
30+
31+
$public_fields['log_path'] = array(
32+
'property' => 'location',
33+
);
34+
35+
return $public_fields;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function access() {
42+
$account = $this->getAccount();
43+
return user_access('view site reports', $account);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$plugin = array(
4+
'label' => t('Watchdog entries'),
5+
'resource' => 'watchdog',
6+
'name' => 'watchdog',
7+
'data_provider_options' => array(
8+
'table_name' => 'watchdog',
9+
'id_column' => 'wid',
10+
),
11+
'description' => t('Expose watchdog entries to the REST API.'),
12+
'class' => 'RestfulWatchdogResource',
13+
'authentication_types' => TRUE,
14+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \RestfulQueryVariable
6+
*/
7+
8+
class RestfulVariableResource extends \RestfulDataProviderVariable {
9+
10+
/**
11+
* {@inheritdoc}
12+
*/
13+
public function publicFieldsInfo() {
14+
return array(
15+
'variable_name' => array(
16+
'property' => 'name',
17+
),
18+
'variable_value' => array(
19+
'property' => 'value',
20+
),
21+
);
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function access() {
28+
$account = $this->getAccount();
29+
return user_access('adminsiter site configuration', $account);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
$plugin = array(
4+
'label' => t('Variable'),
5+
'description' => t('Expose site variables to the REST API.'),
6+
'resource' => 'variables',
7+
'class' => 'RestfulVariableResource',
8+
'authentication_types' => TRUE,
9+
'render_cache' => array(
10+
'render' => TRUE,
11+
),
12+
);

modules/restful_token_auth/tests/RestfulTokenAuthenticationTestCase.test

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ class RestfulTokenAuthenticationTestCase extends DrupalWebTestCase {
1111
return array(
1212
'name' => 'Token Authentication',
1313
'description' => 'Test the request authentication with a token.',
14-
'group' => 'Restful',
14+
'group' => 'RESTful',
1515
);
1616
}
1717

1818
function setUp() {
1919
parent::setUp('restful_example', 'restful_token_auth', 'entityreference');
20-
restful_create_field_refresh_token();
2120
}
2221

2322
/**

plugins/restful/RestfulDataProviderDbQuery.php

+61-49
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ public function setTableName($table_name) {
7575
$this->tableName = $table_name;
7676
}
7777

78+
/**
79+
* @return string
80+
**/
81+
public function getPrimary() {
82+
return $this->primary;
83+
}
84+
85+
/**
86+
* @param string $primary
87+
**/
88+
public function setPrimary($primary) {
89+
$this->primary = $primary;
90+
}
91+
7892
/**
7993
* Constructs a RestfulDataProviderDbQuery object.
8094
*
@@ -112,7 +126,9 @@ public function defaultSortInfo() {
112126
$sorts = array();
113127
foreach ($this->getIdColumn() as $column) {
114128
if (!empty($this->getPublicFields[$column])) {
129+
// Sort by the first ID column that is a public field.
115130
$sorts[$column] = 'ASC';
131+
break;
116132
}
117133
}
118134
return $sorts;
@@ -230,7 +246,8 @@ public function getQueryCount() {
230246
public function getTotalCount() {
231247
return intval($this
232248
->getQueryCount()
233-
->execute());
249+
->execute()
250+
->fetchField());
234251
}
235252

236253
/**
@@ -341,22 +358,20 @@ protected function replace($id) {
341358
* {@inheritdoc}
342359
*/
343360
public function update($id, $full_replace = FALSE) {
344-
$query = db_update($this->getTableName());
345-
foreach ($this->getIdColumn() as $index => $column) {
346-
$query->condition($column, current($this->getColumnFromIds(array($id), $index)));
347-
}
348-
349361
// Build the update array.
350362
$request = $this->getRequest();
351363
static::cleanRequest($request);
352364
$save = FALSE;
353365
$original_request = $request;
354366

355367
$public_fields = $this->getPublicFields();
356-
$fields = array();
368+
369+
$id_columns = $this->getIdColumn();
370+
371+
$record = array();
357372
foreach ($public_fields as $public_field_name => $info) {
373+
// Ignore passthrough public fields.
358374
if (!empty($info['create_or_update_passthrough'])) {
359-
// Allow passing the value in the request.
360375
unset($original_request[$public_field_name]);
361376
continue;
362377
}
@@ -365,62 +380,67 @@ public function update($id, $full_replace = FALSE) {
365380
if ($this->isPrimaryField($info['property'])) {
366381
continue;
367382
}
368-
// Check if the public property is set in the payload.
369-
if (!isset($request[$public_field_name])) {
370-
if ($full_replace) {
371-
$fields[$info['property']] = NULL;
372-
}
383+
384+
if (isset($request[$public_field_name])) {
385+
$record[$info['property']] = $request[$public_field_name];
373386
}
374-
else {
375-
$fields[$info['property']] = $request[$public_field_name];
387+
// For unset fields on full updates, pass NULL to drupal_write_record().
388+
elseif ($full_replace) {
389+
$record[$info['property']] = NULL;
376390
}
377391

378392
unset($original_request[$public_field_name]);
379393
$save = TRUE;
380394
}
381395

396+
// No request was sent.
382397
if (!$save) {
383-
// No request was sent.
384398
throw new \RestfulBadRequestException('No values were sent with the request.');
385399
}
386400

387-
if ($original_request) {
388-
// Request had illegal values.
401+
// If the original request is not empty, then illegal values are present.
402+
if (!empty($original_request)) {
389403
$error_message = format_plural(count($original_request), 'Property @names is invalid.', 'Property @names are invalid.', array('@names' => implode(', ', array_keys($original_request))));
390404
throw new \RestfulBadRequestException($error_message);
391405
}
392406

393-
// Once the update array is built, execute the query.
394-
$query->fields($fields)->execute();
407+
// Add the id column values into the record.
408+
foreach ($this->getIdColumn() as $index => $column) {
409+
$record[$column] = current($this->getColumnFromIds(array($id), $index));
410+
}
411+
412+
// Once the record is built, write it.
413+
if (!drupal_write_record($this->getTableName(), $record, $id_columns)) {
414+
throw new \RestfulServiceUnavailable('Record could not be updated to the database.');
415+
}
395416

396417
// Clear the rendered cache before calling the view method.
397418
$this->clearRenderedCache(array(
398419
'tb' => $this->getTableName(),
399420
'cl' => implode(',', $this->getIdColumn()),
400421
'id' => $id,
401422
));
402-
return $this->view($id, TRUE);
423+
424+
return $this->view($id);
403425
}
404426

405427
/**
406428
* {@inheritdoc}
407429
*/
408430
public function create() {
409-
$query = db_insert($this->getTableName());
410-
411-
// Build the update array.
412431
$request = $this->getRequest();
413432
static::cleanRequest($request);
414433
$save = FALSE;
415434
$original_request = $request;
416435

417436
$public_fields = $this->getPublicFields();
418-
$fields = array();
419-
$id_values = array_fill(0, count($this->getIdColumn()), FALSE);
437+
$id_columns = $this->getIdColumn();
420438

439+
440+
$record = array();
421441
foreach ($public_fields as $public_field_name => $info) {
442+
// Ignore passthrough public fields.
422443
if (!empty($info['create_or_update_passthrough'])) {
423-
// Allow passing the value in the request.
424444
unset($original_request[$public_field_name]);
425445
continue;
426446
}
@@ -431,46 +451,38 @@ public function create() {
431451
continue;
432452
}
433453

434-
// Check if the public property is set in the payload.
435-
if (($index = array_search($info['property'], $this->getIdColumn())) !== FALSE) {
436-
$id_values[$index] = $request[$public_field_name];
437-
}
438-
439454
if (isset($request[$public_field_name])) {
440-
$fields[$info['property']] = $request[$public_field_name];
455+
$record[$info['property']] = $request[$public_field_name];
441456
}
442457

443458
unset($original_request[$public_field_name]);
444459
$save = TRUE;
445460
}
446461

462+
// No request was sent.
447463
if (!$save) {
448-
// No request was sent.
449464
throw new \RestfulBadRequestException('No values were sent with the request.');
450465
}
451466

452-
if ($original_request) {
453-
// Request had illegal values.
467+
// If the original request is not empty, then illegal values are present.
468+
if (!empty($original_request)) {
454469
$error_message = format_plural(count($original_request), 'Property @names is invalid.', 'Property @names are invalid.', array('@names' => implode(', ', array_keys($original_request))));
455470
throw new \RestfulBadRequestException($error_message);
456471
}
457472

458-
$passed_id = NULL;
459-
460-
// If we have the full primary key passed use it.
461-
if (count(array_filter($id_values)) == count($id_values)) {
462-
$passed_id = implode(self::COLUMN_IDS_SEPARATOR, $id_values);
463-
}
473+
// Once the record is built, write it and view it.
474+
if (drupal_write_record($this->getTableName(), $record)) {
475+
// Handle multiple id columns.
476+
$id_values = array();
477+
foreach ($id_columns as $id_column) {
478+
$id_values[$id_column] = $record[$id_column];
479+
}
480+
$id = implode(self::COLUMN_IDS_SEPARATOR, $id_values);
464481

465-
// Once the update array is built, execute the query.
466-
if ($id = $query->fields($fields)->execute()) {
467-
return $this->view($id, TRUE);
482+
return $this->view($id);
468483
}
484+
return;
469485

470-
// Some times db_insert() does not know how to get the ID.
471-
if ($passed_id) {
472-
return $this->view($passed_id);
473-
}
474486
}
475487

476488
/**

0 commit comments

Comments
 (0)