uc_cart_links.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @file
  4. * Cart Links administration menu items.
  5. */
  6. /**
  7. * Defines a form to configure the Cart Links settings.
  8. *
  9. * @see uc_cart_links_settings_form_validate()
  10. * @ingroup forms
  11. */
  12. function uc_cart_links_settings_form($form, &$form_state) {
  13. $form['uc_cart_links_add_show'] = array(
  14. '#type' => 'checkbox',
  15. '#title' => t('Display the cart link product action when you add a product to your cart.'),
  16. '#default_value' => variable_get('uc_cart_links_add_show', FALSE),
  17. );
  18. $form['uc_cart_links_track'] = array(
  19. '#type' => 'checkbox',
  20. '#title' => t('Track clicks through Cart Links that specify tracking IDs.'),
  21. '#default_value' => variable_get('uc_cart_links_track', TRUE),
  22. );
  23. $form['uc_cart_links_empty'] = array(
  24. '#type' => 'checkbox',
  25. '#title' => t('Allow Cart Links to empty customer carts.'),
  26. '#default_value' => variable_get('uc_cart_links_empty', TRUE),
  27. );
  28. $form['uc_cart_links_messages'] = array(
  29. '#type' => 'textarea',
  30. '#title' => t('Cart Links messages'),
  31. '#description' => t('Enter messages available to the Cart Links API for display through a link. Separate messages with a line break. Each message should have a numeric key and text value, separated by "|". For example: 1337|Message text.'),
  32. '#default_value' => variable_get('uc_cart_links_messages', ''),
  33. );
  34. $form['uc_cart_links_restrictions'] = array(
  35. '#type' => 'textarea',
  36. '#title' => t('Cart Links restrictions'),
  37. '#description' => t('To restrict what Cart Links may be used on your site, enter all valid Cart Links in this textbox. Separate links with a line break. Leave blank to permit any cart link.'),
  38. '#default_value' => variable_get('uc_cart_links_restrictions', ''),
  39. );
  40. $form['uc_cart_links_invalid_page'] = array(
  41. '#type' => 'textfield',
  42. '#title' => t('Invalid link redirect page'),
  43. '#description' => t('Enter the URL to redirect to when an invalid cart link is used.'),
  44. '#default_value' => variable_get('uc_cart_links_invalid_page', ''),
  45. '#size' => 32,
  46. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  47. );
  48. return system_settings_form($form);
  49. }
  50. /**
  51. * Validation handler for uc_cart_links_settings form.
  52. *
  53. * @see uc_cart_links_settings_form()
  54. */
  55. function uc_cart_links_settings_form_validate($form, &$form_state) {
  56. $messages = (string) $form_state['values']['uc_cart_links_messages'];
  57. if (!empty($messages)) {
  58. $data = explode("\n", $messages);
  59. foreach ($data as $message) {
  60. // Ignore blank lines.
  61. if (preg_match('/^\s*$/', $message)) {
  62. continue;
  63. }
  64. // Check for properly formattted messages.
  65. // Each line must be one or more numeric characters for the key followed
  66. // by "|" followed by one or more characters for the value. Both the key
  67. // and the value may have leading and/or trailing whitespace.
  68. elseif (!preg_match('/^\s*[1-9][0-9]*\s*\|\s*\S+.*$/', $message)) {
  69. form_set_error('uc_cart_links_messages', t('Invalid Cart Links message "%message". Messages must be a numeric key followed by "|" followed by a value.', array('%message' => $message)));
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * Displays the Cart Links report.
  77. *
  78. * @return
  79. * HTML output.
  80. */
  81. function uc_cart_links_report() {
  82. $header = array(
  83. array('data' => t('ID'), 'field' => 'cart_link_id'),
  84. array('data' => t('Clicks'), 'field' => 'clicks'),
  85. array('data' => t('Last click'), 'field' => 'last_click', 'sort' => 'desc'),
  86. );
  87. $query = db_select('uc_cart_link_clicks')->extend('PagerDefault')->extend('TableSort')
  88. ->fields('uc_cart_link_clicks')
  89. ->limit(25)
  90. ->element(1)
  91. ->orderByHeader($header);
  92. $rows = array();
  93. $result = $query->execute();
  94. foreach ($result as $data) {
  95. $rows[] = array(
  96. check_plain($data->cart_link_id),
  97. $data->clicks,
  98. format_date($data->last_click, 'short'),
  99. );
  100. }
  101. $build['report'] = array(
  102. '#theme' => 'table',
  103. '#header' => $header,
  104. '#rows' => $rows,
  105. '#empty' => t('No Cart Links have been tracked yet.'),
  106. );
  107. $build['pager'] = array(
  108. '#theme' => 'pager',
  109. '#element' => 1,
  110. );
  111. return $build;
  112. }
  113. /**
  114. * Provides instructions on how to create Cart Links.
  115. *
  116. * @return
  117. * Form API array with help text.
  118. */
  119. function uc_cart_links_creation_help() {
  120. $build = array(
  121. '#prefix' => '<p>',
  122. '#suffix' => '</p>',
  123. );
  124. $build['introduction'] = array(
  125. '#prefix' => '<p>',
  126. '#markup' => t("Cart Links allow you to craft links that add products to customer shopping carts and redirect customers to any page on the site. A store owner might use a Cart Link as a 'Buy it now' link in an e-mail, in a blog post, or on any page, either on or off site. These links may be identified with a unique ID, and clicks on these links may be reported to the administrator in order to track the effectiveness of each unique ID. You may track affiliate sales, see basic reports, and make sure malicious users don't create unapproved links."),
  127. '#suffix' => '</p>',
  128. );
  129. $build['uses'] = array(
  130. '#prefix' => t('The following actions may be configured to occur when a link is clicked:'),
  131. '#theme' => 'item_list',
  132. '#items' => array(
  133. t("Add any quantity of any number of products to the customer's cart, with specific attributes and options for each added product, if applicable."),
  134. t('Display a custom message to the user.'),
  135. t('Track the click for display on a store report.'),
  136. t("Empty the customer's shopping cart."),
  137. t('Redirect to any page on the site.'),
  138. ),
  139. );
  140. $build['suggestions'] = array(
  141. '#prefix' => '<p>',
  142. '#markup' => t('A Cart Link URL looks like:<blockquote><code>/cart/add/<em>&lt;cart_link_content&gt;</em></code></blockquote>where <code><em>&lt;cart_link_content&gt;</em></code> consists of one or more actions separated by a dash. Absolute URLs may also be used, e.g.:<blockquote><code>http://www.example.com/cart/add/<em>&lt;cart_link_content&gt;</em></code></blockquote>'),
  143. '#suffix' => '</p>',
  144. );
  145. // t('Specify the redirection by adding ?destination=url where url is the page to go to.'),
  146. $header = array(t('Action'), t('Description'), t('Argument'));
  147. $rows = array(
  148. array('p', t('Adds a product to the cart.'), t('A product node number, followed by optional arguments described in the table below.')),
  149. array('i', t('Sets the ID of the cart link.'), t('An alphanumeric string (32 characters max) to identify the link.')),
  150. array('m', t('Displays a message to the customer when the link is clicked.'), t('A <a href="!url">numeric message ID</a> to identify which message to display.', array('!url' => url('admin/store/settings/cart-links')))),
  151. array('e', t('Empties the cart. If used, this should be the first action.'), t('None.')),
  152. );
  153. $build['commands'] = array(
  154. '#prefix' => t('Allowed actions are:'),
  155. '#theme' => 'table',
  156. '#header' => $header,
  157. '#rows' => $rows,
  158. );
  159. $build['required'] = array(
  160. '#prefix' => '<p>',
  161. '#markup' => t('The only required part of the <code><em>&lt;cart_link_content&gt;</em></code> is the "p" action, which must be immediately followed by a product node number. For example, to add product node 23 to a cart, use the following:<blockquote><code>/cart/add/p23</code></blockquote>To use this on your site, simply create an HTML anchor tag referencing your Cart Link URL:<blockquote><code>&lt;a href="http://www.example.com/cart/add/p23"&gt;Link text.&lt;/a&gt;</code></blockquote>'),
  162. '#suffix' => '</p>',
  163. );
  164. $header = array(t('Argument'), t('Description'), t('Values'));
  165. $rows = array(
  166. array('q', t('Specifies quantity of this product to add.'), t('A positive integer.')),
  167. array('a&lt;aid&gt;o&lt;oid&gt;', t('Specifies attribute/option for this product.'), t('aid is the integer attribute ID. oid is the integer option ID for radio, checkbox, and select options, or a url-escaped text string for textfield options.')),
  168. array('s', t('Silent. Suppresses add-to-cart message for this product.
  169. (The add-to-cart message may be enabled on the <a href="!url">cart settings page</a>).', array('!url' => url('admin/store/settings/cart'))), t('None.')),
  170. );
  171. $build['args'] = array(
  172. '#prefix' => t('Optional arguments for "p" allow you to control the quantity, set product attributes and options, and suppress the default product action message normally shown when a product is added to a cart. These optional arguments are appended to the "p" action and separated with an underscore. Allowed arguments for "p" are:'),
  173. '#theme' => 'table',
  174. '#header' => $header,
  175. '#rows' => $rows,
  176. );
  177. $build['quantity'] = array(
  178. '#prefix' => '<p>',
  179. '#markup' => t('For example, you may set the product quantity by appending the "q" argument to the "p" action. To add 5 items of product 23 you would use the link:<blockquote><code>/cart/add/p23_q5</code></blockquote>'),
  180. '#suffix' => '</p>',
  181. );
  182. $build['optional'] = array(
  183. '#prefix' => '<p>',
  184. '#markup' => t('Product attributes and options may be set with the <code>a&lt;aid&gt;o&lt;oid&gt;</code> argument. For example, if product 23 has an attribute named "Size" with attribute ID = 12, and if there are three options defined for this attribute ("Small", "Medium", and "Large", with option IDs 4, 5, and 6 respectively), then to add a "Medium" to the cart you would use the link:<blockquote><code>/cart/add/p23_a12o5</code></blockquote>To add two products, one "Medium" and one "Small", you would use two actions:<blockquote><code>/cart/add/p23_a12o5-p23_a12o4</code></blockquote>Or, to just add two "Medium" products:<blockquote><code>/cart/add/p23_q2_a12o5</code></blockquote>'),
  185. '#suffix' => '</p>',
  186. );
  187. $build['example'] = array(
  188. '#prefix' => '<p>',
  189. '#markup' => t('A Cart Link that uses all of the available actions and arguments might look something like this:<blockquote><code>/cart/add/e-p23_q5_a12o5_a19o9_a1oA%20Text%20String_s-ispecialoffer-m77?destination=cart/checkout</code></blockquote>Note that the "e", "p", "i", and "m" actions are separated by dashes, while the optional arguments within the "p" action are separated by underscores. This example will first empty the shopping cart, then add 5 items of product 23 to the cart, track clicks with the ID "specialoffer", display a custom message with the ID "77", then redirect the user to the checkout page. In this case product 23 has three attributes which are set (aid = 12, 19, and 1), one of which is a textfield attribute (aid = 1).'),
  190. '#suffix' => '</p>',
  191. );
  192. $build['help'] = array(
  193. '#prefix' => '<p>',
  194. '#markup' => t('<a href="!url">Visit the settings page</a> to set preferences, define messages, and restrict links that may be used.', array('!url' => url('admin/store/settings/cart-links'))),
  195. '#suffix' => '</p>',
  196. );
  197. return $build;
  198. }