/**
  * Subscribe array of users to the object
  * 
  * If $replace is set to true, all subscriptions for this object will be 
  * dropped and $users will be subscribed to it
  *
  * @param array $users
  * @param ProjectObject $object
  * @param boolean $replace
  * @return boolean
  */
 function subscribeUsers($users, $object, $replace = true)
 {
     db_begin_work();
     $object_id = (int) $object->getId();
     if ($object_id) {
         $subscriptions_table = TABLE_PREFIX . 'subscriptions';
         if ($replace) {
             Subscriptions::deleteByParent($object);
             // cleanup
         }
         // if
         $to_subscribe = array();
         if (is_foreachable($users)) {
             foreach ($users as $user) {
                 if (instance_of($user, 'User')) {
                     $user_id = (int) $user->getId();
                 } else {
                     $user_id = (int) $user;
                 }
                 // if
                 if ($user_id) {
                     if (isset($to_subscribe[$user_id])) {
                         continue;
                         // duplicate user ID!
                     } else {
                         if (!$replace && array_var(db_execute_one("SELECT COUNT(*) AS 'row_count' FROM {$subscriptions_table} WHERE user_id = ? AND parent_id = ?", $user_id, $object_id), 'row_count') > 0) {
                             continue;
                             // Make sure that we do not have this user already subscribed
                         }
                         // if
                         cache_remove("user_subscriptions_{$user_id}");
                         $to_subscribe[$user_id] = "({$user_id}, {$object_id})";
                     }
                     // if
                 }
                 // if
             }
             // foreach
         }
         // if
         // Insert subscriptions
         if (is_foreachable($to_subscribe)) {
             $insert = db_execute("INSERT INTO {$subscriptions_table} VALUES " . implode(', ', $to_subscribe));
             if (!$insert || is_error($insert)) {
                 db_rollback();
                 return $insert;
             }
             // if
         }
         // if
     }
     // if
     db_commit();
     return true;
 }
 /**
  * Delete this object
  * 
  * If $drop_subitems is TRUE subitems will be delete from the database. If it 
  * is false relation will be nullified
  *
  * @param boolean $drop_subitems
  * @return boolean
  * @throws DBQueryError
  */
 function delete($drop_subitems = true)
 {
     db_begin_work();
     $delete = parent::delete();
     if (is_error($delete) || !$delete) {
         db_rollback();
         return $delete;
     }
     // if
     $subitems = $this->getSubitems();
     if (is_foreachable($subitems)) {
         foreach ($subitems as $subitem) {
             if ($drop_subitems) {
                 $delete = $subitem->delete();
                 if (is_error($delete)) {
                     db_rollback();
                     return $delete;
                 }
                 // if
             } else {
                 $subitem->setParent(null, false);
                 $save = $subitem->save();
                 if (is_error($save)) {
                     db_rollback();
                     return $save;
                 }
                 // if
             }
             // if
         }
         // foreach
     }
     // if
     StarredObjects::deleteByObject($this);
     // Attachments
     if ($this->can_have_attachments) {
         Attachments::deleteByObject($this);
     }
     // if
     // Subscriptions
     if ($this->can_have_subscribers) {
         Subscriptions::deleteByParent($this);
     }
     // if
     // Asignments
     if ($this->can_have_assignees) {
         Assignments::deleteByObject($this);
     }
     // if
     // Activity log
     if ($this->log_activities) {
         ActivityLogs::deleteByObject($this);
     }
     // if
     // Reminders
     if ($this->can_send_reminders) {
         Reminders::deleteByObject($this);
     }
     // if
     search_index_remove($this->getId(), 'ProjectObject');
     db_commit();
     return true;
 }