Guest User

Untitled

a guest
Sep 8th, 2018
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.25 KB | None | 0 0
  1. This one of my code work:
  2.  
  3. <?php
  4. class toy{
  5.    
  6.     //Constructor for the class toy.
  7.     function __construct(){
  8.     }
  9.     function getToysInfo($toy_id = ""){
  10.         global $objDB;
  11.        
  12.         if(!empty($toy_id)){
  13.             $sql="select * from toys where toy_id='".$toy_id."'";
  14.         }else{
  15.             $sql="select * from toys";
  16.         }
  17.         $res=$objDB->select($sql);
  18.                            
  19.         return $res;
  20.    
  21.     }
  22.    
  23.     //Following function gets the count of total records from the table for paging.
  24.     function getToysCount($extraPara=""){
  25.         global $objDB;
  26.         $sql = "SELECT count(*) as tot FROM toys WHERE 1=1 ".$extraPara;
  27.         $res = $objDB->select($sql);
  28.         return $res[0]['tot'];
  29.     }
  30.    
  31.     //Following function gets the records from the table for listing.
  32.     function getToysData($extraPara=""){
  33.         global $objDB;
  34.         $sql = "SELECT * FROM toys WHERE 1=1 ".$extraPara;
  35.         $res = $objDB->select($sql);
  36.         return $res;
  37.     }
  38.     //Following function gets the records from the table for for perticular record, required for edit mode auto fill up.
  39.     function getToyById($pkid){
  40.         global $objDB;
  41.         $sql = "SELECT * FROM toys WHERE toy_id= ".$pkid;
  42.         $res = $objDB->select($sql);
  43.         if(count($res) > 0){
  44.             return $res;
  45.         }
  46.     }
  47.    
  48.     //Following function makes entry to the table. Argument passed is POST Array.
  49.     function addToy(){
  50.         global $objDB; 
  51.          $sql = "INSERT INTO `toys` SET
  52.         `toy_name` = '".addslashes($_REQUEST['toy_name'])."',      
  53.         `toy_desc` = '".addslashes($_REQUEST['toy_desc'])."',
  54.         `toy_price` = '".$_REQUEST['toy_price']."',
  55.         `brand_id` = '".$_REQUEST['brand_id']."',
  56.         `age_range` = '".$_REQUEST['age_range']."',
  57.         `createdOn` = '".date('Y-m-d H:i:s')."',
  58.         `status` = '".$_REQUEST['status']."'";     
  59.         $ins = $objDB->insert($sql);
  60.         if($ins){
  61.             $this->addToyCategories($ins);
  62.             $this->addToyImages($ins,'add');
  63.             $this->addToyStocks($ins,$_REQUEST['stock']);
  64.             return $ins;
  65.         }else{
  66.             return false;
  67.         }
  68.     }
  69.    
  70.     function addToyCategories($toy_id){
  71.         global $objDB;
  72.         $catArr = $_REQUEST['cat_id'];
  73.         if(is_array($catArr) && count($catArr) > 0 ){
  74.             foreach($catArr as $x){
  75.                 $sql = "insert into toy_category_map(`toy_id`,`cat_id`) values('".$toy_id."','".$x."')";
  76.                 $objDB->insert($sql);
  77.             }
  78.         }
  79.        
  80.         return true;
  81.     }
  82.    
  83.     function deleteToyCategories($toy_id){
  84.         global $objDB;
  85.         if($ids != ""){
  86.             $sql = "DELETE FROM toy_category_map WHERE toy_id = '".$toy_id."'";
  87.             $res = $objDB->sql_query($sql);
  88.             return true;
  89.         }else{
  90.             return false;
  91.         }
  92.     }
  93.    
  94.     function getToyCategories($toy_id){
  95.         global $objDB;
  96.        
  97.         $sql="select cat.* from toy_category_map as map
  98.             left join toy_categories as cat on cat.cat_id = map.cat_id
  99.             where map.toy_id='".$toy_id."'";
  100.            
  101.         //echo $sql;   
  102.         $res=$objDB->select($sql);
  103.        
  104.         return $res;
  105.        
  106.     }
  107.    
  108.     function addToyImages($toy_id,$opt){
  109.         global $objDB;
  110.        
  111.         $is_default_exist = $this->isDefaultImageExist($toy_id);
  112.        
  113.         $files = $_FILES['toy_images'];
  114.         if(is_array($files) && count($files) > 0){
  115.             foreach($files['name'] as $key => $x){
  116.                 if(!empty($x)){
  117.                     $file_name = pathinfo($x,PATHINFO_FILENAME);
  118.                     $file_ext = pathinfo($x,PATHINFO_EXTENSION);
  119.                    
  120.                     $image_name = sanitize($file_name);
  121.                     $image_name = $image_name.".".$file_ext;
  122.                    
  123.                     $new_file_path = APP_BASE_PATH.TOY_IMAGES_PATH.$image_name;
  124.                    
  125.                     if(move_uploaded_file($files['tmp_name'][$key],$new_file_path)){
  126.  
  127.                         $is_default = ($_REQUEST['is_default'] == ($key+1)?'Yes':'No');
  128.                        
  129.                         if(!$is_default_exist){
  130.                             $sql = "insert into toy_images(`image_name`,`is_default`,`toy_id`) values( "
  131.                             ." '".$image_name."','".$is_default."','".$toy_id."')";
  132.                         }else{
  133.                             $sql = "insert into toy_images(`image_name`,`toy_id`) values( "
  134.                             ." '".$image_name."','".$toy_id."')";
  135.                         }
  136.  
  137.                         $ins  = $objDB->insert($sql);
  138.                         $this->addSliderImage($key,$ins);
  139.                     }
  140.                    
  141.                 }
  142.             }
  143.         }
  144.        
  145.    
  146.         return true;
  147.     }
  148.    
  149.     function addSliderImage($id,$image_id){
  150.         global $objDB;
  151.         // STORING SLIDER IMAGES
  152.         $slider_file = $_FILES['toy_slider_images'];
  153.         if(isset($slider_file['name'][$id])){
  154.             $x = $slider_file['name'][$id];
  155.             $file_name = pathinfo($x,PATHINFO_FILENAME);
  156.             $file_ext = pathinfo($x,PATHINFO_EXTENSION);
  157.            
  158.             $image_name = sanitize($file_name);
  159.             $image_name = "slider-".$image_name.".".$file_ext;
  160.            
  161.             $new_file_path = APP_BASE_PATH.TOY_IMAGES_PATH.$image_name;
  162.            
  163.             if(move_uploaded_file($slider_file['tmp_name'][$id],$new_file_path)){
  164.                 $sql = "update toy_images set `slider_image` = '".$image_name."' where image_id = '".$image_id."'";
  165.  
  166.                 $objDB->edit($sql);
  167.             }
  168.                
  169.         }
  170.     }
  171.    
  172.     function isDefaultImageExist($toy_id){
  173.         global $objDB;
  174.        
  175.         $sql="SELECT * FROM toy_images WHERE is_default = 'Yes' AND toy_id = '".$toy_id."'";
  176.         $res=$objDB->select($sql);
  177.         if(is_array($res) && count($res) > 0){
  178.             return true;
  179.         }
  180.        
  181.         return false;
  182.     }
  183.     //Following function updates record to the table. Argument passed is POST Array and record unique ID.
  184.     function editToy(){
  185.         global $objDB; 
  186.        
  187.          $sql = "UPDATE `toys` SET
  188.         `toy_name` = '".addslashes($_REQUEST['toy_name'])."',      
  189.         `toy_desc` = '".addslashes($_REQUEST['toy_desc'])."',
  190.         `toy_price` = '".$_REQUEST['toy_price']."',
  191.         `brand_id` = '".$_REQUEST['brand_id']."',
  192.         `age_range` = '".$_REQUEST['age_range']."',
  193.         `status` = '".$_REQUEST['status']."'
  194.          where toy_id = '".$_REQUEST['toy_id']."'";    
  195.         $rs = $objDB->edit($sql);
  196.        
  197.         $this->deleteToyCategories($_REQUEST['toy_id']);
  198.         $this->addToyCategories($_REQUEST['toy_id']);
  199.         $this->addToyImages($_REQUEST['toy_id'],'edit');
  200.         $this->addToyStocks($_REQUEST['toy_id'],$_REQUEST['stock']);
  201.         return true;
  202.     }
  203.    
  204.     function getToyImages($toy_id){
  205.         global $objDB;
  206.        
  207.         $sql="select * from toy_images where toy_id='".$toy_id."'";
  208.         $res=$objDB->select($sql);
  209.        
  210.         return $res;
  211.     }
  212.    
  213.     function deleteToyImage($image_id){
  214.         global $objDB;
  215.         $sql = "DELETE FROM toy_images WHERE image_id  = '".$image_id."'";
  216.         $res = $objDB->sql_query($sql);
  217.     }
  218.    
  219.     function checkToyname($username){
  220.         global $objDB;
  221.         $sql = "SELECT count(*) as tot FROM toys WHERE username='".$username."'";
  222.         $res = $objDB->select($sql);
  223.         ($res);
  224.         if($res){
  225.             $cnt=$res[0]['tot'];
  226.             return $cnt;
  227.         }else{
  228.             return false;
  229.         }
  230.     }
  231.     //Following functon check for toys table and finds out if email already exists or not
  232.     function checkToyEmail($email,$id=""){
  233.         global $objDB;
  234.         $idCond ="";
  235.         if($id != ""){
  236.             $idCond = " and toy_id !='".$id."'";
  237.         }
  238.         $sql="select toy_id from toys where email='".$email."' ".$idCond;
  239.  
  240.         $res=$objDB->select($sql);
  241.         if(count($res) > 0){
  242.             return '_DUPLICATE_';
  243.         }else{
  244.             return '_UNIQUE_';
  245.         }
  246.     }
  247.    
  248.     //Following function deletes all the records specyfied by the argument (comma seperated).
  249.     function deleteToys($ids){
  250.         global $objDB;
  251.         if($ids != ""){
  252.             $sql = "DELETE FROM toys WHERE toy_id in ('".$ids."')";
  253.             $res = $objDB->sql_query($sql);
  254.             return true;
  255.         }else{
  256.             return false;
  257.         }
  258.     }
  259.    
  260.     //Following function marks all the records as Active specyfied by the argument (comma seperated).
  261.     function markActive($ids){
  262.         global $objDB;
  263.         if($ids != ""){
  264.             $sql = "UPDATE toys SET status = 'Active' WHERE toy_id in ('".$ids."')";
  265.             $res = $objDB->edit($sql);
  266.             return true;
  267.         }else{
  268.             return false;
  269.         }
  270.     }
  271.    
  272.     //Following function marks all the records as Inactive specyfied by the argument (comma seperated).
  273.     function markInActive($ids){
  274.         global $objDB;
  275.         if($ids != ""){
  276.             $sql = "UPDATE toys SET status = 'InActive' WHERE toy_id in ('".$ids."')";
  277.             $res = $objDB->edit($sql);
  278.             return true;
  279.         }else{
  280.             return false;
  281.         }
  282.     }
  283.    
  284.     //checks wether the old password provided is corrct or not
  285.     function validPass($aid){
  286.         global $objDB;
  287.         $query="SELECT `password`
  288.                 FROM `toys`
  289.                 WHERE `toy_id`='".$aid."'";
  290.         $password= $objDB->select($query);
  291.         if($password[0]["password"]==$_POST["old_password"])
  292.         {
  293.             return true;
  294.         }
  295.         else{
  296.             return false;
  297.         }
  298.     }
  299.    
  300.     //change password
  301.     function changePassword($aid){
  302.         global $objDB;
  303.         $query="UPDATE `toys`
  304.                 SET `password` = '".$_POST["password"]."'
  305.                 WHERE `toy_id` = '".$aid."'";
  306.         $objDB->edit($query);      
  307.         return true;
  308.     }
  309.    
  310.     //function to get toys details
  311.     function getToyDetail($email){
  312.         Global $objDB;
  313.        
  314.         $sql = "SELECT *
  315.                     FROM toys
  316.                     WHERE email='" . $email . "'";
  317.         return $objDB->select($sql);
  318.     }
  319.     //function to send password
  320.     function sendPassword($email){
  321.         Global $objDB, $cfg;
  322.        
  323.         //the given email address for the forget password request is valid now let us send the email with the forget password to the user
  324.         //Let us do some preparation before sending an email
  325.         //Let us assign few variable which are require in the template that is going to be send in email
  326.         $customer=$this->getToyDetail($email);
  327.        
  328.         $objDB->assign('username',$customer[0]['email']);
  329.         $objDB->assign('vPassword',$customer[0]['password']);
  330.         $objDB->assign('AppTitle',$objDB->AppName);
  331.         $objDB->assign('SupportEmail',$objDB->AppSupportEmail);
  332.         $objDB->assign('Cusotmer',stripslashes($customer[0]['first_name'])." ".stripslashes($customer[0]['last_name']));
  333.        
  334.         //Following method sets From Email addres for Email
  335.         $objDB->From=$objDB->getValueFromTable("setting", "varname", "ADMIN_MAIL", "value");
  336.         //Following method sets From Name for Email
  337.         $objDB->FromName=$objDB->getValueFromTable("setting", "varname", "ADMIN_NAME", "value");
  338.        
  339.         //$template_detail = $objDB->getTemplateDetail("FORGET_PASSWORD");
  340.     }
  341.    
  342.     function addToyStocks($toy_id,$stock){
  343.         global $objDB; 
  344.         if($stock == 0) return;
  345.        
  346.          $sql = "INSERT INTO `toy_stocks` SET
  347.         `toy_id` = '".$toy_id."',
  348.         `stock` = '".$stock."',
  349.         `createdOn` = '".date('Y-m-d H:i:s')."'";
  350.  
  351.         $ins = $objDB->insert($sql);
  352.         return true;
  353.     }
  354.  
  355.     function getToyStocks($toy_id){
  356.         global $objDB;
  357.         $tot_stock = 0;
  358.         $sql = "select sum(stock) as tot from toy_stocks where toy_id='".$toy_id."' group by toy_id";
  359.         $res = $objDB->select($sql);
  360.         if(is_array($res) && count($res) > 0){
  361.             $tot_stock = $res[0]['tot'];
  362.             //$tot_ordered_stock = $this->getOrderedToyCounts($toy_id);
  363.            
  364.             return $tot_stock;
  365.         }
  366.        
  367.        
  368.         return 0;
  369.     }
  370.    
  371.     function getOrderedToyCounts($toy_id){
  372.         global $objDB;
  373.        
  374.         $sql = "SELECT sum(qty) AS tot FROM order_detail WHERE toy_id='".$toy_id."' AND `status` in ('delivered') GROUP BY toy_id";
  375.         $res = $objDB->select($sql);
  376.        
  377.         if(is_array($res) && count($res) > 0){
  378.             return $res[0]['tot'];
  379.         }
  380.         return 0;
  381.     }
  382.    
  383.     function getReturnToysReqCount(){
  384.         global $objDB;
  385.         $sql = "SELECT count(*) as tot FROM toy_returns";
  386.         $res = $objDB->select($sql);
  387.        
  388.         return $res[0]['tot']; 
  389.     }
  390.    
  391.     function getReturnToyReqData($return_id = "",$extraParam = ""){
  392.         global $objDB;
  393.         $sql = "SELECT * FROM toy_returns where 1=1";
  394.         if(!empty($return_id)) $sql.= " and return_id='".$return_id."'";
  395.         $sql.=" order by return_id desc";
  396.         $sql.=$extraParam;
  397.  
  398.         $res = $objDB->select($sql);
  399.         return $res;
  400.     }
  401.    
  402.     function updateToyBoxReturnStatus($return_id,$toyboxids){
  403.         global $objDB,$queueObj;
  404.        
  405.         $arr = explode("\n",$toyboxids);
  406.  
  407.         $cnt = 0;
  408.         if(is_array($arr) && count($arr) > 0){
  409.             foreach($arr as $x){
  410.                 $sql = "update toy_queue set `status` = 'returned' where queue_id = '".$x."' and `status` in ('delivered','investigation')";
  411.                 if($objDB->edit($sql)){
  412.                     $cnt++;
  413.                 }
  414.             }
  415.            
  416.             if($cnt > 0){
  417.                 $sql = "update toy_returns set `is_processed` = 'Yes' where return_id = '".$return_id."'";
  418.                 $objDB->edit($sql);
  419.                
  420.                 $boxInfo = $this->getToyBoxInfo($x);
  421.                 if($boxInfo){
  422.                     //SEND RETURN BOX EMAIL
  423.                     $subject = "Toy Box Returned";
  424.                     $tpl = file_get_contents(APP_BASE_PATH."templates/pickup-toy-email.html");
  425.                    
  426.                     $body=str_replace( array('{NAME}','{BOXID}','{TOYNAME}','{RETURNEDON}','{SITE_NAME}','{SITE_URL}'), array($boxInfo['first_name']." ".$boxInfo['last_name'],$x,stripslashes($boxInfo['toy_name']),getFormattedDate(date("Y-m-d")),APP_NAME,APP_URL),$tpl);
  427.                    
  428.                     sendEmail($boxInfo['email'],_APP_EMAIL,APP_EMAIL_FROM,$subject,$body);
  429.                 }
  430.                        
  431.             }
  432.         }else{
  433.             $sql = "update toy_queue set `status` = 'returned' where queue_id = '".$toyboxids."' and `status` in ('delivered','investigation')";
  434.             $objDB->edit($sql);
  435.            
  436.             $sql = "update toy_returns set `is_processed` = 'Yes' where return_id = '".$return_id."'";
  437.             $objDB->edit($sql);
  438.            
  439.             $boxInfo = $this->getToyBoxInfo($x);
  440.             if($boxInfo){
  441.                 //SEND RETURN BOX EMAIL
  442.                 $subject = "Toy Box Returned";
  443.                 $tpl = file_get_contents(APP_BASE_PATH."templates/pickup-toy-email.html");
  444.                
  445.                 $body=str_replace( array('{NAME}','{BOXID}','{TOYNAME}','{RETURNEDON}','{SITE_NAME}','{SITE_URL}'), array($boxInfo['first_name']." ".$boxInfo['last_name'],$toyboxids,stripslashes($boxInfo['toy_name']),getFormattedDate(date("Y-m-d")),APP_NAME,APP_URL),$tpl);
  446.                
  447.                 sendEmail($boxInfo['email'],_APP_EMAIL,APP_EMAIL_FROM,$subject,$body);
  448.             }
  449.         }
  450.     }
  451.    
  452.     function getToyBoxInfo($queue_id){
  453.         global $objDB;
  454.        
  455.         $sql = "SELECT tq.*,u.first_name,u.last_name,t.toy_name,u.email FROM toy_queue AS tq
  456.                 LEFT JOIN toys AS t ON t.toy_id = tq.toy_id
  457.                 LEFT JOIN users AS u ON u.user_id = tq.user_id
  458.                 WHERE queue_id='".$queue_id."'";
  459.                
  460.         $rs = $objDB->select($sql);
  461.        
  462.         if(is_array($rs) && count($rs) > 0){
  463.             return $rs[0];
  464.         }
  465.        
  466.         return false;
  467.     }
  468.  
  469.  
  470. } //end class
  471.    
  472.    
  473.    
  474. ?>
Add Comment
Please, Sign In to add comment