Advertisement
nthhtn

User.php

Jul 4th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.36 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: uygfd
  5.  * Date: 04-Jul-15
  6.  * Time: 15:22
  7.  */
  8. require_once __DIR__."/define.php";
  9. require_once __DIR__."/CPSConnection.php";
  10. class User
  11. {
  12.     private $name;
  13.     private $password;
  14.     private $type;
  15.     private $email;
  16.     private $birthday;
  17.     private $phone;
  18.  
  19.     public function __construct($name=null,$password=null,$type=null,$email=null,$birthday=null,$phone=null)
  20.     {
  21.         $this->name=$name;
  22.         $this->password=$password;
  23.         $this->type=$type;
  24.         $this->email=$email;
  25.         $this->birthday=$birthday;
  26.         $this->phone=$phone;
  27.     }
  28.  
  29.     /**
  30.      * @return mixed
  31.      */
  32.     public function getName()
  33.     {
  34.         return $this->name;
  35.     }
  36.  
  37.     /**
  38.      * @param mixed $name
  39.      */
  40.     public function setName($name)
  41.     {
  42.         $this->name = $name;
  43.     }
  44.  
  45.     /**
  46.      * @return mixed
  47.      */
  48.     public function getPassword()
  49.     {
  50.         return $this->password;
  51.     }
  52.  
  53.     /**
  54.      * @param mixed $password
  55.      */
  56.     public function setPassword($password)
  57.     {
  58.         $this->password = $password;
  59.     }
  60.  
  61.     /**
  62.      * @return mixed
  63.      */
  64.     public function getType()
  65.     {
  66.         return $this->type;
  67.     }
  68.  
  69.     /**
  70.      * @param mixed $type
  71.      */
  72.     public function setType($type)
  73.     {
  74.         $this->type = $type;
  75.     }
  76.  
  77.     /**
  78.      * @return mixed
  79.      */
  80.     public function getEmail()
  81.     {
  82.         return $this->email;
  83.     }
  84.  
  85.     /**
  86.      * @param mixed $email
  87.      */
  88.     public function setEmail($email)
  89.     {
  90.         $this->email = $email;
  91.     }
  92.  
  93.     /**
  94.      * @return mixed
  95.      */
  96.     public function getBirthday()
  97.     {
  98.         return $this->birthday;
  99.     }
  100.  
  101.     /**
  102.      * @param mixed $birthday
  103.      */
  104.     public function setBirthday($birthday)
  105.     {
  106.         $this->birthday = $birthday;
  107.     }
  108.  
  109.     /**
  110.      * @return mixed
  111.      */
  112.     public function getPhone()
  113.     {
  114.         return $this->phone;
  115.     }
  116.  
  117.     /**
  118.      * @param mixed $phone
  119.      */
  120.     public function setPhone($phone)
  121.     {
  122.         $this->phone = $phone;
  123.     }
  124.  
  125.     public function insertUser()
  126.     {
  127.         $CPSConn = new CPSConnection(DB_NAME,USERNAME,PASSWORD,ACCOUNT_ID);
  128.         $cps = $CPSConn->initConnection();
  129.         $insert=array("title"=>"user",
  130.                 "body"=>array(
  131.                     "name"=>$this->getName(),
  132.                     "password"=>$this->getPassword(),
  133.                     "type"=>$this->getType(),
  134.                     "email"=>$this->getEmail(),
  135.                     "birthday"=>$this->getBirthday(),
  136.                     "phone"=>$this->getPhone())
  137.         );
  138.         $insertReq = new CPS_InsertRequest(uniqid(),$insert);
  139.         $cps->sendRequest($insertReq);
  140.         echo "User inserted!";
  141.     }
  142.  
  143.     public function updateUser($id,$name,$password,$type,$email,$birthday,$phone)
  144.     {
  145.         $CPSConn = new CPSConnection(DB_NAME,USERNAME,PASSWORD,ACCOUNT_ID);
  146.         $cps = $CPSConn->initConnection();
  147.         $update=array("title"=>"user",
  148.             "body"=>array(
  149.                 "name"=>$name,
  150.                 "password"=>$password,
  151.                 "type"=>$type,
  152.                 "email"=>$email,
  153.                 "birthday"=>$birthday,
  154.                 "phone"=>$phone)
  155.         );
  156.         $updateReq = new CPS_UpdateRequest($id,$update);
  157.         $cps->sendRequest($updateReq);
  158.         echo "User updated!";
  159.     }
  160.  
  161.     public function selectUser($id)
  162.     {
  163.         $CPSConn = new CPSConnection(DB_NAME,USERNAME,PASSWORD,ACCOUNT_ID);
  164.         $cps = $CPSConn->initConnection();
  165.         $selectReq=new CPS_RetrieveRequest($id);
  166.         $selectRes=$cps->sendRequest($selectReq);
  167.         echo "User selected!<br>";
  168.         foreach ($selectRes->getDocuments() as $id=>$doc)
  169.         {
  170.             if ($doc->title=="user") {
  171.                 echo "Found!<br>";
  172.                 $this->setName($doc->body->name);
  173.                 $this->setPassword($doc->body->password);
  174.                 $this->setType($doc->body->type);
  175.                 $this->setEmail($doc->body->email);
  176.                 $this->setBirthday($doc->body->birthday);
  177.                 $this->setPhone($doc->body->phone);
  178.             }
  179.         }
  180.     }
  181.  
  182.     public function deleteUser($id)
  183.     {
  184.         $CPSConn = new CPSConnection(DB_NAME,USERNAME,PASSWORD,ACCOUNT_ID);
  185.         $cps = $CPSConn->initConnection();
  186.         $deleteReq=new CPS_DeleteRequest($id);
  187.         $cps->sendRequest($deleteReq);
  188.         echo "User deleted!";
  189.     }
  190.  
  191.     public function selectUserByName($name)
  192.     {
  193.         $CPSConn = new CPSConnection(DB_NAME,USERNAME,PASSWORD,ACCOUNT_ID);
  194.         $cps = $CPSConn->initConnection();
  195.         $query=CPS_Term("user","title");
  196.         $searchReq=new CPS_SearchRequest($query);
  197.         $searchRes=$cps->sendRequest($searchReq);
  198.         echo "User selected!<br>";
  199.         if ($searchRes->getHits()>0)
  200.         {
  201.             foreach ($searchRes->getDocuments() as $id=>$doc)
  202.             {
  203.                 if ($doc->body->name==$name)
  204.                 {
  205.                     echo "Found!<br>";
  206.                     $this->setName($doc->body->name);
  207.                     $this->setPassword($doc->body->password);
  208.                     $this->setType($doc->body->type);
  209.                     $this->setEmail($doc->body->email);
  210.                     $this->setBirthday($doc->body->birthday);
  211.                     $this->setPhone($doc->body->phone);
  212.                 }
  213.             }
  214.         }
  215.     }
  216.  
  217.     public function selectUserByNameKeyword($keyword)
  218.     {
  219.         $CPSConn = new CPSConnection(DB_NAME,USERNAME,PASSWORD,ACCOUNT_ID);
  220.         $cps = $CPSConn->initConnection();
  221.         $searchReq=new CPS_SQLSearchRequest('SELECT id WHERE CONTAINS(body.name,"*'.$keyword.'*") AND CONTAINS(title,"user")');
  222.         $searchRes=$cps->sendRequest($searchReq);
  223.         echo "User selected!<br>";
  224.         if ($searchRes->getHits()>0)
  225.         {
  226.             foreach ($searchRes->getDocuments() as $id=>$doc)
  227.             {
  228.                 echo "Found!<br>";
  229.                 $this->setName($doc->body->name);
  230.                 $this->setPassword($doc->body->password);
  231.                 $this->setType($doc->body->type);
  232.                 $this->setEmail($doc->body->email);
  233.                 $this->setBirthday($doc->body->birthday);
  234.                 $this->setPhone($doc->body->phone);
  235.             }
  236.         }
  237.     }
  238. }
  239. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement