View source
<?php
function webform_service_library() {
return array(
'webform_service' => array(
'title' => t('Webform Service JavaScript API'),
'version' => '0.1',
'js' => array(
drupal_get_path('module', 'webform_service') . '/js/webform_service_crud.js' => array(),
),
),
);
}
function webform_service_services_resources() {
require_once "resources/webform_resource.inc";
require_once "resources/submission_resource.inc";
$resources = array();
$resources += _webform_resource_definition();
$resources += _submission_resource_definition();
return $resources;
}
function webform_service_submission_access($op = 'view', $args = array()) {
if (empty($args[0])) {
return services_error(t('Must provide a uuid.'), 404);
}
if ($op == 'create') {
if (is_array($args[0]) || is_object($args[0])) {
$webform = (object) $args[0];
}
else {
if (in_array(gettype($args[0]), array(
'string',
'integer',
))) {
$webform = webform_service_resource_load($args[0]);
}
}
$webform = webform_node_view($webform, 'node');
return empty($webform['#enabled']);
}
else {
$webform = webform_submission_uuid_webform($args[0]);
$submission = webform_submission_uuid_submission($args[0]);
return webform_submission_access($webform, $submission, $op);
}
}
function webform_service_resource_access($op = 'view', $type = '', $args = array()) {
$node = null;
if (empty($args[0])) {
return services_error(t('Must provide a uuid.'), 404);
}
else {
if (is_array($args[0]) || is_object($args[0])) {
$node = (object) $args[0];
}
else {
if (in_array(gettype($args[0]), array(
'string',
'integer',
))) {
$node = webform_service_resource_load($args[0]);
}
}
}
if ($node) {
$node->type = $type;
module_load_include('inc', 'services', 'resources/node_resource');
return _node_resource_access($op, array(
$node,
));
}
else {
return services_error(t('Webform @uuid could not be found', array(
'@uuid' => $args[0],
)), 404);
}
}
function webform_service_field_value($entity, $field_name, $value_name = 'value') {
$items = field_get_items('node', $entity, $field_name);
if (isset($items[0])) {
if (isset($items[0][$value_name])) {
return $items[0][$value_name];
}
else {
return $items[0];
}
}
return '';
}
function webform_service_get_components($webform) {
$components = array();
foreach ($webform['components'] as $cid => $component) {
if (isset($component['extra']['items'])) {
$items = preg_split("/\r\n|\n|\r/", $component['extra']['items']);
$component['extra']['items'] = array();
foreach ($items as $item) {
$parts = explode('|', $item);
$key = $parts[0];
$value = isset($parts[1]) ? $parts[1] : $key;
$component['extra']['items'][$key] = $value;
}
}
$components[$cid] = $component;
}
return $components;
}
function webform_service_get_conditionals($webform) {
$conditionals = array();
foreach ($webform['conditionals'] as $cid => $conditional) {
$conditionals[$cid] = $conditional;
}
return $conditionals;
}
function webform_service_get_webform($entity) {
$webform = array();
if (isset($entity->webform)) {
$webform = array(
'components' => webform_service_get_components($entity->webform),
'conditionals' => webform_service_get_conditionals($entity->webform),
);
}
return $webform;
}
function webform_service_get_resource($entity) {
$resource = array();
if (gettype($entity) === 'string') {
$entity = webform_service_resource_load($entity);
}
if ($entity) {
$uri = services_resource_uri(array(
'webform',
$entity->uuid,
));
$resource = (object) array(
'uri' => services_resource_uri(array(
'webform',
$entity->uuid,
)),
'nid' => $entity->nid,
'uuid' => $entity->uuid,
'title' => $entity->title,
'description' => webform_service_field_value($entity, 'body'),
'created' => $entity->created,
'updated' => $entity->changed,
'submissions' => $uri . '/submissions',
'webform' => webform_service_get_webform($entity),
);
}
return $resource;
}
function webform_service_parse_submission($webform, $submission) {
global $user;
$webform = (object) $webform;
$submission = (object) $submission;
if (isset($submission->submission)) {
$submission = (object) $submission->submission;
}
$data = array();
foreach ($submission->data as $cid => $values) {
$data[$cid] = $values['values'];
}
$submission_obj = (object) array(
'nid' => !empty($submission->nid) ? $submission->nid : $webform->nid,
'uid' => !empty($submission->uid) ? $submission->uid : $user->uid,
'submitted' => !empty($submission->submitted) ? $submission->submitted : REQUEST_TIME,
'remote_addr' => !empty($submission->remote_addr) ? $submission->remote_addr : ip_address(),
'is_draft' => 0,
'data' => $data,
);
if (!empty($submission->sid)) {
$submission_obj->sid = $submission->sid;
}
if (!empty($submission->uuid)) {
$submission_obj->uuid = $submission->uuid;
}
if (!empty($submission->completed)) {
$submission_obj->completed = $submission->completed;
}
if (!empty($submission->modified)) {
$submission_obj->modified = $submission->modified;
}
drupal_alter('webform_service_submission', $submission_obj, $submission);
return $submission_obj;
}
function webform_service_get_submission_by_uuid($uuid) {
$webform = webform_submission_uuid_webform($uuid);
$submission = webform_submission_uuid_submission($uuid);
return webform_service_get_submission($webform, $submission);
}
function webform_service_get_submission($webform, $submission) {
$return = array();
if ($webform && $submission) {
$components = webform_service_get_components($webform->webform);
$values = array();
foreach ($components as $cid => $component) {
$values[$cid] = array(
'form_key' => $component['form_key'],
'cid' => $component['cid'],
'type' => $component['type'],
);
if (isset($submission->data[$component['cid']])) {
$values[$cid]['values'] = $submission->data[$component['cid']];
}
}
$account = user_load($submission->uid);
$submissionHasUuid = isset($submission->uuid);
$return = array(
'sid' => $submission->sid,
'uuid' => $submissionHasUuid ? $submission->uuid : NULL,
'uri' => $submissionHasUuid ? services_resource_uri(array(
'submission',
$submission->uuid,
)) : NULL,
'webform' => services_resource_uri(array(
'webform',
$webform->uuid,
)),
'user' => $account->uuid,
'submitted' => $submission->submitted,
'data' => $values,
);
}
return $return;
}
function webform_service_submission_index($uuid, $page, $page_size, $parameters) {
global $user;
if ($webform = webform_service_resource_load($uuid)) {
$index = array();
module_load_include('inc', 'webform', 'includes/webform.submissions');
$parameters['nid'] = $webform->nid;
if (user_access('access all webform results')) {
}
else {
if (user_access('access own webform submissions')) {
$parameters['uid'] = $user->uid;
}
else {
return services_error(t('User @userid does not have access to submissions', array(
'@userid' => $user->uid,
)), 401);
}
}
$submissions = webform_get_submissions($parameters);
foreach ($submissions as $submission) {
$index[] = webform_service_get_submission($webform, $submission);
}
return $index;
}
else {
return services_error(t('@uuid could not be found', array(
'@uuid' => $uuid,
)), 404);
}
}
function webform_service_resource_index($type, $result) {
$items = array();
if (!empty($result['node'])) {
$entities = entity_load('node', array_keys($result['node']));
foreach ($entities as $entity) {
$item = webform_service_get_resource($entity);
if ($item) {
$items[] = $item;
}
}
}
return $items;
}
function webform_service_resource_load($uuid, $reset = FALSE) {
$webforms =& drupal_static(__FUNCTION__);
if ($reset || empty($webforms[$uuid])) {
$entity = entity_uuid_load('node', array(
$uuid,
), array(), $reset);
$webforms[$uuid] = reset($entity);
}
return empty($webforms[$uuid]) ? NULL : $webforms[$uuid];
}
function webform_service_set_webform_values(&$webform, $values, $force_set = FALSE) {
foreach ($values as $param => $value) {
if (is_array($value)) {
if (!isset($webform[$param])) {
$webform[$param] = array();
}
webform_service_set_webform_values($webform[$param], $value, $force_set);
}
else {
if (!$force_set || !isset($webform[$param])) {
$webform[$param] = $value;
}
}
}
}
function webform_service_webform_prepare($webform, &$node = null) {
$webform = (array) $webform;
if ($node) {
foreach ($webform['webform'] as $param => $value) {
if (!is_array($value)) {
$node->webform[$param] = $value;
}
}
}
webform_service_set_webform_values($webform['webform'], webform_node_defaults(), TRUE);
if (!empty($webform['webform']['components'])) {
module_load_include('inc', 'webform', 'includes/webform.components');
$weight = 0;
foreach ($webform['webform']['components'] as &$component) {
if (!empty($component['type'])) {
if (empty($component['cid'])) {
$component['pid'] = empty($components['pid']) ? 0 : $components['pid'];
$component['form_key'] = $component['form_key'];
$component['weight'] = empty($components['weight']) ? $weight++ : $components['weight'];
webform_component_defaults($component);
}
if ($node) {
if (empty($component['cid'])) {
$cid = max(array_keys($node->webform['components'])) + 1;
$component['nid'] = $node->nid;
}
else {
$cid = $component['cid'];
}
$node->webform['components'][$cid] = $component;
}
}
}
}
return $webform;
}
function webform_service_node_submit($node, $form, &$form_state) {
$break = TRUE;
}
function webform_service_validate_parameters(&$page_size) {
$default_pagesize = variable_get("services_node_index_page_size", 20);
if (!user_access('perform unlimited index queries') && $page_size > $default_pagesize) {
$page_size = $default_pagesize;
}
}