Advertisement
lmohanarun

Clusterpoint JSON document store

Oct 23rd, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.04 KB | None | 0 0
  1. <?php
  2. // config
  3. $accountId = 102193;
  4. $database = 'todo';
  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'; //this is a dummy password and you should signup for your own at clusterpoint.com
  8.  
  9. // More about INSERT: https://www.clusterpoint.com/docs/?page=Insert
  10. if (isset($_POST['new_priority'])) {
  11.     // insert new to-do item
  12.     $doc = array(
  13.         'priority' => $_POST['new_priority'],
  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 todo["' . addslashes($_POST['update_id']) . '"] SET text = "' . addslashes($_POST['text']) . '", priority = "' . addslashes($_POST['priority']) . '"';
  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 todo 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 todo 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. $todos = 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.     $priorityInput = date('Y-m-d H:i:s', (string)$document->timestamp) . '
  110.        <input type="number" name="priority[]" value="' . (string)$document->priority . '" id="priority_' . $inc . '"> ';
  111.  
  112.     $textInput = '<input type="text" name="text[]" value="' . (string)$document->text . '" id="text_' . $inc . '"> ';
  113.     $todos[] = '<li>  ' . $priorityInput . $textInput . $btnUpdate . $btnDelete . '</li>';
  114. }
  115.  
  116.  
  117. // More about Aggregation: https://www.clusterpoint.com/docs/?page=Aggregation_with_GROUP_BY
  118. $ch = curl_init();
  119. $query = 'SELECT priority, COUNT(priority) AS count FROM todo GROUP BY priority ORDER BY priority DESC';
  120. curl_setopt($ch, CURLOPT_URL, $url . '/_query'); // We added _query here!
  121. curl_setopt($ch, CURLOPT_USERPWD, $userPassword);
  122. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  124. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  125. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  126. curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
  127. $response = json_decode(curl_exec($ch));
  128. $errorMsg = curl_error($ch);
  129. if ($errorMsg) {
  130.     var_dump($errorMsg);
  131. }
  132. curl_close($ch);
  133.  
  134. $stats = array();
  135. foreach ($response->results as $document) {
  136.     $stats[] = '<li>' . $document->count . ' tasks with priority: ' . $document->priority . ' </li>';
  137. }
  138.  
  139. ?>
  140.     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  141.     <html>
  142.     <head>
  143.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  144.  
  145.         <title>Simple To-Do list</title>
  146.         <style type="text/css">
  147.             body {
  148.                 width: 600px;
  149.                 margin: 0 auto;
  150.             }
  151.  
  152.             ul {
  153.                 list-style: none;
  154.                 padding: 0;
  155.             }
  156.  
  157.             ul li {
  158.                 border: 1px solid gray;
  159.                 padding: 5px
  160.             }
  161.  
  162.             input[type="number"] {
  163.                 width: 70px;
  164.             }
  165.         </style>
  166.     </head>
  167.     <body>
  168.  
  169.  
  170.     <h1>To-Do list:</h1>
  171.     <form action="" method="get">
  172.         Search: <input type="text" value="<? echo @$_GET['search'] ?>" name="search"/>
  173.         <button type="submit">Search</button>
  174.     </form>
  175.     <ul>
  176.         <?php echo implode('', $todos) ?>
  177.     </ul>
  178.     <ul>
  179.         <? echo implode('', $stats) ?>
  180.     </ul>
  181.     <ul>
  182.         <li>
  183.             <p>Add new item:</p>
  184.             <input type="number" name="priority" value="" id="new_priority" placeholder="Priority"/>
  185.             <input type="text" name="todo" value="" id="new_text" placeholder="Text"/>
  186.             <button onclick="createItem()">Add</button>
  187.         </li>
  188.     </ul>
  189.  
  190.     </body>
  191.     </html>
  192.     <script type="text/javascript">
  193.         function createItem() {
  194.             $.post('', {
  195.                     new_priority: $('#new_priority').val(),
  196.                     new_text:     $('#new_text').val()
  197.                 },
  198.                 function () {
  199.                     alert('Item created!');
  200.                     location.reload();
  201.                 });
  202.         }
  203.  
  204.         function updateItem(docId, fieldsId) {
  205.             $.post('', {
  206.                     update_id: docId,
  207.                     text:      $('#text_' + fieldsId).val(),
  208.                     priority:  $('#priority_' + fieldsId).val()
  209.                 },
  210.                 function () {
  211.                     alert('Item updated!');
  212.                     location.reload();
  213.                 });
  214.         }
  215.  
  216.         function deleteItem(id) {
  217.             $.post('', { delete_id: id },
  218.               function () {
  219.                   alert('Item deleted!');
  220.                   location.reload();
  221.               });
  222.         }
  223.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement