/**
  * Return visible types filter by project
  *
  * @param User $user
  * @param Project $project
  * @param array $only_types
  * @param boolean $use_cache
  * @return string
  */
 function getVisibleTypesFilterByProject($user, $project, $only_types = null, $use_cache = true)
 {
     $project_id = $project->getId();
     $cache_id = 'visible_project_types_filter_for_' . $user->getId();
     if ($only_types) {
         $cache_key = implode('-', $only_types);
     } else {
         $cache_key = 'all_types';
     }
     // if
     // Get and prepare cached value
     $cached_value = cache_get($cache_id);
     if (is_array($cached_value)) {
         if (!isset($cached_value[$project_id])) {
             $cached_value[$project_id] = array();
         }
         // if
     } else {
         $cached_value = array($project->getId() => array());
     }
     // if
     // From cache?
     if ($use_cache && isset($cached_value[$cache_key])) {
         return $cached_value[$cache_key];
     }
     // if
     // Nope, load...
     $project_objects_table = TABLE_PREFIX . 'project_objects';
     if ($only_types !== null) {
         $escaped_only_types = db_escape($only_types);
     }
     // if
     if ($user->isAdministrator() || $user->isProjectManager() || $user->isProjectLeader($project)) {
         if ($only_types === null) {
             $filter = "({$project_objects_table}.project_id = {$project_id})";
         } else {
             $filter = "({$project_objects_table}.project_id = {$project_id} AND type IN ({$escaped_only_types}))";
         }
         // if
         // Add to cache and return
         $cached_value[$project_id][$cache_key] = $filter;
         cache_set($cache_id, $cached_value);
         return $filter;
     }
     // if
     $types = ProjectUsers::getVisibleTypesByProject($user, $project);
     if ($only_types !== null) {
         foreach ($types as $k => $v) {
             if (!in_array($v, $only_types)) {
                 unset($types[$k]);
             }
             // if
         }
         // foreach
     }
     // if
     if (is_foreachable($types)) {
         $project_id = $project->getId();
         $escaped_types = db_escape($types);
         $filter = "({$project_objects_table}.project_id = '{$project_id}' AND ({$project_objects_table}.type IN ({$escaped_types}) OR ({$project_objects_table}.type IN ('Attachment', 'Task', 'Comment') AND {$project_objects_table}.parent_type IN ({$escaped_types}))))";
         $cached_value[$project_id][$cache_key] = $filter;
         cache_set($cache_id, $cached_value);
         return $filter;
     } else {
         $cached_value[$project_id][$cache_key] = '';
         cache_set($cache_id, $cached_value);
         return '';
     }
     // if
 }