. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** * Implements hook_mica_datasets_connection_info(). */ function mica_opal_mica_datasets_connection_info() { $connections = array(); $connections['mica_dataset_opal_connection'] = array( 'name' => t('Opal'), 'description' => t('Connection for Opal data repository.'), 'class' => 'MicaDatasetOpalConnectionClass', ); return $connections; } /** * Implements hook_menu_local_tasks_alter(). */ function mica_opal_menu_local_tasks_alter(&$data, $router_item, $root_path) { $links = array(); switch ($root_path) { case 'node/%': $node = $router_item['page_arguments'][0]; if ($node != NULL && $node->type === 'dataset') { if (node_access('update', $node)) { // add variables $links['export-opal'] = array( '#theme' => 'menu_local_action', '#weight' => 6, '#link' => array( 'title' => t('Export an Opal View'), 'href' => 'opal-dataset/node/' . $node->nid, ), ); } } break; } $data['actions']['output'] = array_merge($data['actions']['output'], $links); } function mica_opal_fetch_harmonization_view($dataset_nid, $study_nid) { $connector = mica_dataset_connector_query($dataset_nid, $study_nid); if ($connector){ $opal = new MicaDatasetOpalConnectionClass($connector); $post = new HttpClientRequest($connector->__call("getTableResourceURI", array("variables")), array( 'method' => 'GET', 'headers' => array('Accept' => array('application/json')), 'data' => array(), 'parameters' => array(), )); $raw = $opal->client()->execute($post); $array = json_decode($raw, TRUE); // Support JSON lines format. if (!is_array($array)) { $raw = preg_replace('/}\s*{/', '},{', $raw); $raw = '[' . $raw . ']'; $array = json_decode($raw, TRUE); } if (is_array($array)) { $harmonization = array(); $harmonization['dataset_nid'] = $dataset_nid; $harmonization['study_nid'] = $study_nid; $all_items = mica_opal_jsonPath($array, '$.*'); unset($array); $position = 1; global $user; foreach ($all_items as $item) { $name = array_shift(mica_opal_jsonPath($item, 'name')); $label = array_shift(mica_opal_jsonPath($item, 'attributes[?(@[\'name\']==\'label\')].value')); $value_type = array_shift(mica_opal_jsonPath($item, 'valueType')); $unit = array_shift(mica_opal_jsonPath($item, 'unit')); $repeatable = array_shift(mica_opal_jsonPath($item, 'isRepeatable')); // Explicitely set to 0 to avoid the error when saving an empty field into the database if (empty($repeatable)) { $repeatable = '0'; } $script = array_shift(mica_opal_jsonPath($item, 'attributes[?(@[@name == \'script\' and @namespace == \'opal\'].value')); if (strlen($script) === 0) { $script = array_shift(mica_opal_jsonPath($item, 'attributes[?(@[\'name\']==\'script\')].value')); } $status = array_shift(mica_opal_jsonPath($item, 'attributes[?(@[\'name\']==\'status\'), ?(@[\'namespace\']==\'maelstrom\')].value')); if (empty($status)) { $status = 'undetermined'; } $status = strtolower($status); $description = array_shift(mica_opal_jsonPath($item, 'attributes[?(@[\'name\']==\'description\'), ?(@[\'namespace\']==\'maelstrom\')].value')); $comment = array_shift(mica_opal_jsonPath($item, 'attributes[?(@[\'name\']==\'comment\'), ?(@[\'namespace\']==\'maelstrom\')].value')); $categories_name = mica_opal_jsonPath($item, 'categories.*.name'); $categories_label = mica_opal_jsonPath($item, 'categories.*.attributes[?(@[\'name\']==\'label\')].value'); $categories_missing = mica_opal_jsonPath($item, 'categories.*.isMissing'); $harmonization['variables'][$name] = array( 'title' => $name, 'label' => $label, 'value_type' => $value_type, 'unit' => $unit, 'repeatable' => $repeatable, 'script' => $script, 'status' => $status, 'description' => $description, 'comment' => $comment, ); if (!empty($categories_name)) { for ($i = 0; $i < count($categories_name); $i++) { $harmonization['variables'][$name]['categories'][] = array( 'name' => $categories_name[$i], 'label' => isset($categories_label[$i]) ? $categories_label[$i] : '', 'missing' => $categories_missing[$i] ? '1' : '0', ); } } } return $harmonization; } throw new Exception(t('There was an error decoding the JSON document.')); } return FALSE; } /** * Utilizes the jsonPath function from jsonpath-0.8.1.php * * jsonPath returns false if the expression returns zero results and that will * mess up our for loops, so return an empty array instead. * * TODO Figure out error handling. * * @param $array * The input array to parse * @$expression * The JSONPath expression. * @return array * Returns an array that is the output of jsonPath */ function mica_opal_jsonPath($array, $expression) { $libraries_jsonpath_loc = drupal_get_path('module', 'mica_opal') . '/includes/jsonpath-0.8.1.php'; if (!file_exists($libraries_jsonpath_loc)) { drupal_set_message("Cannot find jsonpath-0.8.1.php", "error"); return NULL; } include_once $libraries_jsonpath_loc; $result = jsonPath($array, $expression); return ($result === FALSE) ? array() : $result; }