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

/inc/amfphp/Amfphp/Services/ec_admin_promotions.php

https://github.com/EmranAhmed/wp-easycart
PHP | 234 lines | 138 code | 41 blank | 55 comment | 34 complexity | 8b124260acee43ea4a3caaee850fa9ef MD5 | raw file
  1. <?php
  2. /*
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. //All Code and Design is copyrighted by Level Four Development, llc
  6. //
  7. //Level Four Development, LLC provides this code "as is" without warranty of any kind, either express or implied,
  8. //including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
  9. //
  10. //Only licnesed users may use this code and storfront for live purposes. All other use is prohibited and may be
  11. //subject to copyright violation laws. If you have any questions regarding proper use of this code, please
  12. //contact Level Four Development, llc and EasyCart prior to use.
  13. //
  14. //All use of this storefront is subject to our terms of agreement found on Level Four Development, llc's website.
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. */
  18. class ec_admin_promotions
  19. {
  20. function ec_admin_promotions() {
  21. /*load our connection settings
  22. if( file_exists( '../../../../wp-easycart-data/connection/ec_conn.php' ) ) {
  23. require_once('../../../../wp-easycart-data/connection/ec_conn.php');
  24. } else {
  25. require_once('../../../connection/ec_conn.php');
  26. };*/
  27. //set our connection variables
  28. $dbhost = DB_HOST;
  29. $dbname = DB_NAME;
  30. $dbuser = DB_USER;
  31. $dbpass = DB_PASSWORD;
  32. global $wpdb;
  33. define ('WP_PREFIX', $wpdb->prefix);
  34. //make a connection to our database
  35. $this->conn = mysql_connect($dbhost, $dbuser, $dbpass);
  36. mysql_select_db ($dbname);
  37. mysql_query("SET CHARACTER SET utf8", $this->conn);
  38. mysql_query("SET NAMES 'utf8'", $this->conn);
  39. }
  40. //secure all of the services for logged in authenticated users only
  41. public function _getMethodRoles($methodName){
  42. if ($methodName == 'getpromotions') return array('admin');
  43. else if($methodName == 'deletepromotion') return array('admin');
  44. else if($methodName == 'updatepromotion') return array('admin');
  45. else if($methodName == 'addpromotion') return array('admin');
  46. else return null;
  47. }
  48. //HELPER - used to escape out SQL calls
  49. function escape($sql)
  50. {
  51. $args = func_get_args();
  52. foreach($args as $key => $val)
  53. {
  54. $args[$key] = mysql_real_escape_string($val);
  55. }
  56. $args[0] = $sql;
  57. return call_user_func_array('sprintf', $args);
  58. }
  59. //promotion functions
  60. function getpromotions($startrecord, $limit, $orderby, $ordertype, $filter) {
  61. $timezone = mysql_query("SELECT ec_setting.timezone from ec_setting");
  62. $timezoneobject = mysql_fetch_object($timezone);
  63. date_default_timezone_set($timezoneobject->timezone);
  64. $serverdtz = date_default_timezone_get();
  65. $dtz = new DateTimeZone($serverdtz);
  66. $server_time = new DateTime('now', $dtz);
  67. $offset = $dtz->getOffset( $server_time );
  68. //Create SQL Query
  69. $query= mysql_query("SELECT SQL_CALC_FOUND_ROWS ec_promotion.*, ec_promotion.start_date AS original_start_date, ec_promotion.end_date AS original_end_date, UNIX_TIMESTAMP(ec_promotion.start_date) AS start_date, UNIX_TIMESTAMP(ec_promotion.end_date) AS end_date FROM ec_promotion WHERE ec_promotion.promotion_id != '' ".$filter." ORDER BY ". $orderby ." ". $ordertype . " LIMIT ". $startrecord .", ". $limit."");
  70. $totalquery=mysql_query("SELECT FOUND_ROWS()");
  71. $totalrows = mysql_fetch_object($totalquery);
  72. //if results, convert to an array for use in flash
  73. if(mysql_num_rows($query) > 0) {
  74. while ($row=mysql_fetch_object($query)) {
  75. $row->totalrows=$totalrows;
  76. $row->timezone=$serverdtz;
  77. $phpstarttime = strtotime($row->original_start_date);
  78. $phpendtime = strtotime($row->original_end_date);
  79. $row->offset = $offset;
  80. $row->offset_start_date = strval($phpstarttime);
  81. $row->offset_end_date = strval($phpendtime);
  82. $returnArray[] = $row;
  83. }
  84. return($returnArray); //return array results if there are some
  85. } else {
  86. $returnArray[] = "noresults";
  87. return $returnArray; //return noresults if there are no results
  88. }
  89. }
  90. function deletepromotion($promotionid) {
  91. //Create SQL Query
  92. $deletesql = $this->escape("DELETE FROM ec_promotion WHERE ec_promotion.promotion_id = '%s'", $promotionid);
  93. //Run query on database;
  94. mysql_query($deletesql);
  95. //if no errors, return their current Client ID
  96. //if results, convert to an array for use in flash
  97. if(!mysql_error()) {
  98. $returnArray[] ="success";
  99. return($returnArray); //return array results if there are some
  100. } else {
  101. $returnArray[] = "error";
  102. return $returnArray; //return noresults if there are no results
  103. }
  104. }
  105. function updatepromotion($promotionid, $promotioninfo) {
  106. //convert object to array
  107. $promotioninfo = (array)$promotioninfo;
  108. if ($promotioninfo['startdate'] != '') {
  109. $unixstartdate = $promotioninfo['startdate']->timeStamp / 1000;
  110. $startdate = date("'Y-m-d H:i:s'", strtotime("midnight", $unixstartdate));
  111. } else {
  112. $startdate = 'NULL';
  113. }
  114. if ($promotioninfo['enddate'] != '') {
  115. $unixenddate = $promotioninfo['enddate']->timeStamp / 1000;
  116. $enddate = date("'Y-m-d H:i:s'", strtotime("tomorrow", $unixenddate) -1);
  117. } else {
  118. $enddate = 'NULL';
  119. }
  120. //Create SQL Query
  121. $sql = sprintf("Replace into ec_promotion(ec_promotion.promotion_id, ec_promotion.name, ec_promotion.type, ec_promotion.start_date, ec_promotion.end_date, ec_promotion.product_id_1, ec_promotion.manufacturer_id_1, ec_promotion.category_id_1, ec_promotion.price1, ec_promotion.price2, ec_promotion.percentage1)
  122. values('".$promotionid."', '%s', '%s', ".$startdate.", ".$enddate.", '%s', '%s', '%s', '%s', '%s', '%s')",
  123. mysql_real_escape_string($promotioninfo['promotionname']),
  124. mysql_real_escape_string($promotioninfo['promotiontype']),
  125. mysql_real_escape_string($promotioninfo['product1']),
  126. mysql_real_escape_string($promotioninfo['manufacturer1']),
  127. mysql_real_escape_string($promotioninfo['category1']),
  128. mysql_real_escape_string($promotioninfo['price1']),
  129. mysql_real_escape_string($promotioninfo['price2']),
  130. mysql_real_escape_string($promotioninfo['percentage1']));
  131. //Run query on database;
  132. mysql_query($sql);
  133. //if no errors, return their current Client ID
  134. //if results, convert to an array for use in flash
  135. if(!mysql_error()) {
  136. $returnArray[] ="success";
  137. return($returnArray); //return array results if there are some
  138. } else {
  139. return mysql_error();
  140. return $returnArray; //return noresults if there are no results
  141. }
  142. }
  143. function addpromotion($promotioninfo) {
  144. //convert object to array
  145. $promotioninfo = (array)$promotioninfo;
  146. if ($promotioninfo['startdate'] != '') {
  147. $unixstartdate = $promotioninfo['startdate']->timeStamp / 1000;
  148. $startdate = date("'Y-m-d H:i:s'", strtotime("midnight", $unixstartdate));
  149. } else {
  150. $startdate = 'NULL';
  151. }
  152. if ($promotioninfo['enddate'] != '') {
  153. $unixenddate = $promotioninfo['enddate']->timeStamp / 1000;
  154. $enddate = date("'Y-m-d H:i:s'", strtotime("tomorrow", $unixenddate) -1);
  155. } else {
  156. $enddate = 'NULL';
  157. }
  158. /* if ($promotioninfo['startdate'] != '') {
  159. $startdate = date('Y-m-d H:i:s', ($promotioninfo['startdate']->timeStamp / 1000));
  160. } else {
  161. $startdate = 'NULL';
  162. }
  163. if ($promotioninfo['enddate'] != '') {
  164. $enddate = date('Y-m-d H:i:s', ($promotioninfo['enddate']->timeStamp / 1000));
  165. } else {
  166. $enddate = 'NULL';
  167. }*/
  168. //Create SQL Query
  169. $sql = sprintf("Insert into ec_promotion(ec_promotion.promotion_id, ec_promotion.name, ec_promotion.type, ec_promotion.start_date, ec_promotion.end_date, ec_promotion.product_id_1, ec_promotion.manufacturer_id_1, ec_promotion.category_id_1, ec_promotion.price1, ec_promotion.price2, ec_promotion.percentage1)
  170. values(NULL, '%s', '%s', ".$startdate.", ".$enddate.", '%s', '%s', '%s', '%s', '%s', '%s')",
  171. mysql_real_escape_string($promotioninfo['promotionname']),
  172. mysql_real_escape_string($promotioninfo['promotiontype']),
  173. mysql_real_escape_string($promotioninfo['product1']),
  174. mysql_real_escape_string($promotioninfo['manufacturer1']),
  175. mysql_real_escape_string($promotioninfo['category1']),
  176. mysql_real_escape_string($promotioninfo['price1']),
  177. mysql_real_escape_string($promotioninfo['price2']),
  178. mysql_real_escape_string($promotioninfo['percentage1']));
  179. // return $sql;
  180. //Run query on database;
  181. mysql_query($sql);
  182. //if no errors, return their current Client ID
  183. //if results, convert to an array for use in flash
  184. if(!mysql_error()) {
  185. $returnArray[] ="success";
  186. return($returnArray); //return array results if there are some
  187. } else {
  188. return mysql_error();
  189. //return $returnArray; //return noresults if there are no results
  190. }
  191. }
  192. }//close class
  193. ?>