daily pastebin goal
29%
SHARE
TWEET

PP: PF Install App Pack Code Block

globiws May 29th, 2018 (edited) 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* install app pack script
  2.  * will do the following:
  3.  *  - create an oatuh user using transaction id in url parameter txnid
  4.  *  - get this user to login into podio
  5.  *  - ask user to select a workspace to install the app pack in
  6.  *  - add user to your workspace (so that they are connected to your org)
  7.  *  - install the app pack into the selected workspace
  8.  *  - remove user from your workspace
  9.  */
  10.  
  11. // configuration
  12. $api_service = "ppdemo"; // the name you gave your podio integration on the Account page under "API Services"
  13. $share_id = 12345; // ID of the app market app pack to install
  14. $widget_id = "abckbbmtFVya1"; // ID of the PF website widget serving this code block
  15. $my_customer_space_id = 234567; // podio space to add the customer to
  16. $remove_customer_after = true; // should the customer be removed from the above space after install?
  17. $sucess_message = '<h3>Intallation Successful</h3>' .
  18.     '<p>The Podio Apps have been installed in your selected workspace. ' .
  19.     'To complete the setup and install the Flows, please Refresh your GlobiFlow account ' .
  20.     'by clicking <a href="https://secure.globiflow.com/buildtree.php" target="_blank">THIS LINK</a>.</p>' .
  21.     '<p>Thank you for choosing MYCOMPANYNAME</p>';
  22. // end configuration
  23.  
  24. // get the txnid from the url parameter
  25. $user_id = @json_decode($pf_payload, true)["GET"]['txnid'];
  26. //$user_id = "xxx"; // testing
  27.  
  28. // redirect auth failed
  29. if ( @json_decode($pf_payload, true)["GET"]['oops'] == 1 ) return "Ooops - something went wrong!";
  30.  
  31. // redirect install complete
  32. if ( @json_decode($pf_payload, true)["GET"]['success'] == 1 ) return $sucess_message;
  33.  
  34. if ( empty($user_id) ) return "Missing Transaction ID in url (txn)";
  35.  
  36. // check the current user podio auth token status
  37. try {
  38.     $status = call_pf_script("api_user_token_status.pf", ["api_service" => $api_service, "user_id" => $user_id]);
  39. } catch (Exception $e) {
  40.     $status = false;
  41. }
  42.  
  43. // if they have not logged in redirect them to log in
  44. if ( $status === false || @json_decode($status, true)['auth_token'] === false ) {
  45.     // force him to log in
  46.     $login_link = call_pf_script("api_get_login_link.pf", ["api_service" => $api_service, "user_id" => $user_id]);
  47.     $url_fail = "https://procfuwidgets.b-cdn.net/html/" . $widget_id . "?txnid=".$user_id."&oops=1";
  48.     $url_ok = "https://procfuwidgets.b-cdn.net/html/" . $widget_id . "?txnid=".$user_id."&ok=1";
  49.     call_pf_script("api_user_urls_set.pf", ["api_service" => $api_service, "user_id" => $user_id, "success_url" => $url_ok, "fail_url" => $url_fail]);
  50.     return('<script>document.location.href="' . ($login_link) . '";</script>');
  51. }
  52.  
  53. // get user status
  54. try {
  55.     $request_url = "https://api.podio.com/user";
  56.     $user_status = call_pf_script("api_make_request.pf", ["api_service" => $api_service, "user_id" => $user_id, "request_url" => $request_url, "method" => "GET"]);
  57. } catch (Exception $e) {
  58.     // force him to log in again
  59.     $login_link = call_pf_script("api_get_login_link.pf", ["api_service" => $api_service, "user_id" => $user_id]);
  60.     $url_fail = "https://procfuwidgets.b-cdn.net/html/" . $widget_id . "?txnid=".$user_id."&oops=1";
  61.     $url_ok = "https://procfuwidgets.b-cdn.net/html/" . $widget_id . "?txnid=".$user_id."&ok=1";
  62.     call_pf_script("api_user_urls_set.pf", ["api_service" => $api_service, "user_id" => $user_id, "success_url" => $url_ok, "fail_url" => $url_fail]);
  63.     return('<script>document.location.href="' . ($login_link) . '";</script>');
  64. }
  65.  
  66. // we're logged in now - let user select space to import to
  67.  
  68. // check if we already have a space
  69. $selected_space = intval(@json_decode($pf_payload, true)["GET"]['space']);
  70.  
  71. if ( $selected_space <= 0 ) {
  72.     $orgs = call_pf_script("api_make_request.pf", ["api_service" => $api_service, "user_id" => $user_id, "request_url" => "https://api.podio.com/org/", "method" => "GET"]);
  73.     $orgs = @json_decode($orgs, true);
  74.     if ( $orgs === null ) return "Ooops - something went wrong!";
  75.  
  76.     $html = "<h3>Select Podio Space to install to:</h3>";
  77.     $html .= '<p><select id="space">';
  78.     foreach ( $orgs as $org ) {
  79.         foreach ( $org['spaces'] as $space ) {
  80.             if ( $space['role'] == "admin" ) {
  81.                 $html .= '<option value="'.$space['space_id'].'">'.$org['name'].' &gt; '.$space['name'].'</option>';
  82.             }
  83.         }
  84.     }
  85.     $html .= '</select></p>';
  86.     $html .= '<p><button onclick="select();">Submit</button></p>';
  87.     $html .= '<script>';
  88.     $html .= 'function select() { ';
  89.     $html .= 'var e = document.getElementById("space"); ';
  90.     $html .= 'var val = e.options[e.selectedIndex].value; ';
  91.     $html .= 'var txt = e.options[e.selectedIndex].text; ';
  92.     $html .= 'if ( confirm("Install to "+txt) ) { ';
  93.     $html .= 'var url = "https://procfuwidgets.b-cdn.net/html/' . $widget_id . '?txnid=' . $user_id . '&ok=2&space="+val; ';
  94.     $html .= 'document.location.href=url; ';
  95.     $html .= '} ';
  96.     $html .= '} ';
  97.     $html .= '</script>';
  98.     return $html;
  99. }
  100.  
  101. // we have a space so install
  102.  
  103. try {
  104.     // step 1 - get customer's email address
  105.     $email_address = @json_decode($user_status, true)["mail"];
  106.     $podio_user_id = intval(@json_decode($user_status, true)["user_id"]);
  107.    
  108.     // step 2 - add customer to a space in my org
  109.     call_pf_script("space_add_user.pf", ["space_id" => $my_customer_space_id, "email_address" => $email_address, "role" => "regular", "message" => "Welcome to our space"]);
  110.  
  111.     // step 3 - install app pack
  112.     $request_url = "https://api.podio.com/app_store/".$share_id."/install/";
  113.     $attributes = [
  114.         'space_id' => $selected_space
  115.     ];
  116.     call_pf_script("api_make_request.pf", ["api_service" => $api_service, "user_id" => $user_id, "request_url" => $request_url, "method" => "POST", "headers" => "", "body" => json_encode($attributes)]);
  117.  
  118.     // step 3 - remove customer from my space if needed
  119.     if ( $remove_customer_after ) {
  120.         call_pf_script("space_remove_user.pf", ["space_id" => $my_customer_space_id, "user_id" => $podio_user_id]);
  121.     }
  122.  
  123. } catch (Exception $e) {
  124.     return "Ooops - something went wrong!<br>".$e;
  125. }
  126.  
  127. $url_success = "https://procfuwidgets.b-cdn.net/html/" . $widget_id . "?txnid=".$user_id."&success=1";
  128. return('<script>document.location.href="' . ($url_success) . '";</script>');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top