formsrv_2.module 3.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/**
 * Created by IntelliJ IDEA.
 * User: abuddenberg
 * Date: 8/22/13
 * Time: 11:58 AM
 * To change this template use File | Settings | File Templates.
 */

/**
 * Implementation of hook_help().
 */
function formsrv_help($path, $arg) {
    $output = '';
    switch ($path) {
        case "admin/help#formsrv":
            $output = '<p>'.  t("Provides RESTful endpoint for serving out Webform submissions") .'</p>';
            break;
    }
    return $output;
}

/**
 * Implementation of hook_menu().
 */
function formsrv_menu() {
27
28
29
30
31
32
33
34
//    $items['metadata/figures/all'] = array(
//        'page callback' => 'formsrv_figure_metadata_json',
//        'type' => MENU_CALLBACK,
//        'access arguments' => array('access content'),
//    );

    $items['metadata/figures'] = array(
        'page callback' => 'formsrv_figure_metadata_json',
35
36
37
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
    );
38
39
40
41
42
43
44

    $items['metadata/list'] = array(
        'page callback' => 'formsrv_figure_list_json',
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
    );

45
46
47
    return $items;
}

48
49
50
51
52
53
/**
 * Renders a list of figure node IDs with a basic descriptions in JSON.
 */
function formsrv_figure_list_json(){
    $q = <<<END
        #Query to give brief listing and description of figures in Drupal
54
55
56
57
58
59
60
        SELECT
          n.nid,
          n.title,
          ci.field_metadata_t1_value as t1
        FROM {node} n
          JOIN {content_type_chapter_image} ci ON n.nid = ci.nid AND n.vid = ci.vid
        WHERE n.type = 'chapter_image'
61
END;
62
63
64
65
66
    $list = array();

    $result = db_query($q);
    while($row = $result->fetch_assoc()){
        $list[] = array(
67
68
69
70
            'nid' => $row['nid'],
            'node_title' => $row['title'],
            'figure_name' => json_decode($row['t1'])->{'figure'}->{'graphics_title'},
            'url' => url('metadata/figures/'.$row['nid'])
71
72
        );
    }
73
74
    drupal_set_header('Content-Type: application/json');
    echo json_encode($list);
75
76
77
78
79
80
81
}

/**
 * Creates and populates a tree-like data structure of figures and images with their related datasources.
 * If the node ID of the figure from trh
 * Renders this data structure as JSON.
 */
82
function formsrv_2_figure_metadata_json($image_nid_query = ''){
83
    $q = <<<END
84
85
86
87
        SELECT
          n.nid,
          ci.field_metadata_t1_value as t1,
          ci.field_metadata_t2_value as t2,
88
          ci.field_cr_permission_status_value as copyright,
89
90
91
92
93
          f.filepath
        FROM {node} n
          JOIN {content_type_chapter_image} ci ON n.nid = ci.nid AND n.vid = ci.vid
          JOIN {files} f ON f.fid = ci.field_chapter_image_image_fid
        WHERE n.nid = %d;
94
95
END;

96
    $list = array();
97

98
    $result = db_query($q, $image_nid_query);
99
    while($row = $result->fetch_assoc()){
100
101
        $list[] = array(
            'nid' => $row['nid'],
102
            'copyright' => $row['copyright'],
103
104
105
106
            't1' => json_decode($row['t1']),
            't2' => json_decode($row['t2']),
            'filepath' => $row['filepath']
        );
107
108
    }

109
110
111
    drupal_set_header('Content-Type: application/json');
    echo json_encode($list);
}