PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/interfaces/admin/components/pages/controllers/elements_edit.php

https://gitlab.com/x33n/platform
PHP | 169 lines | 143 code | 17 blank | 9 comment | 33 complexity | 917326932443b215820545cee03e7a04 MD5 | raw file
  1. <?php
  2. if (!$request_parameters) {
  3. AdminHelper::controllerRedirect('/');
  4. }
  5. $current_element = $cash_admin->setCurrentElement($request_parameters[0]);
  6. if ($current_element) {
  7. $cash_admin->page_data['form_state_action'] = 'doelementedit';
  8. $cash_admin->page_data = array_merge($cash_admin->page_data,$current_element);
  9. $effective_user = $cash_admin->effective_user_id;
  10. if ($current_element['user_id'] == $effective_user) {
  11. // handle template change
  12. if (isset($_POST['change_template_id'])) {
  13. if ($_POST['change_template_id'] != $_POST['current_template_id']) {
  14. $new_template_id = $cash_admin->requestAndStore(
  15. array(
  16. 'cash_request_type' => 'element',
  17. 'cash_action' => 'setelementtemplate',
  18. 'element_id' => $request_parameters[0],
  19. 'template_id' => $_POST['change_template_id']
  20. )
  21. );
  22. if ($new_template_id) {
  23. if ($_POST['current_template_id'] > 0) {
  24. // delete old custom templates
  25. $cash_admin->requestAndStore(
  26. array(
  27. 'cash_request_type' => 'system',
  28. 'cash_action' => 'deletetemplate',
  29. 'template_id' => $_POST['current_template_id']
  30. )
  31. );
  32. }
  33. $cash_admin->page_data['template_id'] = $_POST['change_template_id'];
  34. }
  35. }
  36. }
  37. // deal with templates
  38. $embed_templates = AdminHelper::echoTemplateOptions('embed',$cash_admin->page_data['template_id']);
  39. $cash_admin->page_data['template_options'] = $embed_templates;
  40. if ($cash_admin->page_data['template_id'] >= 0) {
  41. $cash_admin->page_data['custom_template'] = true;
  42. }
  43. $analytics = $cash_admin->requestAndStore(
  44. array(
  45. 'cash_request_type' => 'element',
  46. 'cash_action' => 'getanalytics',
  47. 'analtyics_type' => 'elementbasics',
  48. 'element_id' => $request_parameters[0],
  49. 'user_id' => $cash_admin->effective_user_id
  50. )
  51. );
  52. $cash_admin->page_data['total_views'] = 0;
  53. if (is_array($analytics['payload'])) {
  54. $cash_admin->page_data['total_views'] = CASHSystem::formatCount($analytics['payload']['total']);
  55. $methods_array = array();
  56. $locations_array = array();
  57. foreach ($analytics['payload']['methods'] as $method => $total) {
  58. $methods_string = array ('direct','api_public','api_key','api_fullauth');
  59. $methods_translation = array('direct (embedded on this site)','api_public (shared to another site)','api_key (shared to another site)','api_fullauth (another site with your API credentials)');
  60. $methods_array[] = array(
  61. 'access_method' => str_replace($methods_string,$methods_translation,$method),
  62. 'total' => $total
  63. );
  64. }
  65. foreach ($analytics['payload']['locations'] as $location => $total) {
  66. $locations_array[] = array(
  67. 'access_location' => $location,
  68. 'total' => $total
  69. );
  70. }
  71. $tmp_locations_array = array(); // temp array to combine totals by hostname
  72. foreach ($locations_array as $key => $location) {
  73. // cycle through all locations, push to temp array and combine if necessary
  74. $parsed = parse_url($location['access_location']);
  75. if (isset($tmp_locations_array[$parsed['host']])) {
  76. $tmp_locations_array[$parsed['host']] = $tmp_locations_array[$parsed['host']] + $location['total'];
  77. } else {
  78. $tmp_locations_array[$parsed['host']] = $location['total'];
  79. }
  80. }
  81. arsort($tmp_locations_array); // sort temp array most to least
  82. $tmp_locations_array = array_slice($tmp_locations_array, 0, 5); // trim temp array to no more than 5
  83. $locations_array = array(); // let's rebuild the locations array
  84. foreach ($tmp_locations_array as $location => $total) {
  85. $locations_array[] = array(
  86. 'access_location' => $location,
  87. 'total' => $total
  88. );
  89. }
  90. $cash_admin->page_data['location_analytics'] = new ArrayIterator($locations_array);
  91. $cash_admin->page_data['method_analytics'] = new ArrayIterator($methods_array);
  92. }
  93. // Detects if element add has happened and deals with POST data if it has
  94. AdminHelper::handleElementFormPOST($_POST,$cash_admin);
  95. // Set basic id/name stuff for the element
  96. AdminHelper::setBasicElementFormData($cash_admin);
  97. $app_json = AdminHelper::getElementAppJSON($current_element['type']);
  98. if ($app_json) {
  99. foreach ($app_json['options'] as $section_name => $details) {
  100. foreach ($details['data'] as $data => $values) {
  101. // 95% of the time all options will be set, but we check in case NEW options
  102. // have been added to the app.json definition since this element was first added
  103. if (isset($current_element['options'][$data])) {
  104. if ($values['type'] == 'select') {
  105. $default_val = AdminHelper::echoFormOptions(str_replace('/','_',$values['values']),$current_element['options'][$data],false,true);
  106. } else {
  107. $default_val = $current_element['options'][$data];
  108. }
  109. } else {
  110. // option not defined, so instead spit out defaults
  111. if (isset($values['default']) && $values['type'] !== 'select') {
  112. if ($values['type'] == 'boolean') {
  113. if ($values['default']) {
  114. $default_val = true;
  115. }
  116. } else if ($values['type'] == 'number') {
  117. $default_val = $values['default'];
  118. } else {
  119. $default_val = $values['default']['en'];
  120. }
  121. }
  122. if ($values['type'] == 'select') {
  123. $default_val = AdminHelper::echoFormOptions(str_replace('/','_',$values['values']),0,false,true);
  124. }
  125. }
  126. $cash_admin->page_data['options_' . $data] = $default_val;
  127. }
  128. }
  129. $cash_admin->page_data['ui_title'] = '' . $current_element['name'] . '';
  130. $cash_admin->page_data['element_button_text'] = 'Edit the element';
  131. $cash_admin->page_data['element_rendered_content'] = $cash_admin->mustache_groomer->render(AdminHelper::getElementTemplate($current_element['type']), $cash_admin->page_data);
  132. }
  133. $campaign_response = $cash_admin->requestAndStore(
  134. array(
  135. 'cash_request_type' => 'element',
  136. 'cash_action' => 'getcampaignforelement',
  137. 'id' => $current_element['id']
  138. )
  139. );
  140. if ($campaign_response['payload']) {
  141. $cash_admin->page_data['campaign_id'] = $campaign_response['payload']['id'];
  142. $cash_admin->page_data['campaign_title'] = $campaign_response['payload']['title'];
  143. }
  144. } else {
  145. AdminHelper::controllerRedirect('/elements/');
  146. }
  147. } else {
  148. AdminHelper::controllerRedirect('/elements/');
  149. }
  150. $cash_admin->page_data['platform_path'] = CASH_PLATFORM_PATH;
  151. $cash_admin->setPageContentTemplate('elements_details');
  152. ?>