Source for file viewer.php

Documentation is available at viewer.php

  1. <?php
  2.  
  3. /* Processing and output, called by the "component" interface class */
  4.  
  5. class pl_olga_vv_viewer {
  6.  
  7. var $_debug_prefix;
  8.  
  9. var $_config;
  10. var $_phantom;
  11. var $_topic;
  12. var $_layout;
  13. var $_create_by_uri;
  14. var $_allow_create_by_uri;
  15. var $_user;
  16. var $_is_owner;
  17. var $_argv;
  18. var $_view;
  19. var $do_vote;
  20. var $_antispam;
  21. var $form_prefix;
  22.  
  23. function pl_olga_vv_viewer($topic, $config) {
  24.  
  25. global $midgard;
  26. $this->_debug_prefix = "pl.olga.vv viewer::";
  27.  
  28. $this->_config = $config;
  29. $this->_topic = $topic;
  30. $this->_phantom = false;
  31. $this->_layout = false;
  32. $this->_allow_create_by_uri = $this->_config->get("allow_create_by_uri");
  33. $this->_antispam = $this->_config->get("antispam");
  34. $this->_create_by_uri = false;
  35. $this->_view = false;
  36. $this->_is_owner = false;
  37. $this->do_vote=false;
  38. $this->_user = $midgard->user;
  39. if ($this->_user) {
  40. if (mgd_is_topic_owner($this->_topic->id)) {
  41. $this->_is_owner = true;
  42. }
  43. }
  44. $this->form_prefix = "pl_olga_vv_viewer_";
  45.  
  46. }
  47.  
  48.  
  49. function can_handle($argc, $argv) {
  50.  
  51. debug_push($this->_debug_prefix . "can_handle");
  52.  
  53. $this->_argv = $argv;
  54.  
  55. // see if we can handle this request.
  56. if (!$this->_getPhantom($argc, $argv)) {
  57. // errstr and errcode are already set by getArticle
  58. debug_add("could not locate phantom article. see above.");
  59. debug_pop();
  60. return false;
  61. }
  62. $GLOBALS["pl_olga_vv_nap_activeid"] = $this->_thread->id;
  63.  
  64. debug_pop();
  65. return true;
  66. }
  67.  
  68. function _getPhantom($argc, $argv) {
  69. global $pl_olga_vv_layouts;
  70.  
  71. debug_push($this->_debug_prefix . "_getPhantom");
  72.  
  73. if ($argc == 0) {
  74. // Ooops
  75. return false;
  76. }
  77.  
  78. if ($argc == 1) {
  79.  
  80. $this->_phantom = mgd_get_article_by_name($this->_topic->id,$argv[0]);
  81. if (!$this->_phantom) {
  82. // Allow access non-existing URIs if accepted by config
  83. if ($this->_allow_create_by_uri) {
  84. debug_add("Allowing autocreation of phantom", MIDCOM_LOG_INFO);
  85. return true;
  86. } else {
  87. debug_add("Phantom $argv[0] could not be loaded: " . mgd_errstr(), MIDCOM_LOG_INFO);
  88. $this->errstr = "Phantom $argv[0] could not be loaded: " . mgd_errstr();
  89. $this->errcode = MIDCOM_ERRNOTFOUND;
  90. return false;
  91. }
  92. }
  93. return true;
  94.  
  95. } else {
  96.  
  97. debug_add("too many parameters", MIDCOM_LOG_DEBUG);
  98. debug_pop();
  99. return false;
  100. }
  101. }
  102.  
  103. function handle() {
  104. global $_REQUEST,$_SERVER;
  105. debug_push($this->_debug_prefix . "handle");
  106.  
  107. if (!$this->_phantom) {
  108. // Create missing phantom article by URI if requested
  109. if ($this->_allow_create_by_uri && $this->_is_owner) {
  110.  
  111. $article = mgd_get_article();
  112. $article->author = $this->_user;
  113. // Usually we refer here with resource GUID
  114. $target_object = mgd_get_object_by_guid($this->_argv[0]);
  115. if ($target_object) {
  116. if (isset($target_object->title)) {
  117. $article->title = $target_object->title;
  118. }
  119. if (isset($target_object->author)) {
  120. $article->author = $target_object->author;
  121. } else {
  122. $article->author = $target_object->creator;
  123. }
  124. }
  125. $article->name =$this->_argv[0];
  126. $article->topic = $this->_topic->id;
  127. $stat = $article->create();
  128. if ($stat) {
  129. $this->_phantom = mgd_get_article($stat);
  130. debug_add("Phantom $this->_argv[0] was created: " . mgd_errstr(), MIDCOM_LOG_INFO);
  131. } else {
  132. debug_add("Phantom $this->_argv[0] could not be created: " . mgd_errstr(), MIDCOM_LOG_INFO);
  133. return false;
  134. }
  135. } else {
  136. // Phantom are not allowed to be autocreated
  137. return false;
  138. }
  139. }
  140.  
  141. // If we got Anti-Spam set check who voted lately
  142.  
  143. if($this->_antispam){
  144. $last_vote=$this->_phantom->extra3;
  145. if($last_vote==$_SERVER["REMOTE_ADDR"]){
  146. $this->do_vote=false;
  147. }else{
  148. $this->do_vote=true;
  149. }
  150. }else{
  151. $this->do_vote=true;
  152. }
  153.  
  154. // Vote form sent
  155. if (array_key_exists($this->form_prefix."vote_submit", $_REQUEST)) {
  156. // Save only votes with values and do not increase view counter
  157. if ($_REQUEST[$this->form_prefix."vote_value"]) {
  158. debug_add("Trying to save a vote", MIDCOM_LOG_DEBUG);
  159. if($this->do_vote){
  160. $this->_phantom->extra1=$this->_phantom->extra1+$_REQUEST[$this->form_prefix."vote_value"];
  161. $this->_phantom->extra2++;
  162. if($this->_antispam) $this->_phantom->extra3=$_SERVER["REMOTE_ADDR"];
  163. $this->_phantom->update();
  164. $this->do_vote=false;
  165. }
  166. return true;
  167. }
  168. }
  169. $this->_phantom->view++;
  170. $this->_phantom->update();
  171. debug_pop();
  172. return true;
  173. }
  174. function show() {
  175. global $midcom;
  176. global $view;
  177. global $view_form_prefix;
  178. $view_form_prefix = $this->form_prefix;
  179. debug_push($this->_debug_prefix . "show");
  180.  
  181. // get l10n libraries
  182. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  183. $GLOBALS["view_l10n"] = $i18n->get_l10n("pl.olga.vv");
  184. $GLOBALS["view_l10n_midcom"] = $i18n->get_l10n("midcom");
  185. $view["views"] = $this->_phantom->view*1;
  186. $view["vote"] = $this->_phantom->extra1*1;
  187. $view["votes"] = $this->_phantom->extra2*1;
  188. $view["result"] = ($view["votes"]>0)?round($view["vote"]/$view["votes"],2):0;
  189.  
  190. midcom_show_style("view-result");
  191. if($this->do_vote) midcom_show_style("vote-widget");
  192. debug_pop();
  193. return true;
  194. }
  195.  
  196.  
  197. function get_metadata() {
  198.  
  199. // metadata for the current element
  200. /*
  201. return array (
  202. MIDCOM_META_CREATOR => ...,
  203. MIDCOM_META_EDITOR => ...,
  204. MIDCOM_META_CREATED => ...,
  205. MIDCOM_META_EDITED => ...
  206. );*/
  207. }
  208.  
  209. } // viewer
  210.  
  211. ?>

Documentation generated on Mon, 21 Nov 2005 18:23:43 +0100 by phpDocumentor 1.3.0RC3