Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.20 KB | None | 0 0
  1. <?php
  2.    
  3. class Device {
  4.      
  5.     private $imei;
  6.     private $sv;
  7.     private $dateAdded;
  8.     private $tac;
  9.     private $brand;
  10.     private $model;
  11.     private $os;
  12.  
  13.     private $dbCon;
  14.  
  15.     function __construct($dbCon){
  16.         $this->dbCon = $dbCon;
  17.     }
  18.  
  19.     function __autoload($classname) {
  20.         $filename = "./". $classname .".php";
  21.         include_once($filename);
  22.     }
  23.  
  24.     function setDeviceFromAstellia($astelliaInfo) {
  25.         if (!empty($astelliaInfo)) {
  26.             $this->setImei($astelliaInfo['imei']);
  27.             $this->setTacBy($this->getImei());
  28.             $this->setBrand($astelliaInfo['Manufacturer']);
  29.             $this->setDateAdded($astelliaInfo['date']);
  30.             $this->setModel($astelliaInfo['Mobile Type']);
  31.             $this->setSv($astelliaInfo['sv']);
  32.             if (!$this->exists()) {
  33.                 $this->persistNewDevice(0);
  34.             } else if ($this->getBrand() != 'UNKNOWN' && $this->getPersistedBrand() == 'UNKNOWN') {
  35.                 $this->updatePersistedDeviceDetails();
  36.             }
  37.             return true;
  38.         } else {
  39.             return false;
  40.         }
  41.     }
  42.  
  43.     function setDeviceBy($tac) {
  44.         $deviceInfo = $this->dbCon->select("deviceInfo", [
  45.             "tac",
  46.             "brand",
  47.             "model",
  48.             "date_added",
  49.             "os"
  50.         ], [
  51.             "tac" => $tac
  52.         ]);
  53.         $deviceInfo = $deviceInfo[0];
  54.         $this->setBrand($deviceInfo['brand']);
  55.         $this->setTac($tac);
  56.         $this->setModel($deviceInfo['model']);
  57.         $this->setDateAdded($deviceInfo['date_added']);
  58.         $this->setOs($deviceInfo['os']);
  59.     }
  60.  
  61.     function setDeviceFromImeiSv($imei, $sv) {
  62.         if (!empty($imei)) {
  63.             $this->setImei($imei);
  64.             $this->setTacBy($this->getImei());
  65.             $this->setSv($sv);
  66.             $deviceInfo = $this->getDeviceByTac($this->getTac());
  67.             if (!empty($deviceInfo)) {
  68.                 $this->setBrand($deviceInfo['brand']);
  69.                 $this->setModel($deviceInfo['model']);
  70.                 $this->setDateAdded($deviceInfo['date_added']);
  71.             } else {
  72.                 $this->setBrand("UNKNOWN");
  73.                 $this->setModel("[" . $this->getTac() . "]");
  74.                 $this->setDateAdded(date('Y-m-d H:i:s', time()));
  75.                 $this->persistNewDevice(1);
  76.             }
  77.             return true;
  78.         } else {
  79.             return false;
  80.         }
  81.     }
  82.  
  83.     function equals($device) {
  84.         //error_log($device->getImei() . " == " . $this->getImei());
  85.         if ( $device->getImei() != null && $this->getImei() != null ) {
  86.             if ($device->getImei() == $this->getImei()) {
  87.                 return true;
  88.             } else {
  89.                 return false;//array("newDevice:" => $this->getImei(), "device:" => $device->getImei());
  90.             }
  91.         } else {
  92.             return false;
  93.         }
  94.  
  95.     }
  96.  
  97.     function persistNewDevice($source) {
  98.         $insert = $this->dbCon->insert("deviceInfo", [
  99.             "tac"          => $this->getTac(),
  100.             "brand"        => $this->getBrand(),
  101.             "model"        => $this->getModel(),
  102.             "date_added"   => $this->getDateAdded(),
  103.             "source" => $source
  104.         ]);
  105.         return $insert;
  106.     }
  107.  
  108.     function exists() {
  109.         $result = $this->dbCon->select("deviceInfo", [
  110.             "tac",
  111.             "brand",
  112.             "model"
  113.         ], [
  114.             "tac" => $this->getTac()
  115.         ]);
  116.         //$result = $result[0];
  117.         //error_log("result: => " . var_dump($result) . "THIS => " . print_r($this->toString()));
  118.         if ($result == null) {
  119.             return false;
  120.         } else {
  121.             return true;
  122.         }
  123.     }
  124.  
  125.     function getOsList() {
  126.         $deviceInfo = $this->dbCon->select("osVersion", "os",
  127.             ["GROUP" => "os"]);
  128.         //error_log($this->dbCon->log());
  129.         return $deviceInfo;
  130.     }
  131.  
  132.     function getOsVersionList($os) {
  133.         $deviceInfo = $this->dbCon->select("osVersion", "osVersion", ["os" => $os]);
  134.         return $deviceInfo;
  135.     }
  136.  
  137.     function getPersistedBrand() {
  138.         $result = $this->dbCon->select("deviceInfo", [
  139.             "brand"
  140.         ], [
  141.             "tac" => $this->getTac()
  142.         ]);
  143.         return $result[0]['brand'];
  144.     }
  145.  
  146.     function getAllBrands() {
  147.         $brands = $this->dbCon->select("deviceInfo", "brand", [
  148.             "GROUP" => "brand"
  149.         ]);
  150.         return $brands;
  151.     }
  152.  
  153.     function updatePersistedDeviceDetails() {
  154.         $result = $this->dbCon->update("deviceInfo",[
  155.             "brand"      => $this->getBrand(),
  156.             "model"      =>  $this->getModel(),
  157.             "date_added" => date('Y-m-d H:i:s', time())
  158.         ], [
  159.             "tac" => $this->getTac()
  160.         ]);
  161.         error_log("Device Updated: " . $this->getBrand() . " | " . $this->getModel());
  162.     }
  163.  
  164.     function getDeviceByTac($tac) {
  165.         $deviceInfo = $this->dbCon->get("deviceInfo", [
  166.             "tac",
  167.             "brand",
  168.             "model",
  169.             "date_added",
  170.             "os"
  171.         ], [
  172.             "tac" => $tac
  173.         ]);
  174.         if (isset($deviceInfo)) {
  175.             return $deviceInfo;
  176.         } else {
  177.             return "";
  178.         }
  179.     }
  180.  
  181.     function getDeviceByModel($model, $tac) {
  182.         $deviceInfo = $this->dbCon->select("deviceInfo", [
  183.             "tac",
  184.             "brand",
  185.             "model",
  186.             "date_added",
  187.             "os"
  188.         ], [
  189.             "AND" => [
  190.                 "model" => $model,
  191.                 "tac[!]" => $tac
  192.             ]
  193.         ]);
  194.         if (isset($deviceInfo[0])) {
  195.             return $deviceInfo;
  196.         } else {
  197.             return [];
  198.         }
  199.     }
  200.  
  201.     function setTac($tac) {
  202.         $this->tac = $tac;
  203.     }
  204.    
  205.     function setTacBy($imei) {
  206.         $this->setTac(substr($imei, 0, 8));
  207.     }
  208.    
  209.     function setImei($imei) {
  210.         if (strlen($imei) > 14) {
  211.             $imei = substr($imei, 0, 14);
  212.         } else if (strlen($imei) < 14) {
  213.             $imei = 0 . $imei;
  214.         }
  215.         $this->imei = $imei;
  216.     }
  217.    
  218.     function setDateAdded($dateAdded) {
  219.         $this->dateAdded = $dateAdded;
  220.     }
  221.    
  222.     function setBrand($brand) {
  223.         $this->brand = $brand;
  224.     }
  225.    
  226.     function setModel($model) {
  227.         $this->model = $model;
  228.     }
  229.    
  230.     function setSv($sv) {
  231.         $this->sv = $sv;
  232.     }
  233.  
  234.     function getSv() {
  235.         return $this->sv;
  236.     }
  237.  
  238.     function getTac() {
  239.         return $this->tac;
  240.     }
  241.    
  242.     function getImei() {
  243.         return $this->imei;
  244.     }  
  245.    
  246.     function getDateAdded() {
  247.         return $this->dateAdded;
  248.     }  
  249.    
  250.     function getBrand() {
  251.         return $this->brand;
  252.     }  
  253.    
  254.     function getModel() {
  255.         return $this->model;
  256.     }
  257.  
  258.     /**
  259.      * @return mixed
  260.      */
  261.     public function getOs()
  262.     {
  263.         return $this->os;
  264.     }
  265.  
  266.     /**
  267.      * @param mixed $os
  268.      */
  269.     public function setOs($os)
  270.     {
  271.         $this->os = $os;
  272.     }
  273.  
  274.     public function toString() {
  275.         $return = get_object_vars($this);
  276.         unset($return['dbCon']);
  277.         //unset($return['device']['dbCon']);
  278.         return $return;
  279.     }
  280.  
  281.     function printMe($object, $text) {
  282.         echo "</br><b>" . $text . "</b> :: ";
  283.         print_r($object->toString());
  284.     }
  285. }
  286.  
  287. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement