Source for file contract.php

Documentation is available at contract.php

  1. <?php
  2. /**
  3. * @package helpdesk
  4. * Copyright OSI 2005. All rights reserved.
  5. * It is expected that version 1.0 will be GPLed, but this interim work
  6. * is kept closed until 1.0 is ready.
  7. */
  8.  
  9. $_contract_saved_er = error_reporting(E_ALL || E_STRICT) ;
  10.  
  11. /**
  12. * import ancillary functions
  13. */
  14. require_once("misc.php");
  15.  
  16. /**
  17. * import HD core
  18. */
  19. require_once("core.php");
  20.  
  21. /**
  22. * Support contract
  23. * @package helpdesk
  24. */
  25. class helpdeskContract extends helpdeskObject
  26. {
  27. public $node ; ///< backlink to drupal node object
  28. public $defTimeIncrement; // default increment for elapsed time on timed contracts
  29. public $isSuspended; // is contract currently in suspended mode ?
  30. public $maxDate; // maximum date for which this contract is valid
  31. public $maxIssue;// maximum number of issues for which this contract is valid
  32. public $maxTime; // maximum elapsed time for which this contract is valid
  33. public $timeId; // time information for this contract
  34. public $uidCustomer; // owner of contract
  35. public $useMaxDate; // use the $maxDate parameter
  36. public $useMaxIssue;// use the $maxIssue parameter
  37. public $useMaxTime; // use the $maxTime parameter
  38.  
  39.  
  40. /**
  41. * Constructor-like method filling node from set of data in node
  42. * If nid is NULL, the node is new and nothing can be filled.
  43. * @param object $node
  44. * @todo consider the issues surrounding timeId from initial submission to actual insert
  45. */
  46. function init($node, $fromDB)
  47. {
  48. if (HELPDESKDEBUGALLFUNCTIONS == TRUE)
  49. { echo "<pre>HD CONTRACT/init (" . (isset($node->nid) ? $node->nid : 'new node') . ")</pre>\r\n" ; }
  50. $this->node = $node ;
  51.  
  52. /**
  53. * node == NULL: can it happen ?
  54. * !is_object($node): can it happen ?
  55. * !isset($node->nid): new node before insert (doing preview)
  56. * full node, but not in DB: new node before insert (initialising for insert)
  57. */
  58. if (!$fromDB)
  59. {
  60. // This is a new node, we initialize the object from the variable, there's nothing in the DB
  61. foreach ($this as $key => $value)
  62. {
  63. if (($key <> 'node') && ($key <> 'timeId')) // node: Don't recurse, timeId: must think more deeply about it
  64. $this->$key = isset($node->$key) ? $node->$key : NULL ;
  65. }
  66. return;
  67. }
  68. else
  69. {
  70. $q = '
  71. SELECT
  72. hdc.defTimeIncrement, hdc.isSuspended,
  73. hdc.timeId, hdc.uidCustomer,
  74. hdc.maxDate, hdc.maxIssue, hdc.maxTime,
  75. hdc.useMaxDate, hdc.useMaxIssue, hdc.useMaxTime,
  76. hdt.stime, hdt.atime, hdt.mtime, hdt.etime
  77. FROM
  78. {hd_contract} hdc
  79. LEFT JOIN {hd_timeinfo} hdt on hdc.timeid = hdt.timeid
  80. WHERE
  81. hdc.nid = %d' ;
  82. $res = db_query($q, $node->nid);
  83. $ar = db_fetch_array($res) ; // Should give exactly one row of result, zero if contract does not exist yet
  84. foreach ($ar as $key => $value)
  85. {
  86. $this->$key = $value ;
  87. }
  88. // echo "<pre>New contract object: " ; print_r ($this) ; "</pre>\r\n";
  89. }
  90. }
  91.  
  92. /**
  93. * implement hook_view for HD contracts
  94. * @todo implement display of the timeinfo object
  95. * @todo implement contract balance display
  96. * @return string
  97. *
  98. */
  99. function view()
  100. {
  101. $headers = array(
  102. t('Attribute'),
  103. t('Use'),
  104. t('Maximum'),
  105. t('Current')
  106. ) ;
  107. $rows = array
  108. ( // Row with attributes on the row and some of its cells.
  109. array // First row
  110. (
  111. t('Number of issues'),
  112. array ('data' => $this->useMaxIssue ? t('Yes') : t('No'), 'style' => 'text-align: center'),
  113. array ('data' => _helpdesk_notempty($this->maxIssue, '0'), 'style' => 'text-align: center'),
  114. HELPDESKUNIMPLEMENTED
  115. ),
  116. array // First row
  117. (
  118. t('Date'),
  119. array ('data' => $this->useMaxDate ? t('Yes') : t('No'), 'style' => 'text-align: center'),
  120. array ('data' => format_date($this->maxDate, 'medium'), 'style' => 'text-align: center'),
  121. HELPDESKUNIMPLEMENTED
  122. ),
  123. array // First row
  124. (
  125. t('Spent minutes'),
  126. array ('data' => $this->useMaxTime ? t('Yes') : t('No'), 'style' => 'text-align: center'),
  127. array ('data' => _helpdesk_notempty($this->maxTime, '0'), 'style' => 'text-align: center'),
  128. HELPDESKUNIMPLEMENTED
  129. ),
  130. );
  131. $output = theme_table($headers, $rows);
  132. $ar = array();
  133. $custname = user_load(array('uid' => $this->uidCustomer))->name;
  134. $ar[] = sprintf (t('Contract holder: %s'), l($custname, 'user/' . $this->uidCustomer)) ;
  135. unset($custname);
  136. $ar[] = sprintf(t('Default time increment for events, in minutes: %d '), _helpdesk_notempty($this->defTimeIncrement, '0'));
  137. $ar[] = sprintf(t('Is contract suspended ? %s'), $this->isSuspended ? t('Yes') : t('No'));
  138. $output .= theme_item_list($ar);
  139. return $output;
  140. }
  141. /**
  142. * Implement hook_insert for HD Contracts
  143. *
  144. * @return void
  145. */
  146. function insert()
  147. {
  148. /* all of this must be inserted
  149. public $node ; ///< backlink to drupal node object
  150. public $defTimeIncrement; // default increment for elapsed time on timed contracts
  151. public $isSuspended; // is contract currently in suspended mode ?
  152. public $maxDate; // maximum date for which this contract is valid
  153. public $maxIssue;// maximum number of issues for which this contract is valid
  154. public $maxTime; // maximum elapsed time for which this contract is valid
  155. public $timeId; // time information for this contract
  156. public $uidCustomer; // owner of contract
  157. public $useMaxDate; // use the $maxDate parameter
  158. public $useMaxIssue;// use the $maxIssue parameter
  159. public $useMaxTime; // use the $maxTime parameter */
  160. $q = '
  161. INSERT INTO {hd_contract}
  162. (deftimeincrement, issuspended,
  163. maxdate, maxissue, maxtime,
  164. nid, timeid, uidcustomer,
  165. usemaxdate, usemaxissue, usemaxtime)
  166. VALUES
  167. (%d, %d,
  168. %d, %d, %d,
  169. %d, %d, %d,
  170. %d, %d, %d)
  171. ';
  172. $res = db_query($q,
  173. $this->defTimeIncrement, $this->isSuspended,
  174. $this->maxDate, $this->maxIssue, $this->maxTime,
  175. $this->node->nid, $this->timeId, $this->uidCustomer,
  176. $this->useMaxDate, $this->useMaxIssue, $this->useMaxTime
  177. );
  178. }
  179. /**
  180. *
  181. * @param helpdeskCustomer $uidcustomer
  182. * @desc lists contracts held by a helpdeskCustomer
  183. * @return array contract summaries (nid, title, created, changed) orderd by nid
  184. */
  185. static function getContractsByUid (helpdeskCustomer $uidcustomer)
  186. {
  187. $q = '
  188. select
  189. n.nid, n.title, n.created, n.changed
  190. from
  191. hd_contract hdc
  192. left join node n on hdc.nid = n.nid
  193. where UIDCUSTOMER = %d
  194. order by
  195. 1
  196. ' ;
  197. $ret = array () ;
  198. $res = db_query ($q, $uidcustomer) ;
  199. while ($o = db_fetch_object($res))
  200. {
  201. $ret[] = array();
  202. $ret[]['nid'] = $o->nid ;
  203. $ret[]['title'] = $o->title ;
  204. $ret[]['created'] = $o->created ;
  205. $ret[]['changed'] = $o->changed ;
  206. }
  207. return $ret ;
  208. }
  209.  
  210. /**
  211. * @return array
  212. * @desc Returns the nids of all tickets bound to this contracts
  213. */
  214. function getTickets ()
  215. {
  216. $ret = array() ;
  217. $sq = 'select distinct NID from {hd_event} ev '
  218. . 'inner join {hd_timeinfo} ti on ev.TIMEID = ti.TIMEID '
  219. . 'where '
  220. . ' (NIDROOT = NID) ' // This only matches tickets, not other events
  221. . ' and (CONTRACTID=%1) '
  222. . 'order by ti.TIMEID desc ' ;
  223. $q = db_query($sq, $this->nid) ;
  224. while ($o = db_result($q))
  225. {
  226. array_push ($o) ;
  227. }
  228. unset ($o) ;
  229. unset ($q) ;
  230. unset ($sq) ;
  231. return $ret ;
  232. }
  233. /**
  234. * Generates the code for hook_form when node is a helpdesk Contract
  235. * @return string HTML
  236. */
  237. function form()
  238. {
  239. // Now we define the form elements specific to our node type.
  240. if (HELPDESKDEBUGALLFUNCTIONS == TRUE)
  241. {
  242. $ret = t ("<p>Defining new %nodename</p>\n", array ('%nodename' => $this->node_name())) ;
  243. }
  244. else
  245. $ret = '' ;
  246. /* This would allow format selection, but contracts are plain text, so we use 1 = filtered html */
  247. // $ret .= filter_form ('format', $node->format);
  248. // $node->format = 1 ; // Filtered HTML
  249. // But there's no "node" code in this method !
  250. // @todo find where input filtering goes in this model
  251.  
  252. // Select contract holder
  253. $q = '
  254. select
  255. distinct hdu.uidcustomer,
  256. u.name
  257. from
  258. {hd_user} hdu
  259. left join {users} u on hdu.uidcustomer = u.uid
  260. order by
  261. 1
  262. ' ;
  263. $res = db_query($q);
  264. $arCustomers = array() ;
  265. while ($o = db_fetch_object($res))
  266. $arCustomers[$o->uidcustomer] = "$o->name ($o->uidcustomer)" ; // We do this in code instead of SQL because SQL concatenation is not portable
  267. $ret = form_select('Contract holder', 'uidCustomer', $this->uidCustomer, $arCustomers,
  268. t('Choose the customer holding this contract.'), NULL,
  269. FALSE, // Only one customer per contract
  270. TRUE);
  271. /* display contract timeinfo */
  272. $ti = new helpdeskTimeInfo();
  273. $ti->init(array(
  274. 'S' => time(),
  275. 'A' => time(),
  276. 'M' => time(),
  277. 'E' => '',
  278. 'showS' => TRUE, 'showA' => TRUE, 'showM' => TRUE, 'showE' => TRUE,
  279. 'enabledS' => TRUE, 'enabledA' => FALSE, 'enabledM' => FALSE, 'enabledE' => TRUE),
  280. FALSE) ; // Load from params, not from DB
  281. $items = $ti->form();
  282. $ret .= form_group
  283. (
  284. t ('Contract date information'),
  285. $items,
  286. t ('Here you must define the date the contract starts (default to current date) and can optionally define when it ends'
  287. // . ', and access/modification dates (default to current date)'
  288. ),
  289. NULL) ;
  290. /**
  291. * Parameters for new contract
  292. */
  293. $items = form_checkbox (t ('Suspended'), 'isSuspended', 1, $this->isSuspended, '', NULL, FALSE) ;
  294. $items .= theme_table
  295. (
  296. array (t ('Contract limits'), t ('Limit value'), t ('Increment')),
  297. array
  298. (
  299. array (
  300. form_checkbox (t ('Minutes spent'), 'useMaxTime', 1, $this->useMaxTime, '', NULL, FALSE),
  301. form_textfield ('', 'maxTime', $this->maxTime, 16, 16, NULL, NULL, FALSE),
  302. form_textfield ('', 'defTimeIncrement', $this->defTimeIncrement, 4, 4, NULL, NULL, FALSE)
  303. ),
  304. array (
  305. form_checkbox (t ('Number of tickets'), 'useMaxIssue', 1, $this->useMaxIssue, '', NULL, FALSE),
  306. form_textfield ('', 'maxIssue', $this->maxIssue, 16, 16, NULL, NULL, FALSE),
  307. '&nbsp;'),
  308. array (
  309. form_checkbox (t ('Date'), 'useMaxDate', 1, $this->useMaxDate, '', NULL, FALSE),
  310. form_textfield ('', 'maxDate', $this->maxDate, 16, 16, NULL, NULL, FALSE),
  311. '&nbsp;')
  312. )
  313. ) ;
  314. $ret .= form_group (
  315. t ('Contract status'),
  316. $items,
  317. t ('Here you set the attributes of the contract: is it time-limited, duration-limited, issue-limited ? does it start suspended ?'
  318. . ' The increment value for time-limited contracts is the default duration consumed by each followup.'),
  319. NULL) ;
  320.  
  321. $ret .= form_textarea(t('Contract wording'), 'body', (isset($this->node->body) ? $this->node->body : ''), 60, 20);
  322. return $ret ;
  323. }
  324. }
  325.  
  326. error_reporting($_contract_saved_er) ;
  327. unset($_contract_saved_er) ;
  328. ?>

Documentation generated on Tue, 01 Nov 2005 23:14:58 +0100 by phpDocumentor 1.3.0RC3