Guest User

Untitled

a guest
Sep 14th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lexo
  5. * Date: 9/13/18
  6. * Time: 11:49 AM
  7. */
  8. require_once("ripcord.php");
  9.  
  10. /**
  11. * Class odoo
  12. */
  13. class odoo {
  14. public $uid = Null;
  15. private $models = Null;
  16. private $url=False;
  17. private $db=False;
  18. private $username=False;
  19. private $password=False;
  20.  
  21. /**
  22. * odoo constructor.
  23. */
  24. function __construct($url,$db, $username, $password)
  25. {
  26. $this->url = $url;
  27. $this->db = $db;
  28. $this->username = $username;
  29. $this->password = $password;
  30.  
  31. if(!empty($this->url)){
  32. $common = ripcord::client("$this->url/xmlrpc/2/common");
  33. $this->uid = $common->authenticate($this->db, $this->username, $this->password, array());
  34. $this->models = ripcord::client("$this->url/xmlrpc/2/object");
  35. }
  36. }
  37.  
  38. /**
  39. * @param $model
  40. * @param $data
  41. * @param bool $multiple
  42. * @return bool|mixed
  43. */
  44. public function create($model,$data, $multiple=False){
  45. $create_data = $multiple? $data:[$data];
  46. $created_id = $this->execute(
  47. $model,
  48. 'create', // Function name
  49. $create_data
  50. );
  51. if(is_int($created_id)){
  52. return $created_id;
  53. }
  54. else{
  55. return False;
  56. }
  57. }
  58.  
  59. /**
  60. * @param $model
  61. * @param $conditions
  62. * @param $data
  63. * @return bool|mixed
  64. */
  65. public function update($model,$conditions,$data){
  66. $id = $this->findOne($model,$conditions);
  67. if($id){
  68. $updated = $this->execute( $model, 'write',
  69. array(array($id), $data));
  70. // return $updated;
  71. return $id;
  72. } else{
  73. return False;
  74. }
  75. }
  76.  
  77. /**
  78. * @param $model
  79. * @param $conditions
  80. * @return mixed
  81. */
  82. public function find($model,$conditions){
  83. $result = $this->execute(
  84. $model, 'search',
  85. [$conditions]);
  86. return $result;
  87. }
  88.  
  89. /**
  90. * @param $model
  91. * @param $conditions
  92. * @return mixed
  93. */
  94. public function findOne($model,$conditions){
  95. $result = $this->execute(
  96. $model, 'search',
  97. [$conditions],
  98. ['limit'=> 1]);
  99. return end($result);
  100. }
  101.  
  102. /**
  103. * @param $model
  104. * @param $conditions
  105. * @return mixed
  106. */
  107. public function read($model, $conditions){
  108. $ids = $this->find($model,$conditions);
  109. $records = $this->execute(
  110. $model, 'search_read', [$conditions]);
  111. return $records;
  112. }
  113.  
  114. /**
  115. * @param $model
  116. * @param $conditions
  117. * @return mixed
  118. */
  119. public function readOne($model, $conditions){
  120. $id = $this->findOne($model,$conditions);
  121. $records = $this->execute($model, 'search_read', [$conditions]);
  122. return end($records);
  123. }
  124.  
  125. /**
  126. * @param $model
  127. * @param $action
  128. * @param array $conditions
  129. * @param array $options
  130. * @return mixed
  131. */
  132. private function execute($model, $action, $conditions=[],$options=[]){
  133. $res = $this->models->execute_kw($this->db, $this->uid, $this->password,
  134. $model, $action, $conditions,$options);
  135. return $res;
  136. }
  137. }
Add Comment
Please, Sign In to add comment