/wordpress-plugin/v1/plugin/model/exhibit.php

https://github.com/eob/datapress-old · PHP · 198 lines · 164 code · 22 blank · 12 comment · 44 complexity · e5c85a3459fc3125284c881933afff33 MD5 · raw file

  1. <?php
  2. require_once('db-methods.php');
  3. class WpPostExhibit {
  4. // Used to save the exhibit in a DB
  5. protected $dbfields = array(
  6. 'id' => NULL,
  7. 'exhibit_config' => NULL,
  8. 'version' => 3,
  9. );
  10. // Use null for non-list strings
  11. // Use an object to clone for arrays of objects
  12. protected $exhibitfields = array(
  13. 'lightbox' => NULL,
  14. 'height' => NULL,
  15. 'lenses' => NULL, // instantiated in constructor
  16. 'views' => NULL, // instantiated in constructor
  17. 'facets' => NULL, // instantiated in constructor
  18. 'datasources' => NULL, // instantiated in constructor
  19. 'css' => NULL,
  20. 'custom_html' => NULL
  21. );
  22. static function getForPost($postid) {
  23. global $wpdb;
  24. $table = WpExhibitConfig::table_name(WpExhibitConfig::$EXHIBITS_ASSOC_TABLE_KEY);
  25. $exhibitid = $wpdb->get_var("SELECT exhibitid FROM $table WHERE postid=$postid ;");
  26. if (!($exhibitid == 0)) {
  27. $post_exhibit = new WpPostExhibit();
  28. if (DbMethods::loadFromDatabase($post_exhibit, $exhibitid)) {
  29. return $post_exhibit;
  30. }
  31. }
  32. return NULL;
  33. }
  34. protected $constructedexhibit = array();
  35. function WpPostExhibit() {
  36. $this->exhibitfields['lenses'] = new WpExhibitLens();
  37. $this->exhibitfields['views'] = new WpExhibitView();
  38. $this->exhibitfields['facets'] = new WpExhibitFacet();
  39. $this->exhibitfields['datasources'] = new WpExhibitDatasource();
  40. }
  41. function getTableName() {
  42. return WpExhibitConfig::table_name(WpExhibitConfig::$EXHIBITS_TABLE_KEY);
  43. }
  44. function getFormPrefix() {
  45. return WpExhibitConfig::$EXHIBIT_FORM_PREFIX;
  46. }
  47. function getLinkCaption() {
  48. return "Exhibit #" . $this->getId();
  49. }
  50. // On JSON schema upgrade, edit this function, and update the version
  51. // variable
  52. function afterDbLoad() {
  53. $this->dbfields['exhibit_config'] = base64_decode($this->dbfields['exhibit_config']);
  54. $this->constructedexhibit = self::build_from_json($this->dbfields['exhibit_config'], $this->exhibitfields);
  55. // version 1 -> version 2 upgrade: add a 'decoration' field on all
  56. // lenses.
  57. if ($this->get('version') == 1) {
  58. $this->set('version', 2);
  59. foreach ($this->get('lenses') as $lens) {
  60. if ($lens->get('decoration') == NULL) {
  61. $lens->set('decoration', 'none');
  62. }
  63. }
  64. }
  65. // To version 3
  66. if ($this->get('version') == 2) {
  67. $this->set('version', 3);
  68. foreach ($this->get('views') as $view) {
  69. if ($view->get('kind') == 'view-tile') {
  70. if ($view->get('decoration') == NULL) {
  71. $view->set('decoration', 'none');
  72. }
  73. }
  74. }
  75. }
  76. }
  77. function getFields() {
  78. return $this->fields;
  79. }
  80. function get($field, $nice=false) {
  81. if (array_key_exists($field, $this->dbfields)) {
  82. return $this->dbfields[$field];
  83. } else if (array_key_exists($field, $this->exhibitfields)) {
  84. return $this->constructedexhibit[$field];
  85. } else {
  86. if (! $nice) {
  87. die("Attempted to get a field that does not exist: $field.");
  88. }
  89. }
  90. }
  91. function set($field, $value, $nice=false) {
  92. if (array_key_exists($field, $this->dbfields)) {
  93. $this->dbfields[$field] = $value;
  94. } else if (array_key_exists($field, $this->exhibitfields)) {
  95. $this->constructedexhibit[$field] = $value;
  96. } else {
  97. if (! $nice) {
  98. die("Attempted to set a field that does not exist: $field.");
  99. }
  100. }
  101. }
  102. function getStatisticReport($currentView, $postid) {
  103. // $postid = $wpdb->get_var("SELECT postid FROM $table WHERE exhibitid=$exhibitid ;");
  104. $permalink = base64_encode(get_permalink($postid));
  105. $viewState = base64_encode($this->get('lightbox') ? "lightbox" : "inline");
  106. $postType = base64_encode(get_post($postid)->post_type);
  107. $currentView = base64_encode($currentView);
  108. $report = "{currentview: \"$currentView\", viewstate: \"$viewState\", posttype: \"$postType\", permalink: \"$permalink\"}";
  109. return $report;
  110. }
  111. function save() {
  112. global $wpdb;
  113. $this->dbfields['exhibit_config'] = self::get_json($this->exhibitfields, $this->constructedexhibit);
  114. $table = $this->getTableName();
  115. if ($this->dbfields['id'] == NULL) {
  116. // Do an insert
  117. $sql = "INSERT INTO $table (id, exhibit_config, version) VALUES (%d, %s, %d);";
  118. $sql = $wpdb->prepare($sql, $this->dbfields['id'], base64_encode($this->dbfields['exhibit_config']), $this->dbfields['version']);
  119. } else {
  120. // Do an update
  121. $sql = "UPDATE $table SET exhibit_config=%s, version=%d WHERE id=%d";
  122. $sql = $wpdb->prepare($sql, base64_encode($this->dbfields['exhibit_config']), $this->dbfields['version'], $this->dbfields['id']);
  123. }
  124. $result = $wpdb->query($sql);
  125. if ($this->dbfields['id'] == NULL) {
  126. // Get the ID of the insert and set it.
  127. $sql = "SELECT LAST_INSERT_ID();";
  128. $this->set('id', $wpdb->get_var($sql));
  129. }
  130. }
  131. static function get_json($fieldlist, $constructed) {
  132. $structure = array();
  133. foreach ($fieldlist as $kind => $factory) {
  134. if ($factory == NULL) { // it's a string field
  135. $structure[$kind] = $constructed[$kind];
  136. } else { // it's a list of cloneable objects
  137. $arrays = array();
  138. if (array_key_exists($kind, $constructed) && ($constructed[$kind] != NULL)) {
  139. foreach ($constructed[$kind] as $object) {
  140. array_push($arrays, $object->getFields());
  141. }
  142. }
  143. $structure[$kind] = $arrays;
  144. }
  145. }
  146. $json_encoded = json_encode($structure);
  147. return $json_encoded;
  148. }
  149. static function build_from_json($json, $fieldlist) {
  150. $decoded = (array) json_decode($json);
  151. $loadinto = array();
  152. foreach ($fieldlist as $kind => $factory) {
  153. if ($factory == NULL) { // it's a string field
  154. if ($json != NULL) {
  155. $loadinto[$kind] = $decoded[$kind];
  156. } else {
  157. $loadinto[$kind] = "";
  158. }
  159. } else { // it's a list of cloneable objects
  160. $objects = array();
  161. if (($json != NULL) && ($decoded != NULL)) {
  162. foreach ($decoded[$kind] as $pairs) {
  163. $cloned = clone $factory;
  164. foreach ((array) $pairs as $key => $value ) {
  165. $cloned->set($key, $value);
  166. }
  167. array_push($objects, $cloned);
  168. }
  169. }
  170. $loadinto[$kind] = $objects;
  171. }
  172. }
  173. return $loadinto;
  174. }
  175. }
  176. ?>