/**
  * 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 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
 }
 function starred()
 {
     $objects = StarredObjects::findByUser($this->logged_user);
     $this->smarty->assign(array("objects" => $objects, "page_title" => lang('Starred')));
 }
 /**
  * Check if $object is starred by $user
  *
  * @param ProjectObject $object
  * @param User $user
  * @return boolean
  */
 function isStarred($object, $user)
 {
     $cache_id = 'object_starred_by_' . $user->getId();
     $starred_objects = cache_get($cache_id);
     if (!is_array($starred_objects)) {
         $starred_objects = StarredObjects::findObjectIdsByUser($user);
         cache_set($cache_id, $starred_objects);
     }
     // if
     return in_array($object->getId(), $starred_objects);
 }
 /**
  * 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;
 }
 /**
  * Starred Project Objects
  *
  * @param void
  * @return null
  */
 function starred()
 {
     $this->wireframe->current_menu_item = 'starred_folder';
     if ($this->request->isSubmitted()) {
         $action = $this->request->post('action');
         if (!in_array($action, array('unstar', 'unstar_and_complete', 'trash'))) {
             $this->httpError(HTTP_ERR_BAD_REQUEST, 'Invalid action');
         }
         // if
         $objects = ProjectObjects::findByIds($this->request->post('objects'), STATE_VISIBLE, $this->logged_user->getVisibility());
         db_begin_work();
         foreach ($objects as $object) {
             // Unstar selected object
             if ($action == 'unstar') {
                 $object->unstar($this->logged_user);
                 // Unstar and marked as completed
             } elseif ($action == 'unstar_and_complete') {
                 $operation = $object->unstar($this->logged_user);
                 if ($operation && !is_error($operation)) {
                     if ($object->can_be_completed) {
                         $object->complete($this->logged_user);
                     }
                     // if
                 }
                 // if
                 // Move to Trash
             } elseif ($action == 'trash') {
                 if (!$object->canDelete($this->logged_user)) {
                     continue;
                 }
                 // if
                 $object->moveToTrash();
             }
             // if
         }
         // foreach
         db_commit();
     }
     // if
     $this->smarty->assign('objects', StarredObjects::findByUser($this->logged_user));
     $this->smarty->assign('user_pages', StarredObjects::findStarredPagesByUser($this->logged_user->getId(), '2'));
     if ($this->request->get('async')) {
         $this->smarty->display(get_template_path('starred', 'dashboard', SYSTEM_MODULE));
         die;
     }
     // if
 }