Source for file pim-defs.php

Documentation is available at pim-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: pim-defs.php */
  22. /* Author: Paul Waite/Mark Kessell */
  23. /* Description: Definitions for manipulating personal information such */
  24. /* as organisations, addresses, emails, phone numbers, */
  25. /* urls, etc. */
  26. /* */
  27. /* ******************************************************************** */
  28. /** @package pim */// DEFINITIONS
  29. /** Create new organisation ID value */
  30. ("NEW_ORGANISATION", -1);
  31. /** Create new contact info ID value */
  32. ("NEW_CONTACTINFO", -1);
  33.  
  34. // ----------------------------------------------------------------------
  35. /**
  36. * The organisation class manages a single set of information related to a
  37. * "organisation". This is an identity distinct from the system-oriented
  38. * uuser record, and is intended to be a generic mechanism for storing
  39. * data on human beings.
  40. * @package pim
  41. */
  42. class organisation extends RenderableObject {
  43. /** Organisation ID */
  44.  
  45. var $orgid = NEW_ORGANISATION;
  46. /** Organisation name */
  47.  
  48. var $org_name = "";
  49. /** Description */
  50.  
  51. var $description = "";
  52. /** Contact person name */
  53.  
  54. var $org_contact = "";
  55. /** Contact info object */
  56.  
  57. var $contactinfo;
  58. /** Whether organisation is enabled */
  59.  
  60. var $enabled = true;
  61. /** Whether organisation exists in database */
  62.  
  63. var $exists = false;
  64. /**
  65. * Constructor
  66. * Create a new organisation object.
  67. * @param string $id The unique identity of the organisation.
  68. */
  69. function organisation($id=NEW_ORGANISATION) {
  70. $this->get($id);
  71. } // organisation
  72. // ....................................................................
  73. /**
  74. * Get the organisation.
  75. * Retrieves the specified organisation from database.
  76. * @param string $id The unique integer identity of the organisation to get.
  77. */
  78. function get($id="") {
  79. debug_trace($this);
  80. if ($id != "") {
  81. $this->orgid = $id;
  82. }
  83. $this->exists = false;
  84. if ($this->orgid != NEW_ORGANISATION) {
  85. // Try and read in existing organisation info..
  86. $q = "SELECT * FROM ax_organisation";
  87. $q .= " WHERE org_id=$this->orgid";
  88. $orgQ = dbrecordset($q);
  89. if ($orgQ->hasdata) {
  90. $this->org_name = $orgQ->field("org_name");
  91. $this->description = $orgQ->field("org_desc");
  92. $this->org_contact = $orgQ->field("org_contact");
  93. $this->enabled = $orgQ->istrue("enabled");
  94. $cinfoid = $orgQ->field("contactinfo_id");
  95. if ($cinfoid != "") {
  96. $this->contactinfo = new contactinfo($cinfoid);
  97. }
  98. else {
  99. $this->contactinfo = new contactinfo();
  100. }
  101. $this->exists = true;
  102. }
  103. }
  104. else {
  105. // Initialise the NEW object values..
  106. $this->initialise();
  107. }
  108. debug_trace();
  109. // Return true if at least the organisation exists..
  110. return $this->exists;
  111. } // get
  112. // ....................................................................
  113. /**
  114. * Save the organisation.
  115. * Save this organisation to the database. Create a new one if it
  116. * doesn't already exist.
  117. */
  118. function save() {
  119. debug_trace($this);
  120. // Deal with brand new organisation..
  121. if (!$this->exists) {
  122. // If we are in need of a new ID, then get one..
  123. if ($this->orgid == NEW_ORGANISATION) {
  124. $this->orgid = get_next_sequencevalue("seq_org_id", "ax_organisation", "org_id");
  125. $this->contactinfo->save();
  126. }
  127. $savQ = new dbinsert("ax_organisation");
  128. $savQ->set("org_id", $this->orgid);
  129. }
  130. else {
  131. $savQ = new dbupdate("ax_organisation");
  132. $savQ->where("org_id=$this->orgid");
  133. }
  134. $savQ->set("org_name", $this->org_name);
  135. $savQ->set("org_desc", $this->description);
  136. $savQ->set("org_contact", $this->org_contact);
  137. $savQ->set("enabled", $this->enabled);
  138. if (isset($this->contactinfo)) {
  139. $savQ->set("contactinfo_id", $this->contactinfo->contactinfoid);
  140. }
  141. $this->exists = $savQ->execute();
  142. debug_trace();
  143.  
  144. } // save
  145. // ....................................................................
  146. /**
  147. * Delete the organisation.
  148. * Delete this organisation from the database.
  149. */
  150. function delete() {
  151. debug_trace($this);
  152. if ($this->exists) {
  153. start_transaction();
  154. if (isset($this->contactinfo)) {
  155. $this->contactinfo->delete();
  156. }
  157. if (dbcommand("DELETE FROM ax_organisation WHERE org_id=$this->orgid")) {
  158. $this->initialise();
  159. $this->exists = false;
  160. }
  161. commit();
  162. }
  163. debug_trace();
  164. } // delete
  165. // ....................................................................
  166. /** Clear the object variables to default values. */
  167.  
  168. function initialise() {
  169. $this->orgid = NEW_ORGANISATION;
  170. $this->org_name = "";
  171. $this->description = "";
  172. $this->org_contact = "";
  173. $this->contactinfo = new contactinfo();
  174. $this->enabled = true;
  175. $this->exists = false;
  176. } // initialise
  177. // ....................................................................
  178. /**
  179. * Process a possible POST of a form with new contact info data. We
  180. * only do anything here if the special var $_save_x is defined (ie. this
  181. * would be from user clicking button named "_save"), and also the organisation
  182. * ID $_ORG_org_id. We return true if POST was processed.
  183. * @return boolean True if POSTed contact info was processed, else false
  184. */
  185. function POSTprocess() {
  186. $processed = false;
  187. debug_trace($this);
  188. global $_save_x, $_ORG_org_id;
  189. if (isset($_save_x) && isset($_ORG_org_id)) {
  190. // Only process org data meant for us..
  191. if ($this->orgid == $_ORG_org_id) {
  192. // Process/save any POSTed contact info..
  193. if (isset($this->contactinfo)) {
  194. $this->contactinfo->POSTprocess();
  195. }
  196. // Access POSTed information..
  197. global $_ORG_org_name, $_ORG_org_name, $_ORG_description;
  198. global $_ORG_org_contact, $_ORG_enabled;
  199. // Stash the new data..
  200. $this->org_name = $_ORG_org_name;
  201. $this->description = $_ORG_description;
  202. $this->org_contact = $_ORG_org_contact;
  203. $this->enabled = isset($_ORG_enabled);
  204. // Now save these changes..
  205. $this->save();
  206. $processed = true;
  207. }
  208. }
  209. if (isset($_delete_x) && isset($_ORG_org_id)) {
  210. $this->delete();
  211. $processed = true;
  212. }
  213. debug_trace();
  214. return $processed;
  215. } // POSTprocess
  216. // ....................................................................
  217. /** Display organisation subform via the render() method.
  218. * @param string $id_mode Whether to hide the ID or not
  219. */
  220. function html($idmode="hide_id") {
  221. debug_trace($this);
  222.  
  223. // FIELD - org_id
  224. if ($idmode == "hide_id") {
  225. $orgid_F = new form_hiddenfield("_ORG_org_id", $this->orgid);
  226. }
  227. else {
  228. $orgid_F = new form_labelfield("Org ID", $this->orgid);
  229. }
  230.  
  231. // FIELD - First name
  232. $org_name_F = new form_textfield("_ORG_org_name", "Organisation name", $this->org_name);
  233. $org_name_F->setstyle("width: 345px");
  234.  
  235. // FIELD - Description
  236. $description_F = new form_textfield("_ORG_description", "Description", $this->description);
  237. $description_F->setstyle("width: 345px");
  238.  
  239. // FIELD - Organisation contact person
  240. $org_contact_F = new form_textfield("_ORG_org_contact", "Contact person", $this->org_contact);
  241. $org_contact_F->setstyle("width: 345px");
  242.  
  243. // FIELD - Enabled
  244. $enabled_F = new form_checkbox("_ORG_enabled", "Enabled");
  245. $enabled_F->checked = $this->enabled;
  246.  
  247. // SUB-FORMS..
  248. $sform = new subform();
  249. $sform->add($orgid_F);
  250. $sform->add($org_name_F);
  251. $sform->add($description_F);
  252. $sform->add($org_contact_F);
  253. $sform->add($enabled_F);
  254. $forms = $sform->render();
  255. if (isset($this->contactinfo)) {
  256. $forms .= $this->contactinfo->render();
  257. }
  258.  
  259. // Table containing the form..
  260. $Tdetail = new table("organisation");
  261. $Tdetail->setpadding(2);
  262. $Tdetail->tr("background-color:#333333;");
  263. $Tdetail->td("<b>ORGANISATION DETAILS</b>", "color:white");
  264. $Tdetail->tr();
  265. $Tdetail->td( $forms );
  266. $Tdetail->td_alignment("right", "top");
  267.  
  268. debug_trace();
  269. return $Tdetail->render();
  270. } // html
  271.  
  272. } // organisation class
  273. // ----------------------------------------------------------------------
  274.  
  275. /**
  276. * The contactinfo class manages a single set of contact information
  277. * which might be associated with a person or thing.
  278. * @package pim
  279. */
  280. class contactinfo extends RenderableObject {
  281. // Public
  282. /** contactinfo ID */
  283.  
  284. var $contactinfoid;
  285. /** addr_postal */
  286.  
  287. var $addr_postal = "";
  288. /** addr_street */
  289.  
  290. var $addr_street = "";
  291. /** addr_suburb */
  292.  
  293. var $addr_suburb = "";
  294. /** addr_city */
  295.  
  296. var $addr_city = "";
  297. /** addr_country */
  298.  
  299. var $addr_country = "";
  300. /** addr_code */
  301.  
  302. var $addr_code = "";
  303. /** addr_other */
  304.  
  305. var $addr_other = "";
  306. /** emails */
  307.  
  308. var $emails = "";
  309. /** phone */
  310.  
  311. var $phone = "";
  312. /** phone_fax */
  313.  
  314. var $phone_fax = "";
  315. /** phone_work */
  316.  
  317. var $phone_work = "";
  318. /** phone_mobile */
  319.  
  320. var $phone_mobile = "";
  321. /** urls */
  322.  
  323. var $urls = "";
  324.  
  325. // Private
  326. /** Whether contactinfo exists in database
  327. @access private */
  328. var $exists = false;
  329. // ....................................................................
  330. /**
  331. * Constructor
  332. * Create a new contactinfo object.
  333. * @param string $id The unique identity of the contactinfo.
  334. */
  335. function contactinfo($id=NEW_CONTACTINFO) {
  336. $this->get($id);
  337. } // contactinfo
  338. // ....................................................................
  339. /**
  340. * Retrieves the specified contactinfo from database.
  341. * @param string $id The unique integer identity of the contactinfo to get.
  342. */
  343. function get($id="") {
  344. debug_trace($this);
  345. if ($id != "") {
  346. $this->contactinfoid = $id;
  347. }
  348. $this->exists = false;
  349. if ($this->contactinfoid != NEW_CONTACTINFO) {
  350. // Try and read in existing contactinfo info..
  351. $q = "SELECT * FROM ax_contactinfo";
  352. $q .= " WHERE contactinfo_id=$this->contactinfoid";
  353. $savQ = dbrecordset($q);
  354. if ($savQ->hasdata) {
  355. $this->addr_postal = $savQ->field("addr_postal");
  356. $this->addr_street = $savQ->field("addr_street");
  357. $this->addr_suburb = $savQ->field("addr_suburb");
  358. $this->addr_city = $savQ->field("addr_city");
  359. $this->addr_country = $savQ->field("addr_country");
  360. $this->addr_code = $savQ->field("addr_code");
  361. $this->addr_other = $savQ->field("addr_other");
  362. $this->emails = $savQ->field("emails");
  363. $this->phone = $savQ->field("phone");
  364. $this->phone_fax = $savQ->field("phone_fax");
  365. $this->phone_work = $savQ->field("phone_work");
  366. $this->phone_mobile = $savQ->field("phone_mobile");
  367. $this->urls = $savQ->field("urls");
  368. $this->exists = true;
  369. }
  370. }
  371. debug_trace();
  372. // Return true if at least the contactinfo exists..
  373. return $this->exists;
  374. } // get
  375. // ....................................................................
  376. /**
  377. * Save the contactinfo.
  378. * Save this contactinfo to the database. Create a new one if it
  379. * doesn't already exist.
  380. */
  381. function save() {
  382. debug_trace($this);
  383. // Deal with brand new contactinfo..
  384. if (!$this->exists) {
  385. // If we are in need of a new ID, then get one..
  386. if ($this->contactinfoid == NEW_CONTACTINFO) {
  387. $this->contactinfoid = get_next_sequencevalue("seq_contactinfo_id", "ax_organisation", "org_id");
  388. }
  389. $savQ = new dbinsert("ax_contactinfo");
  390. $savQ->set("contactinfo_id", $this->contactinfoid);
  391. }
  392. else {
  393. $savQ = new dbupdate("ax_contactinfo");
  394. $savQ->where("contactinfo_id=$this->contactinfoid");
  395. }
  396. $savQ->set("addr_postal", $this->addr_postal);
  397. $savQ->set("addr_street", $this->addr_street);
  398. $savQ->set("addr_suburb", $this->addr_suburb);
  399. $savQ->set("addr_city", $this->addr_city);
  400. $savQ->set("addr_country", $this->addr_country);
  401. $savQ->set("addr_code", $this->addr_code);
  402. $savQ->set("addr_other", $this->addr_other);
  403. $savQ->set("emails", $this->emails);
  404. $savQ->set("phone", $this->phone);
  405. $savQ->set("phone_fax", $this->phone_fax);
  406. $savQ->set("phone_work", $this->phone_work);
  407. $savQ->set("phone_mobile", $this->phone_mobile);
  408. $savQ->set("urls", $this->urls);
  409. $this->exists = $savQ->execute();
  410. debug_trace();
  411. } // save
  412. // ....................................................................
  413. /**
  414. * Delete the contactinfo.
  415. * Delete this contactinfo from the database.
  416. */
  417. function delete() {
  418. debug_trace($this);
  419. if ($this->exists) {
  420. dbcommand("DELETE FROM ax_contactinfo WHERE contactinfo_id=$this->contactinfoid");
  421. }
  422. debug_trace();
  423. } // delete
  424. // ....................................................................
  425. /**
  426. * Process a possible POST of a form with new contact info data. We
  427. * only do anything here if the special var $_save_x is defined (ie. this
  428. * would be from user clicking button named "_save"), and also the contact
  429. * info IF $_CI_contactinfo_id. We return true if POST was processed.
  430. * @return boolean True if POSTed contact info was processed, else false
  431. */
  432. function POSTprocess() {
  433. debug_trace($this);
  434. global $_save_x, $_CI_contactinfo_id;
  435. if (isset($_save_x) && isset($_CI_contactinfo_id)) {
  436. debugbr("CINF POSTprocess: processing submitted form data id=$_CI_contactinfo_id");
  437.  
  438. // Bring our ID into sync with submitted one..
  439. $this->contactinfoid = $_CI_contactinfo_id;
  440. $this->exists = ($this->contactinfoid != NEW_CONTACTINFO);
  441.  
  442. // Access POSTed information..
  443. global $_CI_postal, $_CI_street, $_CI_suburb, $_CI_city;
  444. global $_CI_country, $_CI_code, $_CI_other, $_CI_emails;
  445. global $_CI_phone, $_CI_phone_fax, $_CI_phone_work, $_CI_phone_mobile;
  446. global $_CI_urls;
  447. // Stash the new data..
  448. $this->addr_postal = $_CI_postal;
  449. $this->addr_street = $_CI_street;
  450. $this->addr_suburb = $_CI_suburb;
  451. $this->addr_city = $_CI_city;
  452. $this->addr_country = $_CI_country;
  453. $this->addr_code = $_CI_code;
  454. $this->addr_other = $_CI_other;
  455. $this->emails = $_CI_emails;
  456. $this->phone = $_CI_phone;
  457. $this->phone_fax = $_CI_phone_fax;
  458. $this->phone_work = $_CI_phone_work;
  459. $this->phone_mobile = $_CI_phone_mobile;
  460. $this->urls = $_CI_urls;
  461. // Now save these changes..
  462. $this->save();
  463. $processed = true;
  464. }
  465. debug_trace();
  466. } // POSTprocess
  467. // ....................................................................
  468. /**
  469. * Return a standard contact details set.
  470. * This is designed for in-line contact details such as might be
  471. * included as-is in an email confirmation of order for example. Only
  472. * fields which are not nullstring are included.
  473. */
  474. function contact_details() {
  475. $a = array();
  476. if ($this->addr_postal != "") $a[] = $this->addr_postal;
  477. if ($this->addr_street != "") $a[] = $this->addr_street;
  478. if ($this->addr_suburb != "") $a[] = $this->addr_suburb;
  479. if ($this->addr_city != "") $a[] = $this->addr_city;
  480. if ($this->addr_code != "") $a[] = $this->addr_code;
  481. if ($this->addr_country != "") $a[] = $this->addr_country;
  482. if ($this->addr_other != "") $a[] = $this->addr_other;
  483. if ($this->phone != "") $a[] = "Tel: $this->phone";
  484. if ($this->phone_fax != "") $a[] = "Fax: $this->phone_fax";
  485. if ($this->phone_work != "") $a[] = "Work: $this->phone_work";
  486. if ($this->phone_mobile != "") $a[] = "Mob: $this->phone_mobile";
  487. if ($this->urls != "") $a[] = "WWW: $this->urls";
  488. return implode(",\n", $a);
  489. } // contact_details
  490. // ....................................................................
  491. /** A diagnostic dump of the class variables for debugging purposes. */
  492.  
  493. function dump() {
  494. debug_trace($this);
  495. debugbr("CONTACT INFO DUMP (id=$this->contactinfoid)");
  496. debugbr("addr_postal: " . $this->addr_postal, DBG_DUMP);
  497. debugbr("addr_street: " . $this->addr_street, DBG_DUMP);
  498. debugbr("addr_suburb: " . $this->addr_suburb, DBG_DUMP);
  499. debugbr("addr_city: " . $this->addr_city, DBG_DUMP);
  500. debugbr("addr_country: " . $this->addr_country, DBG_DUMP);
  501. debugbr("addr_code: " . $this->addr_code, DBG_DUMP);
  502. debugbr("addr_other: " . $this->addr_other, DBG_DUMP);
  503. debugbr("emails: " . $this->emails, DBG_DUMP);
  504. debugbr("phone: " . $this->phone, DBG_DUMP);
  505. debugbr("phone_fax: " . $this->phone_fax, DBG_DUMP);
  506. debugbr("phone_work: " . $this->phone_work, DBG_DUMP);
  507. debugbr("phone_mobile: " . $this->phone_mobile, DBG_DUMP);
  508. debugbr("urls: " . $this->urls, DBG_DUMP);
  509. debug_trace();
  510. } // debug_cinfo
  511. // ....................................................................
  512. /** Display Contactinfo subform via the render() method. */
  513.  
  514. function html() {
  515. debug_trace($this);
  516.  
  517. // FIELD - contactinfo_id
  518. $contact_F = new form_hiddenfield("_CI_contactinfo_id", $this->contactinfoid);
  519.  
  520. // FIELD - Addr_postal
  521. $postal_F = new form_textfield("_CI_postal", "Address - Postal", $this->addr_postal);
  522. $postal_F->setstyle("width: 345px");
  523.  
  524. // FIELD - Addr_Street
  525. $street_F = new form_textfield("_CI_street", "Address - Street", $this->addr_street);
  526. $street_F->setstyle("width: 345px");
  527.  
  528. // FIELD - Addr_Suburb
  529. $suburb_F = new form_textfield("_CI_suburb", "Address - Suburb", $this->addr_suburb);
  530. $suburb_F->setstyle("width: 345px");
  531.  
  532. // FIELD - Addr_City
  533. $city_F = new form_textfield("_CI_city", "Address - City", $this->addr_city);
  534. $city_F->setstyle("width: 345px");
  535.  
  536. // FIELD - Addr_Country
  537. $country_F = new form_textfield("_CI_country", "Address - Country", $this->addr_country);
  538. $country_F->setstyle("width: 345px");
  539.  
  540. // FIELD - Addr_Code
  541. $code_F = new form_textfield("_CI_code", "Address - Code", $this->addr_code);
  542. $code_F->setstyle("width: 345px");
  543.  
  544. // FIELD - Addr_Other
  545. $other_F = new form_textfield("_CI_other", "Address - Other", $this->addr_other);
  546. $other_F->setstyle("width: 345px");
  547.  
  548. // FIELD - Email
  549. $this->emails = preg_replace("/[ |]/", "\n", $this->emails);
  550. $emails_F = new form_memofield("_CI_emails", "Email Address", $this->emails);
  551. $emails_F->setstyle("width: 345px");
  552. $emails_caption = new form_labelfield("", "<small><i>Enter one e-mail address per line</i></small>");
  553.  
  554. // FIELD - Phone
  555. $phone_home_F = new form_textfield("_CI_phone", "Home Phone", $this->phone);
  556. $phone_home_F->setstyle("width: 345px");
  557.  
  558. // FIELD - Fax
  559. $phone_fax_F = new form_textfield("_CI_phone_fax", "Fax", $this->phone_fax);
  560. $phone_fax_F->setstyle("width: 345px");
  561.  
  562. // FIELD - phone_work
  563. $phone_work_F = new form_textfield("_CI_phone_work", "Work Phone", $this->phone_work);
  564. $phone_work_F->setstyle("width: 345px");
  565.  
  566. // FIELD - phone_mobile
  567. $phone_mobile_F = new form_textfield("_CI_phone_mobile", "Mobile Phone", $this->phone_mobile);
  568. $phone_mobile_F->setstyle("width: 345px");
  569.  
  570. // FIELD - urls
  571. $this->urls = preg_replace("/[ |]/", "\n", $this->urls);
  572. $urls_F = new form_memofield("_CI_urls", "URL's", $this->urls);
  573. $urls_F->setstyle("width: 345px");
  574. $urls_caption = new form_labelfield("", "<small><i>Enter one URL per line</i></small>");
  575.  
  576. // FORM..
  577. $sform = new subform();
  578. $sform->add($contact_F);
  579. $sform->add($postal_F);
  580. $sform->add($street_F);
  581. $sform->add($suburb_F);
  582. $sform->add($city_F);
  583. $sform->add($country_F);
  584. $sform->add($code_F);
  585. $sform->add($other_F);
  586. $sform->add($emails_F);
  587. $sform->add($emails_caption);
  588. $sform->add($phone_home_F);
  589. $sform->add($phone_fax_F);
  590. $sform->add($phone_work_F);
  591. $sform->add($phone_mobile_F);
  592. $sform->add($urls_F);
  593. $sform->add($urls_caption);
  594.  
  595. $Tdetail = new table("contactinfo");
  596. $Tdetail->setpadding(2);
  597. $Tdetail->tr("background-color:#333333;");
  598. $Tdetail->td("<b>Contact Information</b>", "color:white");
  599. $Tdetail->tr();
  600. $Tdetail->td( $sform->render() );
  601. $Tdetail->td_alignment("right", "top");
  602. $s = $Tdetail->render();
  603.  
  604. debug_trace();
  605. return $s;
  606. } // html
  607.  
  608. } // contactinfo class
  609. // ----------------------------------------------------------------------
  610.  
  611. ?>

Documentation generated by phpDocumentor 1.3.0RC3