Advertisement
lmohanarun

Code Clusterpoint

Nov 3rd, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.85 KB | None | 0 0
  1. <?php
  2. // config
  3. $accountId = 102193;
  4. $database = 'URLS';
  5. // Note: Replace 'api-eu' with 'api-us', if you are connecting to US cloud!
  6. $url = 'https://api-us.clusterpoint.com/v4/' . $accountId . '/' . $database;
  7. $userPassword = 'marun2@gmail.com:capitalism';
  8.  
  9. // More about INSERT: https://www.clusterpoint.com/docs/?page=Insert
  10. if (isset($_POST['new_URL'])) {
  11. // insert new URL item
  12. $doc = array(
  13. 'URL' => $_POST['URL'],
  14. 'text' => $_POST['text'],
  15. 'timestamp' => time(),
  16. );
  17.  
  18. $ch = curl_init();
  19. // https://api-eu.clusterpoint.com/v4/ACCOUNT_ID/DATABASE_NAME[ID] - Insert single document with explicit ID
  20. curl_setopt($ch, CURLOPT_URL, $url); // In this case document ID is automatically generated by system.
  21. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  22. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  23. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  24. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($doc));
  27. $response = curl_exec($ch);
  28. $errorMsg = curl_error($ch);
  29. if ($errorMsg) {
  30. var_dump($errorMsg);
  31. }
  32. curl_close($ch);
  33.  
  34. die(); // AJAX request, just kill it.
  35. }
  36.  
  37.  
  38. // More about UPDATE: https://www.clusterpoint.com/docs/?page=Update
  39. if (isset($_POST['update_id'])) {
  40.  
  41. $query = 'UPDATE URLS["' . addslashes($_POST['update_id']) . '"] SET text = "' . addslashes($_POST['text']) . '", URL = "' . addslashes($_POST['URL']) . '"';
  42.  
  43. $ch = curl_init();
  44. curl_setopt($ch, CURLOPT_URL, $url . '/_query');
  45. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  46. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // or use PATCH request https://www.clusterpoint.com/docs/?page=Update
  47. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  48. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  51. $response = curl_exec($ch);
  52. $errorMsg = curl_error($ch);
  53. if ($errorMsg) {
  54. var_dump($errorMsg);
  55. }
  56. curl_close($ch);
  57.  
  58. die(); // AJAX request, just kill it.
  59. }
  60.  
  61.  
  62. if (isset($_POST['delete_id'])) {
  63. $ch = curl_init();
  64. curl_setopt($ch, CURLOPT_URL, $url . '['.$_POST['delete_id'].']');
  65. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  66. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. $response = curl_exec($ch);
  71. $errorMsg = curl_error($ch);
  72. if ($errorMsg) {
  73. var_dump($errorMsg);
  74. }
  75. curl_close($ch);
  76. die(); // AJAX request, just kill it.
  77. }
  78.  
  79.  
  80. // More about QUERY: https://www.clusterpoint.com/docs/?page=Query
  81. $ch = curl_init();
  82. $ocs = "";
  83. $query = "SELECT _id,text,URL,timestamp FROM URLS ORDER BY timestamp DESC LIMIT 0,300";
  84. if (isset($_GET['occupation-id']))
  85. {
  86. $ocs = $_GET['occupation-id'];
  87. $query = "SELECT _id,text,URL,timestamp FROM URLS WHERE CONTAINS('" . $ocs . "') ORDER BY timestamp DESC LIMIT 0,300";
  88. }
  89. //$query = 'SELECT text,URL,COUNT(URL),timestamp FROM URLS WHERE text.CONTAINS("*'.addslashes($_GET['occupation-id']).'*") LIMIT 0,200';
  90. // More info: https://www.clusterpoint.com/docs/?page=Matching_Documents_with_WHERE
  91. curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
  92. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  93. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  94. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  95. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  98. $response = json_decode(curl_exec($ch));
  99. $errorMsg = curl_error($ch);
  100. if ($errorMsg) {
  101. var_dump($errorMsg);
  102. }
  103. curl_close($ch);
  104. $URLS = array();
  105. $inc = 0;
  106. foreach ($response->results as $document) {
  107. $inc++;
  108. //$btnUpdate = '<button onclick="updateItem(\'' . $document->_id . '\',' . $inc . ')">update</button> ';
  109. $btnDelete = '<button onclick="deleteItem(\"' . (string)$document->_id . '\")">delete</button>';
  110. $URLOutput = "Link number:" . $inc . ": <a target=_blank href=\"" . (string)$document->URL . "\">" . (string)$document->URL . "</a><br>";
  111. $DOCID = "Doc id:" . (string)$document->_id . "<br>";
  112. //$URLInput = '<input type="text" style="background-color:lightgreen;" readonly size="120" name="URL[]" value="' . (string)$document->URL . '" id="URL_' . $inc . '"> ';
  113. //$textInput = '<br><input type="text" name="text[]" id="text_' . $inc . '" value="' .(string)$document->text . '">';
  114. $textOutput = '<p style="color:brown"><i>' .(string)$document->text . '</i></p>';
  115. $mytimestamp = 'Date added: ' . date('Y-m-d H:i',(string)$document->timestamp);
  116. //$URLS[] = '<li> ' . $URLInput . $textInput . $btnUpdate . $btnDelete . '</li>';
  117. $URLS[] = '<li>' . $URLOutput . $DOCID . $textOutput . $mytimestamp . $btnDelete . '</li>';
  118. }
  119.  
  120. // More about Aggregation: https://www.clusterpoint.com/docs/?page=Aggregation_with_GROUP_BY
  121. /*
  122. $ch = curl_init();
  123. $query = 'SELECT URL, COUNT(URL) AS count FROM URLS GROUP BY priority ORDER BY priority DESC';
  124. curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
  125. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  126. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  129. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  130. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  131. $response = json_decode(curl_exec($ch));
  132. $errorMsg = curl_error($ch);
  133. if ($errorMsg) {
  134. var_dump($errorMsg);
  135. }
  136. curl_close($ch);
  137.  
  138. $stats = array();
  139. foreach ($response->results as $document) {
  140. $stats[] = '<li>' . $document->count . ' tasks with priority: ' . $document->priority . ' </li>';
  141. }
  142. */
  143. ?>
  144. <!doctype html>
  145. <head>
  146. <meta charset="utf-8">
  147. <title>LinkColumbus the writers block solver</title>
  148. <!-- <link rel="import" href="../statcounter-include.html"> Inline include-->
  149. <!-- Start of StatCounter Code for BBEdit (Mac) -->
  150. <script type="text/javascript">
  151. //<![CDATA[
  152. var sc_project=10656646;
  153. var sc_invisible=0;
  154. var sc_security="6f5323f6";
  155. var scJsHost = (("https:" == document.location.protocol) ?
  156. "https://secure." : "http://www.");
  157. document.write("<sc"+"ript type='text/javascript' src='" +
  158. scJsHost+
  159. "statcounter.com/counter/counter_xhtml.js'></"+"script>");
  160. //]]>
  161. </script>
  162. <noscript><div class="statcounter"><a title="hit counter"
  163. href="http://statcounter.com/" class="statcounter"><img
  164. class="statcounter"
  165. src="http://c.statcounter.com/10656646/0/6f5323f6/0/"
  166. alt="hit counter" /></a></div></noscript>
  167. <!-- End of StatCounter Code for BBEdit (Mac) -->
  168. <a target=_blank href="http://statcounter.com/p10656646/?guest=1">View My Stats</a>
  169. <!--Start of Zopim Live Chat Script-->
  170. <script type="text/javascript">
  171. window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
  172. d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
  173. _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
  174. $.src="//v2.zopim.com/?3OEmwjPqSV4goKBnrct9k32AQkWeres8";z.t=+new Date;$.
  175. type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
  176. </script>
  177. <!--End of Zopim Live Chat Script-->
  178. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  179. <style type="text/css">
  180. body {
  181. width: 800px;
  182. margin: 0 auto;
  183. }
  184.  
  185. ul {
  186. list-style: none;
  187. padding: 0;
  188. }
  189.  
  190. ul li {
  191. border: 1px solid gray;
  192. padding: 5px
  193. }
  194. /*input[type="text"] {
  195. width: 400px;
  196. }*/
  197. </style>
  198. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  199. <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  200. <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  201. <style>
  202. #occupation-label {
  203. display: block;
  204. }
  205. </style>
  206. <script>
  207. $(function() {
  208. var occupations = [
  209. {
  210. value: "copywriter",
  211. label: "copywriter",
  212. },
  213. {
  214. value: "Hollywood",
  215. label: "Hollywood",
  216. },
  217. {
  218. value: "All audiences",
  219. label: "All audiences",
  220. },
  221. {
  222. value: "medical professional",
  223. label: "medical professional",
  224. },
  225. {
  226. value: "graphic designer",
  227. label: "graphic designer",
  228. },
  229. {
  230. value: "legal professional",
  231. label: "legal professional",
  232. },
  233. {
  234. value: "MBA professor",
  235. label: "MBA professor",
  236. },
  237. {
  238. value: "Human Rights Watch activist",
  239. label: "Human Rights Watch activist",
  240. },
  241. {
  242. value: "Silicon Valley Investor",
  243. label: "Silicon Valley Investor",
  244. },
  245. ];
  246. $( "#occupation-id" ).autocomplete({
  247. minLength: 0,
  248. source: occupations,
  249. focus: function( event, ui ) {
  250. $( "#occupation-id" ).val( ui.item.label );
  251. $( "#occupation-id" ).val( ui.item.value );
  252. return false;
  253. },
  254. select: function( event, ui ) {
  255. $( "#occupation-id" ).val( ui.item.label );
  256. $( "#occupation-id" ).val( ui.item.value );
  257. return false;
  258. }
  259. })
  260. })();
  261. </script>
  262. </head>
  263. <body>
  264. <h2>What if you could peek into other people's manually curated bookmarks? <a href="URLS.php">LinkColumbus</a></h2>
  265. <h3>Testers wanted! Please test out this small webpage in your smartphones. My contact email is marun2@gmail.com</h3>
  266. <h5><p>Data is stored in clusterpoint.com MongoDB NOSQL database JSON document store as a scalable cluster with redundant servers and 10 shards. They have two separate datacenters available US-Dallas, Texas and EU-Riga, Latvia. They have a gratuitous 10 GB free cloud database free to those who are starting with DbAAS (database as a service). This is somewhat like Amazon RDS so is nothing new but the innovation is making this accessible to you and me. This application is based on the following software technologies: PHP, REST, JSON, Web services, jQuery, HTML5/CSS.
  267. When you create Cloud account you choose which of servers to use (for data storage and retrieval) - US or EU.
  268. Database is distributed among shards and replicated on replicas to increase performance and ensure fault tolerance and durability.
  269. </p></h5>
  270. <img src="gears.png">
  271. <p>The idea machine has twentyfour gears. Or a hundred other peoples' bookmarks to start with. So starts the journey of solving writers block.</p>
  272. <p>Mainly intended to be use for copywriters to solve writers block. How to use it: Use it as a springboard_of_ideas for what to write. Dont hit the writers block. Search for "informal writing" if you are trying to brainstorm up something to write about informal writing and you will find a paul graham article. OR as usual, you can navigate all the links by just choosing copywriter and click Search.</p><br>
  273. <form action="URLS.php" method="get">
  274. Choose an occupation(simply press down arrow when inside the textbox): OR search like you do google.<br>
  275. <input id="occupation-id" value="<? echo @$_GET['occupation-id'] ?>" name="occupation-id">
  276. <button type="submit">Search</button>
  277. </form>
  278. <br><br><br>
  279. <b>Here are <? echo count($URLS) ?> search results for <? echo (@$_GET['occupation-id'] == '' ? "all":@$_GET['occupation-id']) ?><br>
  280. <ul>
  281. <?php echo implode('', $URLS) ?>
  282. </ul>
  283. <ul>
  284. <li>
  285. <p>Add new bookmark: Please double check before adding. If you mistakenly add something, click delete on what you added.</p>
  286. <input type="text" size="90" style="background-color:yellow;" name="new_URL" value="" id="new_URL" placeholder="URL"/><br>
  287. <Textarea name="new_text" style="background-color:yellow;" rows=4 cols=90 value="" id="new_text" placeholder="Text"></textarea>
  288. <button onclick="createItem()">Add</button>
  289. </li>
  290. </ul>
  291.  
  292. <script type="text/javascript">
  293. function createItem() {
  294. $.post('', {
  295. new_URL: true,
  296. URL: $('#new_URL').val(),
  297. text: $('#new_text').val()
  298. },
  299. function () {
  300. alert('Item created!');
  301. location.reload();
  302. });
  303. }
  304.  
  305. function updateItem(docId, fieldsId) {
  306. $.post('', {
  307. update_id: docId,
  308. text: $('#text_' + fieldsId).val(),
  309. URL: $('#URL_' + fieldsId).val()
  310. },
  311. function () {
  312. alert('Item updated!');
  313. location.reload();
  314. });
  315. }
  316.  
  317. function deleteItem(_id) {
  318. $.post('', { delete_id: _id },
  319. function () {
  320. alert('Item deleted!');
  321. location.reload();
  322. });
  323. }
  324. </script>
  325. </body>
  326. </html>
  327. <!--
  328. .autocomplete( "instance" )._renderItem = function( ul, ui.item.value ) {
  329. return $( "<li>" )
  330. .append( "<a>" + ui.item.label + "<br>" + "</a>" )
  331. .appendTo( ul );
  332. };
  333. -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement