Advertisement
lmohanarun

Clusterpoint

Oct 23rd, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.50 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['new_URL'],
  14.         'text' => $_POST['new_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. $query = 'SELECT * FROM URLS ORDER BY \'timestamp\' DESC';
  83. // search
  84. if (isset($_GET['search']) && $_GET['search'] !== '') {
  85.     // We are storing item text in field named "text"
  86.     $query = 'SELECT * FROM URLS WHERE text.CONTAINS("*'.addslashes($_GET['search']).'*") ORDER BY \'timestamp\' DESC';
  87.     // More info: https://www.clusterpoint.com/docs/?page=Matching_Documents_with_WHERE
  88. }
  89. curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
  90. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  91. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  93. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  95. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  96. $response = json_decode(curl_exec($ch));
  97. $errorMsg = curl_error($ch);
  98. if ($errorMsg) {
  99.     var_dump($errorMsg);
  100. }
  101. curl_close($ch);
  102. $URLS = array();
  103. $inc = 0;
  104. foreach ($response->results as $document) {
  105.     $inc++;
  106.     $btnUpdate = '<button onclick="updateItem(\'' . $document->_id . '\',' . $inc . ')">update</button> ';
  107.     $btnDelete = '<button onclick="deleteItem(\'' . $document->_id . '\',' . $inc . ')">delete</button> ';
  108.  
  109.     $URLInput = date('Y-m-d H:i:s', (string)$document->timestamp) . '<input type="text" name="URL[]" value="' . (string)$document->URL . '" id="URL_' . $inc . '"> ';
  110.     $textInput = '<input type="text" name="text[]" value="' . (string)$document->text . '" id="text_' . $inc . '"> ';
  111.     $URLS[] = '<li>  ' . $URLInput . $textInput . $btnUpdate . $btnDelete . '</li>';
  112. }
  113.  
  114.  
  115. // More about Aggregation: https://www.clusterpoint.com/docs/?page=Aggregation_with_GROUP_BY
  116. /*
  117. $ch = curl_init();
  118. $query = 'SELECT URL, COUNT(URL) AS count FROM URLS GROUP BY priority ORDER BY priority DESC';
  119. curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
  120. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  121. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  122. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  124. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  125. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  126. $response = json_decode(curl_exec($ch));
  127. $errorMsg = curl_error($ch);
  128. if ($errorMsg) {
  129.     var_dump($errorMsg);
  130. }
  131. curl_close($ch);
  132.  
  133. $stats = array();
  134. foreach ($response->results as $document) {
  135.     $stats[] = '<li>' . $document->count . ' tasks with priority: ' . $document->priority . ' </li>';
  136. }
  137. */
  138. ?>
  139.     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  140.     <html>
  141.     <head>
  142.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  143.  
  144.         <title>My public bookmarks, my comments searchable</title>
  145.         <link rel="import" href="statcounter-include.html">
  146. <!--<script type="text/javascript" src="zopim-include.js" defer></script>-->
  147. <!--Start of Zopim Live Chat Script-->
  148. <script type="text/javascript">
  149. window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
  150. d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
  151. _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
  152. $.src="//v2.zopim.com/?3OEmwjPqSV4goKBnrct9k32AQkWeres8";z.t=+new Date;$.
  153. type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
  154. </script>
  155. <!--End of Zopim Live Chat Script-->
  156.         <style type="text/css">
  157.             body {
  158.                 width: 600px;
  159.                 margin: 0 auto;
  160.             }
  161.  
  162.             ul {
  163.                 list-style: none;
  164.                 padding: 0;
  165.             }
  166.  
  167.             ul li {
  168.                 border: 1px solid gray;
  169.                 padding: 5px
  170.             }
  171.  
  172.             input[type="text"] {
  173.                 width: 300px;
  174.             }
  175.         </style>
  176.     </head>
  177.     <body>
  178.  
  179.  
  180.     <h1>URL list:</h1>
  181.     <form action="" method="get">
  182.         Search: <input type="text" value="<? echo @$_GET['search'] ?>" name="search"/>
  183.         <button type="submit">Search</button>
  184.     </form>
  185.     <ul>
  186.         <?php echo implode('', $URLS) ?>
  187.     </ul>
  188.  
  189.     <ul>
  190.         <? //echo implode('', $stats) ?>
  191.     </ul>
  192.     <ul>
  193.         <li>
  194.             <p>Add new item:</p>
  195.             <input type="text" name="new_URL" value="" id="new_URL" placeholder="URL"/>
  196.             <input type="text" name="new_text" value="" id="new_text" placeholder="Text"/>
  197.             <button onclick="createItem()">Add</button>
  198.         </li>
  199.     </ul>
  200.  
  201.     </body>
  202.     </html>
  203.     <script type="text/javascript">
  204.         function createItem() {
  205.             $.post('', {
  206.                     URL: $('#new_URL').val(),
  207.                     text:     $('#new_text').val()
  208.                 },
  209.                 function () {
  210.                     alert('Item created!');
  211.                     location.reload();
  212.                 });
  213.         }
  214.  
  215.         function updateItem(docId, fieldsId) {
  216.             $.post('', {
  217.                     update_id: docId,
  218.                     text:      $('#text_' + fieldsId).val(),
  219.                     URL:  $('#URL_' + fieldsId).val()
  220.                 },
  221.                 function () {
  222.                     alert('Item updated!');
  223.                     location.reload();
  224.                 });
  225.         }
  226.  
  227.         function deleteItem(id) {
  228.             $.post('', { delete_id: id },
  229.               function () {
  230.                   alert('Item deleted!');
  231.                   location.reload();
  232.               });
  233.         }
  234.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement