/**
  * Clean up system by object ID-s
  * 
  * This function cleans up project objects recursively. It is also infinite 
  * loop safe because it will filter out ID-s that are already removed
  *
  * @param array $ids
  * @return null
  */
 function cleanUpByIds($ids)
 {
     static $cleaned_ids = array();
     // Remove objects that are already cleaned
     if (is_foreachable($ids)) {
         foreach ($ids as $k => $id) {
             if (isset($cleaned_ids[$id]) && $cleaned_ids[$id]) {
                 unset($ids[$k]);
             } else {
                 $cleaned_ids[$id] = false;
             }
             // if
         }
         // foreach
     }
     // if
     if (is_foreachable($ids)) {
         db_begin_work();
         Attachments::deleteByProjectObjectIds($ids);
         Subscriptions::deleteByObjectIds($ids);
         Assignments::deleteByObjectIds($ids);
         ActivityLogs::deleteByObjectIds($ids);
         StarredObjects::deleteByObjectIds($ids);
         Reminders::deleteByObjectIds($ids);
         search_index_remove($ids, 'ProjectObject');
         $rows = db_execute_all('SELECT DISTINCT id FROM ' . TABLE_PREFIX . 'project_objects WHERE parent_id IN (?)', $ids);
         if (is_foreachable($rows)) {
             $subobject_ids = array();
             foreach ($rows as $row) {
                 $subobject_ids[] = (int) $row['id'];
             }
             // foreach
             ProjectObjects::cleanUpByIds($subobject_ids);
         }
         // if
         ProjectObjects::delete(array('id IN (?)', $ids));
         foreach ($ids as $id) {
             $cleaned_ids[$id] = true;
         }
         // if
         db_commit();
     }
     // if
     return true;
 }
 /**
  * Delete project and all realted data
  *
  * @param void
  * @return null
  */
 function delete()
 {
     db_begin_work();
     $delete = parent::delete();
     if ($delete && !is_error($delete)) {
         ProjectObjects::deleteByProject($this);
         ProjectUsers::deleteByProject($this);
         PinnedProjects::deleteByProject($this);
         search_index_remove($this->getId(), 'Project');
         clean_project_permissions_cache($this);
         event_trigger('on_project_deleted', array($this));
         db_commit();
     } else {
         db_rollback();
     }
     // if
     return $delete;
 }
 /**
  * Delete from database
  *
  * @param void
  * @return boolean
  */
 function delete()
 {
     db_begin_work();
     $delete = parent::delete();
     if ($delete && !is_error($delete)) {
         unlink($this->getAvatarPath());
         unlink($this->getAvatarPath(true));
         ProjectUsers::deleteByUser($this);
         Assignments::deleteByUser($this);
         Subscriptions::deleteByUser($this);
         StarredObjects::deleteByUser($this);
         PinnedProjects::deleteByUser($this);
         UserConfigOptions::deleteByUser($this);
         Reminders::deleteByUser($this);
         search_index_remove($this->getId(), 'User');
         $cleanup = array();
         event_trigger('on_user_cleanup', array(&$cleanup));
         if (is_foreachable($cleanup)) {
             foreach ($cleanup as $table_name => $fields) {
                 foreach ($fields as $field) {
                     $condition = '';
                     if (is_array($field)) {
                         $id_field = array_var($field, 'id');
                         $name_field = array_var($field, 'name');
                         $email_field = array_var($field, 'email');
                         $condition = array_var($field, 'condition');
                     } else {
                         $id_field = $field . '_id';
                         $name_field = $field . '_name';
                         $email_field = $field . '_email';
                     }
                     // if
                     if ($condition) {
                         db_execute('UPDATE ' . TABLE_PREFIX . "{$table_name} SET {$id_field} = 0, {$name_field} = ?, {$email_field} = ? WHERE {$id_field} = ? AND {$condition}", $this->getName(), $this->getEmail(), $this->getId());
                     } else {
                         db_execute('UPDATE ' . TABLE_PREFIX . "{$table_name} SET {$id_field} = 0, {$name_field} = ?, {$email_field} = ? WHERE {$id_field} = ?", $this->getName(), $this->getEmail(), $this->getId());
                     }
                     // if
                 }
                 // foreach
             }
             // foreach
         }
         // if
         db_commit();
         return true;
     } else {
         db_rollback();
         return $delete;
     }
     // if
 }
 /**
  * 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;
 }