Example #1
0
 public static function FindById($entityName, $id)
 {
     self::Connect();
     $entity = RedBean_Facade::load($entityName, $id);
     self::Close();
     return $entity;
 }
Example #2
0
 /**
  * Support for RESTFul GET-requests.
  * Only supports very BASIC REST requests, for more functionality please use
  * the JSON-RPC 2 interface.
  * 
  * @param string $pathToResource RESTFul path to resource
  * 
  * @return string $json a JSON encoded response ready for sending to client
  */
 public function handleRESTGetRequest($pathToResource)
 {
     if (!is_string($pathToResource)) {
         return $this->resp(null, 0, -32099, 'IR');
     }
     $resourceInfo = explode('/', $pathToResource);
     $type = $resourceInfo[0];
     try {
         if (count($resourceInfo) < 2) {
             return $this->resp(RedBean_Facade::findAndExport($type));
         } else {
             $id = (int) $resourceInfo[1];
             return $this->resp(RedBean_Facade::load($type, $id)->export(), $id);
         }
     } catch (Exception $e) {
         return $this->resp(null, 0, -32099);
     }
 }
Example #3
0
 /**
  * Get a client by its ID.
  *
  * @param string $client_id
  *
  * @return IOAuth2Client | Client
  */
 public function getClient($client_id)
 {
     $client_bean = $this->redbean->load($this->tables['client'], $client_id);
     $client = new \ebussola\oauth\client\Client($client_bean);
     return $client;
 }
Example #4
0
 /**
  * Support for RESTFul GET-requests.
  * Only supports very BASIC REST requests, for more functionality please use
  * the JSON-RPC 2 interface.
  *
  * @param string $pathToResource RESTFul path to resource
  *
  * @return string $json a JSON encoded response ready for sending to client
  */
 public function handleRESTGetRequest($pathToResource)
 {
     if (!is_string($pathToResource)) {
         return $this->resp(NULL, 0, self::C_JSONRPC2_SPECIFIED_ERROR, 'IR');
     }
     $resourceInfo = explode('/', $pathToResource);
     $type = $resourceInfo[0];
     try {
         if (count($resourceInfo) < 2) {
             return $this->resp(RedBean_Facade::findAndExport($type));
         } else {
             $id = (int) $resourceInfo[1];
             return $this->resp(RedBean_Facade::load($type, $id)->export(), $id);
         }
     } catch (Exception $exception) {
         return $this->resp(NULL, 0, self::C_JSONRPC2_SPECIFIED_ERROR);
     }
 }
Example #5
0
 public function setDescription($description)
 {
     $mdl = R::load('models', $this->id);
     $mdl->description = $description;
     R::store($mdl);
 }
Example #6
0
 public static function load($post, RedBean_ToolBox $toolbox)
 {
     $writer = $toolbox->getWriter();
     if (isset($post["associations"])) {
         $associations = $post["associations"];
         unset($post["associations"]);
     }
     $can = $pairs = $sorted = array();
     foreach ($post as $key => $rawBean) {
         if (is_array($rawBean) && isset($rawBean["type"])) {
             $type = $rawBean["type"];
             unset($rawBean["type"]);
             $idfield = $writer->getIDField($type);
             if (isset($rawBean[$idfield])) {
                 $id = $rawBean[$idfield];
                 if ($id == 0 && count($rawBean) === 1) {
                     continue;
                 }
                 unset($rawBean[$idfield]);
                 $bean = RedBean_Facade::load($type, $id);
             } else {
                 $bean = RedBean_Facade::dispense($type);
             }
             foreach ($rawBean as $field => $value) {
                 if (!empty($value)) {
                     $bean->{$field} = $value;
                 } elseif (!self::$dontSetEmptyValues) {
                     $bean->{$field} = $value;
                 }
             }
             $can[$key] = $bean;
             if (!isset($sorted[$type])) {
                 $sorted[$type] = array();
             }
             $sorted[$type][] = $bean;
         }
     }
     if (isset($associations) && is_array($associations)) {
         foreach ($associations as $assoc) {
             foreach ($assoc as $info) {
                 if ($info == "0" || $info == "") {
                     continue;
                 }
                 $keys = explode("-", $info);
                 if (isset($can[$keys[0]])) {
                     $bean1 = $can[$keys[0]];
                 } else {
                     $loader = explode(":", $keys[0]);
                     $bean1 = RedBean_Facade::load($loader[0], $loader[1]);
                 }
                 $bean2 = $can[$keys[1]];
                 $pairs[] = array($bean1, $bean2);
             }
         }
     }
     return array("can" => $can, "pairs" => $pairs, "sorted" => $sorted);
 }
Example #7
0
 public function getSavedLogo($shop)
 {
     $shopID = self::existsShop($shop)->getID();
     return R::load('shops', $shopID)->logo;
 }
Example #8
0
 /**
  * Processes a JSON object request.
  *
  * @param array $jsonObject JSON request object
  *
  * @return mixed $result result
  */
 public function handleJSONRequest($jsonString)
 {
     //Decode JSON string
     $jsonArray = json_decode($jsonString, true);
     if (!$jsonArray) {
         return $this->resp(null, null, -32700, "Cannot Parse JSON");
     }
     if (!isset($jsonArray["jsonrpc"])) {
         return $this->resp(null, null, -32600, "No RPC version");
     }
     if ($jsonArray["jsonrpc"] != "2.0") {
         return $this->resp(null, null, -32600, "Incompatible RPC Version");
     }
     //DO we have an ID to identify this request?
     if (!isset($jsonArray["id"])) {
         return $this->resp(null, null, -32600, "No ID");
     }
     //Fetch the request Identification String.
     $id = $jsonArray["id"];
     //Do we have a method?
     if (!isset($jsonArray["method"])) {
         return $this->resp(null, $id, -32600, "No method");
     }
     //Do we have params?
     if (!isset($jsonArray["params"])) {
         $data = array();
     } else {
         $data = $jsonArray["params"];
     }
     //Check method signature
     $method = explode(":", trim($jsonArray["method"]));
     if (count($method) != 2) {
         return $this->resp(null, $id, -32600, "Invalid method signature. Use: BEAN:ACTION");
     }
     //Collect Bean and Action
     $beanType = $method[0];
     $action = $method[1];
     //May not contain anything other than ALPHA NUMERIC chars and _
     if (preg_match("/\\W/", $beanType)) {
         return $this->resp(null, $id, -32600, "Invalid Bean Type String");
     }
     if (preg_match("/\\W/", $action)) {
         return $this->resp(null, $id, -32600, "Invalid Action String");
     }
     try {
         switch ($action) {
             case "store":
                 if (!isset($data[0])) {
                     return $this->resp(null, $id, -32602, "First param needs to be Bean Object");
                 }
                 $data = $data[0];
                 if (!isset($data["id"])) {
                     $bean = RedBean_Facade::dispense($beanType);
                 } else {
                     $bean = RedBean_Facade::load($beanType, $data["id"]);
                 }
                 $bean->import($data);
                 $rid = RedBean_Facade::store($bean);
                 return $this->resp($rid, $id);
                 break;
             case "load":
                 if (!isset($data[0])) {
                     return $this->resp(null, $id, -32602, "First param needs to be Bean ID");
                 }
                 $bean = RedBean_Facade::load($beanType, $data[0]);
                 return $this->resp($bean->export(), $id);
                 break;
             case "trash":
                 if (!isset($data[0])) {
                     return $this->resp(null, $id, -32602, "First param needs to be Bean ID");
                 }
                 $bean = RedBean_Facade::load($beanType, $data[0]);
                 RedBean_Facade::trash($bean);
                 return $this->resp("OK", $id);
                 break;
             default:
                 $modelName = $this->modelHelper->getModelName($beanType);
                 if (!class_exists($modelName)) {
                     return $this->resp(null, $id, -32601, "No such bean in the can!");
                 }
                 $beanModel = new $modelName();
                 if (!method_exists($beanModel, $action)) {
                     return $this->resp(null, $id, -32601, "Method not found in Bean: {$beanType} ");
                 }
                 return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id);
         }
     } catch (Exception $exception) {
         return $this->resp(null, $id, -32099, $exception->getCode() . "-" . $exception->getMessage());
     }
 }
Example #9
0
 /**
  * Loads a bean if ID > 0 else dispenses.
  *
  * @param string  $type type
  * @param integer $id   id
  *
  * @return RedBean_OODBBean $bean bean
  */
 public static function loadOrDispense($type, $id = 0)
 {
     return $id ? RedBean_Facade::load($type, (int) $id) : RedBean_Facade::dispense($type);
 }
Example #10
0
 /**
  * This method will inspect the array provided and load/dispense the
  * desired beans. To dispense a new bean, the array must contain:
  *
  * array( "newuser"=> array("type"=>"user","name"=>"John") )
  *
  * - Creates a new bean of type user, property name is set to "John"
  *
  * To load a bean (for association):
  *
  * array( "theaddress"=> array("type"=>"address","id"=>2) )
  *
  * - Loads a bean of type address with ID 2
  *
  * Now to associate this bean in your form:
  *
  * array("associations"=>array( "0" => array( "newuser-theaddress" ) ))
  *
  * - Associates the beans under keys newuser and theaddress.
  *
  * To modify an existing bean:
  *
  * array("existinguser"=>array("type"=>"user","id"=>2,"name"=>"Peter"))
  *
  * - Changes name of bean of type user with ID 2 to 'Peter'
  *
  * This function returns:
  *
  * array(
  * 	"can" => an array with beans, either loaded or dispensed and populated
  *  "pairs" => an array with pairs of beans to be associated
  *  "sorted" => sorted by type
  * );
  *
  * Note that this function actually does not store or associate anything at all,
  * it just prepares two arrays.
  *
  * @static
  * @param  $post the POST array containing the form data
  * @return array hash table containing 'can' and 'pairs'
  *
  */
 public static function load($post, RedBean_ToolBox $toolbox)
 {
     $writer = $toolbox->getWriter();
     //fetch associations first and remove them from the array.
     if (isset($post["associations"])) {
         $associations = $post["associations"];
         unset($post["associations"]);
     }
     //We store beans here
     $can = $pairs = $sorted = array();
     foreach ($post as $key => $rawBean) {
         if (is_array($rawBean) && isset($rawBean["type"])) {
             //get type and remove it from array
             $type = $rawBean["type"];
             unset($rawBean["type"]);
             //does it have an ID?
             $idfield = $writer->getIDField($type);
             if (isset($rawBean[$idfield])) {
                 //yupz, get the id and remove it from array
                 $id = $rawBean[$idfield];
                 //ID == 0, and no more fields then this is an NULL option for a relation, skip.
                 if ($id == 0 && count($rawBean) === 1) {
                     continue;
                 }
                 unset($rawBean[$idfield]);
                 //now we have the id, load the bean and store it in the can
                 $bean = RedBean_Facade::load($type, $id);
             } else {
                 //no id? then get a new bean...
                 $bean = RedBean_Facade::dispense($type);
             }
             //do we need to modify this bean?
             foreach ($rawBean as $field => $value) {
                 if (!empty($value)) {
                     $bean->{$field} = $value;
                 } elseif (!self::$dontSetEmptyValues) {
                     $bean->{$field} = $value;
                 }
             }
             $can[$key] = $bean;
             if (!isset($sorted[$type])) {
                 $sorted[$type] = array();
             }
             $sorted[$type][] = $bean;
         }
     }
     if (isset($associations) && is_array($associations)) {
         foreach ($associations as $assoc) {
             foreach ($assoc as $info) {
                 if ($info == "0" || $info == "") {
                     continue;
                 }
                 $keys = explode("-", $info);
                 //first check if we can find the key in the can, --only key 1 is able to load
                 if (isset($can[$keys[0]])) {
                     $bean1 = $can[$keys[0]];
                 } else {
                     $loader = explode(":", $keys[0]);
                     $bean1 = RedBean_Facade::load($loader[0], $loader[1]);
                 }
                 $bean2 = $can[$keys[1]];
                 $pairs[] = array($bean1, $bean2);
             }
         }
     }
     return array("can" => $can, "pairs" => $pairs, "sorted" => $sorted);
 }