/UI/controllers/controllers.php

https://bitbucket.org/ashwanthkumar/blueignis_workspace · PHP · 321 lines · 179 code · 59 blank · 83 comment · 8 complexity · d3706852c674022e3996cdd948994dce MD5 · raw file

  1. <?php
  2. /**
  3. * Campaign.php
  4. *
  5. * Defines all the Campaign related dispatch / route implementations to be used
  6. * by the application.
  7. *
  8. * @package BlueIgnis
  9. * @component Controllers
  10. */
  11. /**
  12. * Render the Campaign Specific Dashboard
  13. *
  14. * @method GET
  15. * @route /campaign/:id/realtime/twitter
  16. */
  17. function campaignRealtimeTwitterDashboard($id) {
  18. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  19. // Login the user
  20. return redirect('/login');
  21. }
  22. global $db;
  23. $campaign = $db->select("bi_campaign", "id = :id", array(":id" => $id));
  24. $campaign = $campaign[0];
  25. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  26. layout('dashboard.html.php');
  27. // Getting the user from the session
  28. set('user', $_SESSION['user']);
  29. set('isTwitterDashboard', true);
  30. set('is_campaign', true);
  31. set('currentCampaignId', $id);
  32. set('campaignName', $campaign['name']);
  33. set('campaigns', $campaigns);
  34. set('authId', $_SESSION['authId']);
  35. set('user', $_SESSION['user']);
  36. set('db', $db);
  37. set('title', "Realtime Twitter Feeds");
  38. return render("twitter_dashboard.html.php");
  39. }
  40. /**
  41. * Render the Campaign Specific Sentiment Dashboard
  42. *
  43. * @method GET
  44. * @route /campaign/:id/realtime/sentiment
  45. */
  46. function campaignRealtimeSentimentDashboard($id) {
  47. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  48. // Login the user
  49. return redirect('/login');
  50. }
  51. global $db;
  52. $campaign = $db->select("bi_campaign", "id = :id", array(":id" => $id));
  53. $campaign = $campaign[0];
  54. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  55. layout('dashboard.html.php');
  56. // Getting the user from the session
  57. set('user', $_SESSION['user']);
  58. set('isSentimentDashboard', true);
  59. set('is_campaign', true);
  60. set('currentCampaignId', $id);
  61. set('campaignName', $campaign['name']);
  62. set('campaigns', $campaigns);
  63. set('authId', $_SESSION['authId']);
  64. set('user', $_SESSION['user']);
  65. set('db', $db);
  66. set('title', "Realtime Sentiment Feeds");
  67. return render("sentiment_dashboard.html.php");
  68. }
  69. /**
  70. * Renders view for creating a new Campaign
  71. *
  72. * @method GET
  73. * @route /campaign/create
  74. */
  75. function createNewCampaign() {
  76. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  77. // Login the user
  78. return redirect('/login');
  79. }
  80. global $db;
  81. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  82. layout('dashboard.html.php');
  83. // Getting the user from the session
  84. set('user', $_SESSION['user']);
  85. set('currentCampaignId', -1); // Since there is no campaign
  86. set('campaignName', "Create New Campaign");
  87. set('createCampaign', true);
  88. set('campaigns', $campaigns);
  89. set('authId', $_SESSION['authId']);
  90. set('user', $_SESSION['user']);
  91. set('db', $db);
  92. return render("new_campaign.html.php");
  93. }
  94. /**
  95. * Renders the Campaign dashboard
  96. *
  97. * @method GET
  98. * @route /campaign/:id
  99. */
  100. function campaignDashboard($id) {
  101. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  102. // Login the user
  103. return redirect('/login');
  104. }
  105. global $db;
  106. $campaign = $db->select("bi_campaign", "id = :id", array(":id" => $id));
  107. $campaign = $campaign[0];
  108. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  109. layout('dashboard.html.php');
  110. // Getting the user from the session
  111. set('user', $_SESSION['user']);
  112. set('is_campaign', true);
  113. set('currentCampaignId', $id);
  114. set('campaignName', $campaign['name']);
  115. set('campaigns', $campaigns);
  116. set('authId', $_SESSION['authId']);
  117. set('user', $_SESSION['user']);
  118. set('db', $db);
  119. set('title', "Dashboard");
  120. return render("campaign_dashboard.html.php");
  121. }
  122. /**
  123. * Render Key Influencers Dashboard page
  124. *
  125. * @method GET
  126. * @route /campaign/:id/report/ki
  127. */
  128. function campaignReportKeyInfluencers($id) {
  129. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  130. // Login the user
  131. return redirect('/login');
  132. }
  133. global $db;
  134. $campaign = $db->select("bi_campaign", "id = :id", array(":id" => $id));
  135. $campaign = $campaign[0];
  136. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  137. layout('dashboard.html.php');
  138. // Getting the user from the session
  139. set('user', $_SESSION['user']);
  140. set('isKI', true);
  141. set('is_campaign', true);
  142. set('currentCampaignId', $id);
  143. set('campaignName', $campaign['name']);
  144. set('campaigns', $campaigns);
  145. set('authId', $_SESSION['authId']);
  146. set('user', $_SESSION['user']);
  147. set('db', $db);
  148. set('title', "Key Influencers");
  149. return render("keyinfluencers.html.php");
  150. }
  151. /**
  152. * Render Market Reaction Dashboard page
  153. *
  154. * @method GET
  155. * @route /campaign/:id/report/twitter/reaction
  156. */
  157. function campaignReportOverallMarketReaction($id) {
  158. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  159. // Login the user
  160. return redirect('/login');
  161. }
  162. global $db;
  163. $campaign = $db->select("bi_campaign", "id = :id", array(":id" => $id));
  164. $campaign = $campaign[0];
  165. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  166. layout('dashboard.html.php');
  167. // Getting the user from the session
  168. set('user', $_SESSION['user']);
  169. set('is_campaign', true);
  170. set('currentCampaignId', $id);
  171. set('isMarketReaction', true);
  172. set('campaignName', $campaign['name']);
  173. set('campaigns', $campaigns);
  174. set('authId', $_SESSION['authId']);
  175. set('user', $_SESSION['user']);
  176. set('db', $db);
  177. set('title', "Market Reaction");
  178. return render("marketreaction_dashboard.html.php");
  179. }
  180. /**
  181. * Render Market Reaction Dashboard page
  182. *
  183. * @method GET
  184. * @route /campaign/:id/report/twitter/reaction
  185. */
  186. function campaignReportTwitterOverallSourceDistribution($id) {
  187. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  188. // Login the user
  189. return redirect('/login');
  190. }
  191. global $db;
  192. $campaign = $db->select("bi_campaign", "id = :id", array(":id" => $id));
  193. $campaign = $campaign[0];
  194. $campaigns = $db->select("bi_campaign", "user_id = :userid", array(":userid" => $_SESSION['authId']));
  195. layout('dashboard.html.php');
  196. // Getting the user from the session
  197. set('user', $_SESSION['user']);
  198. set('is_campaign', true);
  199. set('currentCampaignId', $id);
  200. set('isTwitterSource', true);
  201. set('campaignName', $campaign['name']);
  202. set('campaigns', $campaigns);
  203. set('authId', $_SESSION['authId']);
  204. set('user', $_SESSION['user']);
  205. set('db', $db);
  206. set('title', "Tweet Source Distribution");
  207. return render("sourcedistribution_dashboard.html.php");
  208. }
  209. /**
  210. * Run the Campaign after creating it
  211. *
  212. * @method POST
  213. * @route /campaign/create/run
  214. */
  215. function runCampaign() {
  216. if(!isset($_SESSION['userauth'])) { // Check if the user authenticated before rendering this page
  217. // Login the user
  218. return redirect('/login');
  219. }
  220. global $db;
  221. $tracks = $_POST['campaign_tracks'];
  222. $tracks = explode(",", $tracks);
  223. print_r($tracks);
  224. $service = $_POST['campaign_service'];
  225. $name = $_POST['campaign_name'];
  226. $campaign = array(
  227. "name" => $name, // TODO: May be sometime we should check for possible duplicates
  228. "createdAt" => date("Y-m-d H:i:s"),
  229. "user_id" => $_SESSION['authId'],
  230. );
  231. $db->insert("bi_campaign", $campaign);
  232. $newCid = $db->lastInsertId(); // Get the Id value of the Campaign value that was inserted last
  233. foreach($tracks as $track) {
  234. $t = array(
  235. "name" => strtolower(trim($track)), // Always rename the words to lower string.
  236. "is_archived" => false,
  237. "campaign_id" => $newCid,
  238. "created_at" => date("Y-m-d H:i:s"),
  239. );
  240. // Add the new track to the redis for processing within the next 1 -2 minute(s)
  241. $db->insert("bi_tracks", $t);
  242. }
  243. // Starting the Campaign Threaded Deamon Process
  244. // TODO Need to submit the Campaign to the Storm
  245. // exec("php /home/blueignis/blueignis/spout/service/ThreadedCampaignQueueWorker.php > /home/blueignis/blueignis/spot/service/campaign-" . $newCid . ".log 2>&1 &");
  246. flash('created_new', true);
  247. flash('created_status', 'success');
  248. flash('created_log', 'Your Campaign has been queued and will start momentarily.');
  249. // Now redirect the user to the Campaign Dashboard
  250. return redirect(url_for("/campaign/$newCid"));
  251. }
  252. /**
  253. * Delete a Campaign completely along with its related data
  254. *
  255. * @method GET
  256. * @route ^/campaign/(\d+)/delete
  257. */
  258. function deleteCampaign($id) {
  259. global $db;
  260. $db->run("delete from bi_tweetmood where firehouse_id IN (select id from bi_tweets where campaign_id = :cid); delete from bi_tweets where campaign_id = :cid; delete from bi_tracks where campaign_id = :cid; delete from bi_campaign where id = :cid;", array(":cid" => $id));
  261. return redirect(url_for('/campaign/create'));
  262. }