Exemplo n.º 1
0
 public function save()
 {
     parent::save();
     $dbh = Project_DB::get();
     switch ($this->_type) {
         case self::PERSON:
             $data = $this->_person->toArray();
             $table = 'clientsp';
             $table_d = 'clientsc';
             break;
         case self::COMPANY:
             $data = $this->_company->toArray();
             $table = 'clientsc';
             $table_d = 'clientsp';
             break;
         default:
             throw new Kiwi_Exception('Kiwi_Client type unknown');
     }
     foreach ($data as $key => $value) {
         if ($value === null) {
             unset($data[$key]);
         }
     }
     $data['ID'] = $this->_data->ID;
     $columns = implode(', ', array_keys($data));
     $column_pdo_hooks = ':' . implode(', :', array_keys($data));
     $query = "REPLACE {$table} ({$columns}) VALUES ({$column_pdo_hooks})";
     $stmt = $dbh->prepare($query);
     foreach ($data as $key => $value) {
         $stmt->bindValue(":{$key}", $value, is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR);
     }
     $query2 = "DELETE FROM {$table_d} WHERE ID=:ID";
     $stmt2 = $dbh->prepare($query2);
     $stmt2->bindValue(':ID', $this->_data->ID, PDO::PARAM_INT);
     $dbh->exec("LOCK TABLES {$table} WRITE, {$table_d} WRITE");
     $locked = true;
     try {
         $stmt->execute();
         $stmt2->execute();
     } catch (PDOException $e) {
         $dbh->exec('UNLOCK TABLES');
         $locked = false;
         $ei = $e->errorInfo;
         throw Kiwi_Exception("Failed to save client - {$ei[2]} ({$ei[0]})");
     }
     if ($locked) {
         $dbh->exec('UNLOCK TABLES');
     }
 }
Exemplo n.º 2
0
 public function remGroup($group_id)
 {
     if (($user_id = $this->_data->ID) == 0) {
         throw Kiwi_Exception("Attempt to modify unregistered user's rights");
     }
     $dbh = Project_DB::get();
     $query = "DELETE from usersgroups WHERE UID=:user_id AND GID=:group_id LIMIT 1";
     $stmt = $dbh->prepare($query);
     $stmt->bindValue(":user_id", (int) $user_id, PDO::PARAM_INT);
     $stmt->bindValue(":group_id", (int) $group_id, PDO::PARAM_INT);
     try {
         $stmt->execute();
     } catch (PDOException $e) {
         $ei = $e->errorInfo;
         throw Kiwi_Exception("Failed to remove user ({$user_id}) from group ({$group_id}) - {$ei[2]} ({$ei[0]})");
     }
 }