/[drupal]/contributions/modules/kt/kt_api/kt_api.module
ViewVC logotype

Diff of /contributions/modules/kt/kt_api/kt_api.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Tue Aug 18 09:06:51 2009 UTC revision 1.1.2.1, Tue Aug 18 09:06:51 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * Implements a REST API to an external Knowledgetree Installation
7     */
8    
9    require_once drupal_get_path('module', 'kt') .'/includes/KnowledgeTreeService_REST.php';
10    
11    function kt_api_menu() {
12      $items['admin/settings/kt'] = array(
13        'title' => t('KnowledgeTree integration'),
14        'description' => t('Configure the integration with KnowledgeTree'),
15        'page callback' => 'drupal_get_form',
16        'page arguments' => array('kt_api_settings'),
17        'access arguments' => array('administer knowledge tree'),
18        'type' => MENU_NORMAL_ITEM,
19      );
20      $items['admin/settings/kt/api'] = array(
21        'title' => t('KnowledgeTree API Settings'),
22        'description' => t('Configure the integration with KnowledgeTree'),
23        'type' => MENU_DEFAULT_LOCAL_TASK,
24        'access arguments' => array('administer knowledge tree'),
25        'weight' => 0,
26      );
27      $items['admin/settings/kt/test'] = array(
28        'title'  => "KT REST Test",
29        'page callback' => 'kt_api_page',
30        'page arguments' => array(1),
31        'access arguments' => array('administer knowledge tree'),
32        'type' => MENU_LOCAL_TASK,
33        'weight' => 5,
34      );
35    
36      return $items;
37    }
38    
39    /**
40     * Implementation of hook_perm().
41     */
42    function kt_api_perm() {
43      return array("administer knowledge tree");
44    }
45    
46    function kt_api_settings() {
47    
48      $form = array();
49    
50      $form['kt_root_url'] = array(
51        '#type' => 'textfield',
52        '#title' => t('Knowledgetree root URL'),
53        '#default_value' => variable_get('kt_root_url', 'http://localhost/kt/'),
54        '#description' => t("The root URL of your Knowledgtree Installation, including a trailing slash. eg. http://localhost/kt/"),
55        '#required' => TRUE,
56      );
57    
58      $form['kt_login'] = array(
59        '#type' => 'fieldset',
60        '#title' => t('KnowledgeTree Authentication'),
61        '#description' => t('Authentication details for accessing KT from Drupal via REST.  <strong>Best to create a new user in KT with restricted permissions. Whatever this user can access in KT will be accessible to all drupal users.</strong>'),
62        '#collapsible' => TRUE,
63        '#collapsed' => FALSE,
64      );
65    
66      $form['kt_login']['kt_username'] = array(
67        '#type' => 'textfield',
68        '#title' => t('Knowledgetree Login Username'),
69        '#default_value' => variable_get('kt_username', 'drupal'),
70        '#size' => 20,
71        '#maxlength' => 50,
72        '#description' => t("The username drupal will use to access Knowledgetree."),
73      );
74    
75      $form['kt_login']['kt_password'] = array(
76        '#type' => 'password',
77        '#title' => t('Knowledgetree Login Password'),
78        '#size' => 20,
79        '#maxlength' => 255,
80        '#description' => t("The password drupal will use to access Knowledgetree."),
81      );
82    
83      $session_timeout_options = array(
84        0 => t("Do not cache session"),
85        30 => t("Cache session for 30 sec"),
86        60 => t("Cahce session for 1 min"),
87        300 => t("Cache session for 5 min"),
88        1800 => t("Cache session for 30 min"),
89      );
90    
91      $form['kt_login']['kt_session_cache'] = array(
92        '#type' => 'select',
93        '#title' => t('KnowledgeTree Session Caching'),
94        '#description' => t('How long should Drupal cache the KT session. The KT default timeout for inactivity is 1200 seconds (See KT General Settings page). If this cache timeout is set to a higher number than KTs session timeout, errors may occur.'),
95        '#default_value' => variable_get('kt_session_cache', 300),
96        '#options' => $session_timeout_options,
97        '#required' => TRUE,
98      );
99    
100      $form['kt_debug'] = array(
101        '#type' => 'checkbox',
102        '#title' => t('KT REST Debug'),
103        '#default_value' => variable_get('kt_debug', 0),
104        '#description' => t('Print verbose debug information on each REST call'),
105      );
106    
107      return system_settings_form($form);
108    }
109    
110    function kt_api_page() {
111    
112      $page = "";
113    
114      $kt = new Drupal_KT_REST;
115      $kt->login();
116    
117      $document = $kt->get_document_detail("1", "");
118      $document = _kt_xmlobj2array($document);
119      $page = '<p>If this test is successful you should see an array with document details matching the document with ID#1 in KnowledgeTree:'."\n<pre>". print_r($document, TRUE) .'</pre>';
120    
121      //$response = $kt->download_document("1");
122      //$page = kpr($response,TRUE);
123    
124      return $page;
125    }
126    
127    /**
128     * Converts an XML object (such as the one returned by SimpleXML::simplexml_load_string)
129     * into a simple array
130     *
131     */
132    function _kt_xmlobj2array($data) {
133      if (is_object($data)) {
134        foreach (get_object_vars($data) as $key => $val) {
135          $ret[$key] = _kt_xmlobj2array($val);
136        }
137        return $ret;
138      }
139      elseif (is_array($data)) {
140        foreach ($data as $key => $val) {
141          $ret[$key] = _kt_xmlobj2array($val);
142        }
143        return $ret;
144      }
145      else {
146        return $data;
147      }
148    }
149    
150    
151    
152    class Drupal_KT_REST extends KnowledgeTreeRESTService {
153    
154      function login() {
155    
156        // Check for a cached session first
157        $cached_session=cache_get('kt_session', 'cache');
158        if (isset($cached_session->data) && ($cached_session->expire > time()) && variable_get('kt_session_cache', 0)) {
159          $this->session = $cached_session->data;
160          $this->log('KT REST: Using cached session :'. print_r($this->session, TRUE), 'DEBUG');
161        }
162        else { // No cached session exists or cached session expired, use regular login call
163    
164          cache_clear_all('kt_session', 'cache'); // Clear old cached session, if any
165    
166          if (($kt_url = variable_get('kt_root_url', '')) == '') {
167            $this->log('KT REST: No KT root URL set. Check the Knowledgetree REST API Settings.', 'ERROR');
168          }
169          if (($kt_user = variable_get('kt_username', '')) == '') {
170            $this->log('KT REST: No KT username set. Check the Knowledgetree REST API Settings.', 'ERROR');
171          }
172          if (($kt_pass = variable_get('kt_password', '')) == '') {
173            $this->log('KT REST: No KT password set. Check the Knowledgetree REST API Settings.', 'ERROR');
174          }
175          parent::login($kt_url .'ktwebservice/KTWebService.php', $kt_user, $kt_pass);
176          if (isset($this->session['session_id']) && $this->session['session_id'] != 0) {
177            // Cache the new session id to keep persistent session with timeout
178            cache_set("kt_session", $this->session, "cache", time() + variable_get('kt_session_cache', 0));
179          }
180          else { // parent::login failed
181            return FALSE;
182          }
183        }
184        return TRUE;
185      }
186    
187      function log($message, $level='DEBUG') {
188        if (is_string($message) && !empty($message)) {
189          switch ($level) {
190            case 'ERROR':
191              watchdog('kt_api', $message, WATCHDOG_ERROR);
192              if (variable_get('kt_debug', 0)) {
193                drupal_set_message('<pre>'. $message .'</pre>', 'error');
194              }
195              break;
196            case 'WARNING':
197            case 'DEBUG':
198              if (variable_get('kt_debug', 0)) {
199                drupal_set_message('<pre>'. $message .'</pre>', 'info');
200              }
201              break;
202          }
203        }
204      }
205    
206    
207    } // end class

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2