PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/public_html/story_view.php

https://github.com/winbladh/imuse
PHP | 208 lines | 163 code | 42 blank | 3 comment | 24 complexity | b1acd5e56f3d7c987d5645545337b6fc MD5 | raw file
  1. <?php
  2. include "includes.php";
  3. $storyid = $_GET['storyid'];
  4. $DB->query("SELECT name, storyjson FROM stories WHERE storyid = '$storyid' AND projectid = '$PROJECTID' LIMIT 0, 1");
  5. if(!$DB->get_num_rows())
  6. {
  7. die("FAILURE");
  8. }
  9. list($sname, $sjson) = $DB->fetch_row();
  10. $TPL->assign("story_name", $sname);
  11. $TPL->assign("story_json", base64_decode($sjson));
  12. if($_POST['submitted_conditions']==1 || $_GET['bypass'] == 1)
  13. {
  14. $TPL->assign("submitted_conditions", "1");
  15. $conditions = array();
  16. $DB->query("SELECT conditionid, conditiontext
  17. FROM conditions
  18. WHERE projectid = '$PROJECTID'
  19. ORDER BY conditionid DESC");
  20. if($DB->get_num_rows())
  21. {
  22. $fcond = array();
  23. $rows = $DB->fetch_assoc();
  24. foreach($rows as $res)
  25. {
  26. $obj = new stdClass();
  27. list($obj->id, $obj->text) = $res;
  28. $obj->value = "false";
  29. if(isset($_POST['cond'][$obj->id]) && $_POST['cond'][$obj->id]=="true")
  30. {
  31. $obj->value = "true";
  32. }
  33. $conditions[] = $obj;
  34. }
  35. }
  36. $TPL->assign("conditions", json_encode($conditions));
  37. }
  38. else
  39. {
  40. $conditions = array();
  41. function get_conditions($obj)
  42. {
  43. global $conditions;
  44. if(isset($obj->condition) && $obj->condition)
  45. {
  46. $conditions[] = $obj->condition;
  47. }
  48. if(isset($obj->children))
  49. {
  50. foreach($obj->children as $child)
  51. {
  52. if(is_object($child))
  53. {
  54. get_conditions($child);
  55. }
  56. }
  57. }
  58. }
  59. get_conditions(json_decode(base64_decode($sjson)));
  60. $condlist = array();
  61. foreach($conditions as $cond)
  62. {
  63. if(preg_match_all("/\bC([0-9]+)\b/", $cond, $match) > 0)
  64. {
  65. foreach($match[1] as $m)
  66. {
  67. $condlist[] = $m;
  68. }
  69. }
  70. }
  71. $condlist = array_unique($condlist);
  72. //print_r($condlist);
  73. $where = "0";
  74. if(count($condlist)>0)
  75. {
  76. $where = "conditionid IN (".implode(",",$condlist).")";
  77. }
  78. else
  79. {
  80. redirect("story_view.php?storyid=$storyid&bypass=1");
  81. }
  82. $condreplace = array(array(),array());
  83. $DB->query("SELECT conditionid, conditiontext
  84. FROM conditions
  85. WHERE $where AND
  86. projectid = '$PROJECTID'
  87. ORDER BY conditionid ASC");
  88. if($DB->get_num_rows())
  89. {
  90. $fcond = array();
  91. $rows = $DB->fetch_assoc();
  92. foreach($rows as $res)
  93. {
  94. $obj = new stdClass();
  95. list($obj->id, $obj->text) = $res;
  96. $fcond[] = $obj;
  97. $condreplace[0][] = "C".$obj->id;
  98. $condreplace[1][] = $obj->text;
  99. }
  100. $TPL->assign("condlist", $fcond);
  101. }
  102. $events = array();
  103. $DB->query("SELECT eventid, info FROM events");
  104. if($DB->get_num_rows())
  105. {
  106. $rows = $DB->fetch_assoc();
  107. foreach($rows as $res)
  108. {
  109. list($eid,$einfo) = $res;
  110. $events[$eid] = $einfo;
  111. }
  112. }
  113. $storyjson = json_decode(base64_decode($sjson));
  114. function recurse_story($obj)
  115. {
  116. global $condreplace, $events;
  117. $class = "";
  118. if($obj->name=="ifConditionTrue"){$class="conditionTrue storyConstruct";$constructname="ifConditionTrue";}
  119. elseif($obj->name=="ifConditionFalse"){$class="conditionFalse";}
  120. elseif($obj->name=="doInOrder"){$class="doInOrder";}
  121. elseif($obj->name=="while"){$class="while";}
  122. /* elseif($obj->name=="ifElse"){$class="ifElse";}*/
  123. elseif($obj->name=="ifElse"){$class="ifElse";$obj->name="checkCondition";}
  124. echo "<fieldset class=\"storyConstruct $class\">";
  125. echo "<legend>{$obj->name}";
  126. if(strlen($obj->condition)>0)
  127. {
  128. $condition = str_replace($condreplace[0], $condreplace[1], $obj->condition);
  129. if($obj->name=="ifConditionTrue"){
  130. echo "<span><code class\"constructcondition\">{$condition}</code></span> is true";
  131. }
  132. else if($obj->name=="ifConditionFalse"){
  133. echo "<span><code class\"constructcondition\">{$condition}</code></span> is false";
  134. }
  135. else{
  136. echo "<span><code class=\"constructcondition\">{$condition}</code></span>";
  137. }
  138. }
  139. echo "</legend>";
  140. if(isset($obj->children) && count($obj->children)>0)
  141. {
  142. foreach($obj->children as $child)
  143. {
  144. if(is_object($child))
  145. {
  146. if($obj->name=="checkCondition"){
  147. $child->condition = $obj->condition;
  148. }
  149. recurse_story($child);
  150. }
  151. else
  152. {
  153. echo "<code class=\"dragEvent\">".parse_event_info($events[$child])."</code>";
  154. }
  155. }
  156. }
  157. echo "</fieldset>";
  158. }
  159. ob_start();
  160. recurse_story($storyjson);
  161. $story_html = ob_get_clean();
  162. $TPL->assign("story_html", $story_html);
  163. }
  164. //$TPL->assign("submitted_conditions", "1");
  165. $TPL->display("story_view.tpl");
  166. ?>