示例#1
0
 function execute($param, $post)
 {
     if (count($param) > 0 && $param[0] == "act") {
         $obj = json_decode($post["json"]);
         $group = GetSingleByCondition(DRINKERCYCLE_TABLE, array("Name" => $obj->Name));
         if ($group instanceof DrinkerCycle) {
             if ($obj->Action == "exists") {
                 $groupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id));
                 return ReturnBoolean($groupRela != null);
             } else {
                 if ($obj->Action == "add") {
                     $drinker = GetSingleByCondition(DRINKER_TABLE, array("Guid" => $obj->Guid));
                     if ($drinker instanceof Drinker) {
                         $presGroupRelation = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id, "DrinkerId" => $drinker->Id));
                         if ($presGroupRelation == null) {
                             $groupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id));
                             $newRela = new DrinkerCyclesDrinkersRelation();
                             $newRela->DrinkerId = $drinker->Id;
                             $newRela->DrinkerCycleId = $group->Id;
                             $newRela->IsAuthenticated = $groupRela == null;
                             return ReturnBoolean(Insert(DRINKERCYCLESDRINKERSRELATION_TABLE, $newRela));
                         }
                         return ReturnBoolean(true);
                     } else {
                         return ReturnNotFound($obj->Guid, "Drinker");
                     }
                 } else {
                     if ($obj->Action == "remove") {
                         $drinker = GetSingleByCondition(DRINKER_TABLE, array("Guid" => $obj->Guid));
                         if ($drinker instanceof Drinker) {
                             $groupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id, "DrinkerId" => $drinker->Id));
                             if ($groupRela instanceof DrinkerCyclesDrinkersRelation) {
                                 return ReturnBoolean(DeleteById(DRINKERCYCLESDRINKERSRELATION_TABLE, $groupRela->Id));
                             } else {
                                 return RelationNotFound($group->Id, $drinker->Id, DRINKERCYCLESDRINKERSRELATION_TABLE);
                             }
                         } else {
                             return ReturnNotFound($obj->Guid, DRINKER_TABLE);
                         }
                     } else {
                         if ($obj->Action == "authenticate" || $obj->Action == "deauthenticate") {
                             $newVal = true;
                             if ($obj->Action == "deauthenticate") {
                                 $newVal = false;
                             }
                             $drinker = GetSingleByCondition(DRINKER_TABLE, array("Guid" => $obj->Guid));
                             if ($drinker instanceof Drinker) {
                                 $groupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id, "DrinkerId" => $drinker->Id));
                                 if ($groupRela instanceof DrinkerCyclesDrinkersRelation) {
                                     //can change others status
                                     if ($groupRela->IsAuthenticated) {
                                         $otherDrinker = GetSingleByCondition(DRINKER_TABLE, array("Guid" => $obj->AuthGuid));
                                         if ($otherDrinker instanceof Drinker) {
                                             $otherGroupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id, "DrinkerId" => $otherDrinker->Id));
                                             if ($otherGroupRela instanceof DrinkerCyclesDrinkersRelation) {
                                                 $otherGroupRela->IsAuthenticated = $newVal;
                                                 return ReturnBoolean(Update(DRINKERCYCLESDRINKERSRELATION_TABLE, $otherGroupRela));
                                             } else {
                                                 return RelationNotFound($group->Id, $otherDrinker->Id, DRINKERCYCLESDRINKERSRELATION_TABLE);
                                             }
                                         } else {
                                             return ReturnNotFound($obj->AuthGuid, "Drinker");
                                         }
                                     } else {
                                         //not authenticated
                                         return ReturnBoolean(false);
                                     }
                                 } else {
                                     return RelationNotFound($group->Id, $drinker->Id, DRINKERCYCLESDRINKERSRELATION_TABLE);
                                 }
                             } else {
                                 return ReturnNotFound($obj->Guid, "Drinker");
                             }
                         } else {
                             if ($obj->Action == "removeforeign") {
                                 $drinker = GetSingleByCondition(DRINKER_TABLE, array("Guid" => $obj->Guid));
                                 if ($drinker instanceof Drinker) {
                                     $groupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id, "DrinkerId" => $drinker->Id));
                                     if ($groupRela instanceof DrinkerCyclesDrinkersRelation) {
                                         //can change others status
                                         if ($groupRela->IsAuthenticated) {
                                             $otherDrinker = GetSingleByCondition(DRINKER_TABLE, array("Guid" => $obj->AuthGuid));
                                             if ($otherDrinker instanceof Drinker) {
                                                 $otherGroupRela = GetSingleByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $group->Id, "DrinkerId" => $otherDrinker->Id));
                                                 if ($otherGroupRela instanceof DrinkerCyclesDrinkersRelation) {
                                                     return ReturnBoolean(Delete(DRINKERCYCLESDRINKERSRELATION_TABLE, $otherGroupRela));
                                                 } else {
                                                     return RelationNotFound($group->Id, $otherDrinker->Id, DRINKERCYCLESDRINKERSRELATION_TABLE);
                                                 }
                                             } else {
                                                 return ReturnNotFound($obj->AuthGuid, "Drinker");
                                             }
                                         } else {
                                             //not authenticated
                                             return ReturnBoolean(false);
                                         }
                                     } else {
                                         return RelationNotFound($group->Id, $drinker->Id, DRINKERCYCLESDRINKERSRELATION_TABLE);
                                     }
                                 } else {
                                     return ReturnNotFound($obj->Guid, "Drinker");
                                 }
                             } else {
                                 return ReturnError(LINK_INVALID);
                             }
                         }
                     }
                 }
             }
         }
         if ($obj->Action == "exists") {
             return ReturnBoolean(false);
         } else {
             if ($obj->Action == "add") {
                 $newGroup = new DrinkerCycle();
                 $newGroup->Name = $obj->Name;
                 $newGroup->Guid = GenerateGuid();
                 if (Insert(DRINKERCYCLE_TABLE, $newGroup)) {
                     return $this->execute($param, $post);
                 } else {
                     return ReturnCrudError($newGroup, "add");
                 }
             } else {
                 return ReturnNotFound($obj->Guid, DRINKERCYCLE_TABLE);
             }
         }
     } else {
         if (ValidateGuid($param[0])) {
             //construct model puh
             $drinker = GetByGuid("Drinker", $param[0]);
             if ($drinker != null && $drinker instanceof Drinker) {
                 $relations = GetAllByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerId" => $drinker->Id));
                 $cyclesEnt = array();
                 $cycles = array();
                 $authCycles = array();
                 foreach ($relations as $relation) {
                     if ($relation instanceof DrinkerCyclesDrinkersRelation) {
                         $cycle = GetById(DRINKERCYCLE_TABLE, $relation->DrinkerCycleId);
                         if ($cycle instanceof DrinkerCycle) {
                             if ($relation->IsAuthenticated) {
                                 $cycl = new DrinkerCycleEntity($cycle);
                                 $cycl->IsAuthenticated = true;
                                 $authCycles[] = $cycle;
                                 $cyclesEnt[] = $cycl;
                             } else {
                                 $cycles[] = $cycle;
                                 $cycl = new DrinkerCycleEntity($cycle);
                                 $cycl->IsAuthenticated = false;
                                 $cyclesEnt[] = $cycl;
                             }
                         }
                     }
                 }
                 $drinkers = array();
                 foreach ($authCycles as $cycle) {
                     $userRelations = GetAllByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerCycleId" => $cycle->Id));
                     foreach ($userRelations as $userRelation) {
                         if ($userRelation instanceof DrinkerCyclesDrinkersRelation) {
                             //exclude self
                             if ($drinker->Id != $userRelation->DrinkerId) {
                                 if (!isset($drinkers[$userRelation->DrinkerId])) {
                                     $user = GetById(DRINKER_TABLE, $userRelation->DrinkerId);
                                     if ($user instanceof Drinker) {
                                         $drinkers[$userRelation->DrinkerId] = new DrinkerEntity($user);
                                     }
                                 }
                                 if ($drinkers[$userRelation->DrinkerId] instanceof DrinkerEntity) {
                                     if ($userRelation->IsAuthenticated) {
                                         $drinkers[$userRelation->DrinkerId]->AuthDrinkerCycles[] = $cycle->Guid;
                                     } else {
                                         $drinkers[$userRelation->DrinkerId]->NonAuthDrinkerCycles[] = $cycle->Guid;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $coll = new DrinkerCycleResponse();
                 $coll->DrinkerCycles = $cyclesEnt;
                 foreach ($drinkers as $drinker) {
                     $coll->Drinkers[] = $drinker;
                 }
                 return json_encode($coll);
             } else {
                 return ReturnNotFound($param[0], "Drinker");
             }
         } else {
             return ReturnError(LINK_INVALID);
         }
     }
 }
 function execute($param, $post)
 {
     if (count($param) > 0) {
         if ($param[0] == "act") {
             $obj = json_decode($post["json"]);
             $user = GetByGuid("Drinker", $obj->Guid);
             if ($obj->Action == "exists") {
                 return ReturnBoolean($user != null);
             } else {
                 if ($obj->Action == "update") {
                     if ($user instanceof Drinker) {
                         $user->Name = $obj->UserInformations->Name;
                         $user->Color = $obj->UserInformations->Color;
                         return ReturnBoolean(Update(DRINKER_TABLE, $user));
                     } else {
                         $user = new Drinker();
                         $user->Name = $obj->UserInformations->Name;
                         $user->Color = $obj->UserInformations->Color;
                         $user->Guid = $obj->Guid;
                         return ReturnBoolean(Insert(DRINKER_TABLE, $user));
                     }
                 } else {
                     if ($obj->Action == "remove") {
                         if ($user instanceof Drinker) {
                             //remove all DrinkerCycleRelations
                             $relations = GetAllByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerId" => $user->Id));
                             if (DeleteAll($relations)) {
                                 return ReturnBoolean(Delete(DRINKER_TABLE, $user));
                             } else {
                                 return ReturnCrudError($relations, "DeleteAll");
                             }
                         } else {
                             return ReturnNotFound($obj->Guid, "Drinker");
                         }
                     }
                 }
             }
         } else {
             if (ValidateGuid($param[0])) {
                 $user = GetByGuid("Drinker", $param[0]);
                 if ($user instanceof Drinker) {
                     $drinker = new DrinkerEntity($user);
                     $relations = GetAllByCondition(DRINKERCYCLESDRINKERSRELATION_TABLE, array("DrinkerId" => $drinker->Id));
                     foreach ($relations as $relation) {
                         if ($relation instanceof DrinkerCyclesDrinkersRelation) {
                             $cycle = GetById(DRINKER_TABLE, $relation->DrinkerCycleId);
                             if ($cycle instanceof DrinkerCycle) {
                                 if ($relation->IsAuthenticated) {
                                     $drinker->AuthDrinkerCycles[] = $cycle->Guid;
                                 } else {
                                     $drinker->NonAuthDrinkerCycles[] = $cycle->Guid;
                                 }
                             }
                         }
                     }
                     $resp = new DrinkerResponse();
                     $resp->Drinker = $drinker;
                     return json_encode($resp);
                 } else {
                     return ReturnNotFound($param[0], "Drinker");
                 }
             }
         }
     }
     return ReturnError(LINK_INVALID);
 }
示例#3
0
 function execute($param, $post)
 {
     if (count($param) > 0) {
         if ($param[0] == "act") {
             $obj = json_decode($post["json"]);
             $drinker = GetByGuid("Drinker", $obj->Guid);
             if ($drinker instanceof Drinker) {
                 if ($obj->Action == "remove") {
                     $existingBeers = array();
                     foreach ($obj->Beers as $beer) {
                         $existingBeer = GetSingleByCondition("Beers", array("Guid" => $beer->Guid));
                         if ($existingBeer != null) {
                             $existingBeers[] = $existingBeer;
                         }
                     }
                     return ReturnBoolean(DeleteAll($existingBeers) && $this->refreshDrinkerProperties($drinker));
                 } else {
                     if ($obj->Action == "add") {
                         $newBeers = array();
                         foreach ($obj->Beers as $beer) {
                             $existingBeer = GetSingleByCondition("Beers", array("Guid" => $beer->Guid));
                             if ($existingBeer == null) {
                                 $newbeer = new Beer();
                                 $newbeer->Guid = $beer->Guid;
                                 $newbeer->DrinkTime = ConvertToDatabaseDateTime($beer->DrinkTime);
                                 $newbeer->DrinkerId = $drinker->Id;
                                 $newBeers[] = $newbeer;
                             }
                         }
                         return ReturnBoolean(InsertAll($newBeers) && $this->refreshDrinkerProperties($drinker));
                     } else {
                         if ($obj->Action == "sync") {
                             $beercount = $this->countExistingBeers($drinker);
                             if ($beercount !== $obj->ExpectedCount) {
                                 return ReturnBoolean(false);
                             }
                             $beers = GetAllByCondition("Beers", array("DrinkerId" => $drinker->Id), "DrinkTime DESC", " LIMIT " . count($obj->Beers));
                             for ($i = 0; $i < count($beers); $i++) {
                                 if ($beers[$i]->Guid != $obj->Beers[$i]->Guid) {
                                     return ReturnBoolean(false);
                                 }
                             }
                             return ReturnBoolean(true);
                         } else {
                             return ReturnError(LINK_INVALID);
                         }
                     }
                 }
             }
             return ReturnNotFound($param[0], "Drinker");
         } else {
             if (ValidateGuid($param[0])) {
                 $drinker = GetByGuid("Drinker", $param[0]);
                 if ($drinker != null && $drinker instanceof Drinker) {
                     /*
                     $limit = 0;
                     if (count($param) > 1 && is_numeric($param[1])) {
                         $beerCount = $this->countExistingBeers($drinker);
                         if ($beerCount > $param[1]) {
                             $limit = $beerCount - $param[0];
                         }
                     }
                     $limitstring = " LIMIT " . $limit;
                     if ($limit == 0)
                         $limitstring = "";
                     */
                     $beers = GetAllByCondition("Beers", array("DrinkerId" => $drinker->Id), " DrinkTime DESC ");
                     $resp = new BeerResponse();
                     foreach ($beers as $beer) {
                         $resp->Beers[] = new BeerEntity($beer);
                     }
                     return json_encode($resp);
                 } else {
                     return ReturnNotFound($param[0], "Drinker");
                 }
             }
         }
     }
     return ReturnError(LINK_INVALID);
 }