Example #1
0
 public function setTenantAccess($data)
 {
     // TO DO: 1. remove all exiting tenants
     //        2. cycle through $data and add access to each tenant specified
     $queries = array("call removeTenantUsers(" . $this->id . ");");
     $tenants = $data->{'tenants'};
     foreach ($tenants as $tenant) {
         $query = "call addTenantUserRole(" . Database::queryNumber($this->id) . "," . Database::queryNumber($tenant->{'tenantid'}) . "," . Database::queryString($tenant->{'role'}) . ");";
         array_push($queries, $query);
     }
     Database::executeQueriesInTransaction($queries);
 }
 public function updateEntity($id, $data)
 {
     // this does a very basic update based upon common pattern
     // override to add custom save functionality
     // if simple is true, child entities are ignored
     Utility::debug('dataentity.updateEntity called', 5);
     $queries = array();
     $this->validateData($data);
     //$queries = array();
     $newID = 0;
     $query = "call update" . $this->getName() . "(" . $id;
     $followOnQueries = array();
     $fieldarray = $this->getFields();
     $separator = ",";
     foreach ($fieldarray as $field) {
         if (!property_exists($data, $field[0])) {
             // assume all required fields already validated, so do what?
             $data->{$field[0]} = null;
         }
         switch ($field[1]) {
             case "string":
                 $query .= $separator . Database::queryString($data->{$field[0]});
                 break;
             case "json":
                 $query .= $separator . Database::queryJSON($data->{$field[0]});
                 break;
             case "boolean":
                 $query .= $separator . Database::queryBoolean($data->{$field[0]});
                 break;
             case "number":
             case "decimal":
             case "hidden":
                 $query .= $separator . Database::queryNumber($data->{$field[0]});
                 break;
             case "date":
                 $query .= $separator . Database::queryDate($data->{$field[0]});
                 break;
             case "picklist":
                 $query .= $separator . Database::queryString($data->{$field[0]});
                 break;
             case "linkedentity":
                 $query .= $separator . Database::queryNumber($data->{$field[0]});
                 break;
             case "linkedentities":
             case "childentities":
                 // if childentity field is not specified, then don't mess with children; only if child array is specific (even if any)
                 $handle = is_array($data->{$field[0]});
                 if ($handle) {
                     // a little extra overhead here, but due to sort/sequence keys, etc., don't want to blow away and replace unless we have to
                     // first, determine whether linkedentities list is different
                     $peekquery = "call get" . ucfirst($field[0]) . "By" . $this->getName() . 'Id(' . Database::queryNumber($id) . ',' . Database::queryNumber($this->tenantid) . ',' . Database::queryNumber($this->userid) . ');';
                     $results = Database::executeQuery($peekquery);
                     $currentSet = array();
                     $debug = '';
                     while ($row = $results->fetch_assoc()) {
                         array_push($currentSet, intval($row["id"]));
                         $debug .= $row["id"] . '-';
                     }
                     $newSet = array();
                     $debug .= '|';
                     if (is_array($data->{$field[0]})) {
                         $children = $data->{$field[0]};
                         foreach ($children as $c) {
                             array_push($newSet, $c->id);
                             $debug .= $c->id . '-';
                         }
                     }
                     Log::debug('SETS: ' . $debug, 5);
                     // first, determine whether we need to remove children
                     for ($i = 0; $i < count($currentSet); $i++) {
                         if (!in_array($currentSet[$i], $newSet)) {
                             // one of the old children is not in new set; for now, we'll remove all
                             $procname = $this->getRemoveChildrenProcName($field[0]);
                             array_push($followOnQueries, 'call ' . $procname . '(' . $id . ',' . $this->tenantid . ');');
                             // blank current set so all children get re-added
                             $currentSet = array();
                             break;
                         }
                         if ($currentSet[$i] != $newSet[$i]) {
                             // the sequence of the members has changed; for now, we'll remove them all, too
                             // may want more nuanced handling of this in the future if these sets get big or complex
                             $procname = $this->getRemoveChildrenProcName($field[0]);
                             array_push($followOnQueries, 'call ' . $procname . '(' . $id . ',' . $this->tenantid . ');');
                             // blank current set so all children get re-added
                             $currentSet = array();
                             break;
                         }
                     }
                     // now, determine which children need to be added
                     if (is_array($data->{$field[0]})) {
                         $children = $data->{$field[0]};
                         foreach ($children as $c) {
                             if (!in_array($c->id, $currentSet)) {
                                 // this child isn't present. Will need to add
                                 $procname = $this->getAddChildProcName($field[2]);
                                 array_push($followOnQueries, 'call ' . $procname . '(' . $id . ',' . $c->id . ',' . $this->tenantid . ');');
                             }
                         }
                     }
                 }
                 break;
             case "propertybag":
                 $propertyBag = serialize($data->{$field[0]});
                 $query .= $separator . Database::queryString($propertyBag);
                 break;
             case "custom":
                 $query .= $separator . $this->getCustomValue($field[0], $data->{$field[0]}, 'update');
         }
         $separator = ", ";
     }
     // assume tenantid is always needed and is last parameter (or 2nd to last if user required)
     $query .= $separator . Database::queryNumber($this->tenantid);
     $separator = ", ";
     // add userid if object hasOwner
     if ($this->hasOwner()) {
         $query .= $separator . Database::queryNumber($this->userid);
     }
     $query .= ')';
     Log::debug('Pushing query ' . $query, 1);
     array_push($queries, $query);
     // handle user-defined properties
     if ($this->hasProperties()) {
         // remove all properties for object - if not specified in the data, assume it's not longer a valid property
         array_push($queries, $this->getDeletePropertiesSQL($id));
         // get array of properties allowed for this entity & tenant
         $keys = $this->getPropertyKeys();
         foreach ($keys as $key) {
             // determine whether data contains a value for this key - field will be prepended with PROP
             if (property_exists($data, 'PROP-' . $key[0])) {
                 // only save if not empty - that's the MO for now
                 $val = $data->{'PROP-' . $key[0]};
                 if (strlen($val) > 0) {
                     array_push($queries, $this->getSavePropertySQL($id, $key[0], $data->{'PROP-' . $key[0]}));
                 }
             }
         }
     }
     // add follow-one queries for child entities
     foreach ($followOnQueries as $q) {
         array_push($queries, $q);
     }
     Database::executeQueriesInTransaction($queries);
     return true;
 }