function getWebpages($project, $tag = '', $page = 1, $webpages_per_page = 10, $orderBy = 'title', $orderDir = 'ASC', $archived = false)
 {
     $orderDir = strtoupper($orderDir);
     if ($orderDir != "ASC" && $orderDir != "DESC") {
         $orderDir = "ASC";
     }
     if ($page < 0) {
         $page = 1;
     }
     //$conditions = logged_user()->isMemberOfOwnerCompany() ? '' : ' `is_private` = 0';
     if ($tag == '' || $tag == null) {
         $tagstr = "1=1";
     } else {
         $tagstr = "(SELECT count(*) FROM `" . TABLE_PREFIX . "tags` WHERE `" . TABLE_PREFIX . "project_webpages`.`id` = `" . TABLE_PREFIX . "tags`.`rel_object_id` AND `" . TABLE_PREFIX . "tags`.`tag` = " . DB::escape($tag) . " AND `" . TABLE_PREFIX . "tags`.`rel_object_manager` = 'ProjectWebpages' ) > 0 ";
     }
     $permission_str = ' AND (' . permissions_sql_for_listings(ProjectWebpages::instance(), ACCESS_LEVEL_READ, logged_user()) . ')';
     if ($project instanceof Project) {
         $pids = $project->getAllSubWorkspacesCSV(true);
         $project_str = " AND " . self::getWorkspaceString($pids);
     } else {
         $project_str = "";
     }
     if ($archived) {
         $archived_cond = " AND `archived_by_id` <> 0";
     } else {
         $archived_cond = " AND `archived_by_id` = 0";
     }
     $conditions = $tagstr . $permission_str . $project_str . $archived_cond;
     return ProjectWebpages::paginate(array("conditions" => $conditions, 'order' => DB::escapeField($orderBy) . " {$orderDir}"), config_option('files_per_page', 10), $page);
     // paginate
 }
 function list_all()
 {
     ajx_current("empty");
     $context = active_context();
     $start = array_var($_GET, 'start', 0);
     $limit = array_var($_GET, 'limit', config_option('files_per_page'));
     $order = array_var($_GET, 'sort');
     if ($order == "updatedOn" || $order == "updated" || $order == "date" || $order == "dateUpdated") {
         $order = "updated_on";
     }
     $order_dir = array_var($_GET, 'dir');
     $page = (int) ($start / $limit) + 1;
     $hide_private = !logged_user()->isMemberOfOwnerCompany();
     if (array_var($_GET, 'action') == 'delete') {
         $ids = explode(',', array_var($_GET, 'webpages'));
         $succ = 0;
         $err = 0;
         foreach ($ids as $id) {
             $web_page = ProjectWebpages::findById($id);
             if (isset($web_page) && $web_page->canDelete(logged_user())) {
                 try {
                     DB::beginWork();
                     $web_page->trash();
                     ApplicationLogs::createLog($web_page, ApplicationLogs::ACTION_TRASH);
                     DB::commit();
                     $succ++;
                 } catch (Exception $e) {
                     DB::rollback();
                     $err++;
                 }
             } else {
                 $err++;
             }
         }
         if ($succ > 0) {
             flash_success(lang("success delete objects", $succ));
         }
         if ($err > 0) {
             flash_error(lang("error delete objects", $err));
         }
     } else {
         if (array_var($_GET, 'action') == 'markasread') {
             $ids = explode(',', array_var($_GET, 'ids'));
             $succ = 0;
             $err = 0;
             foreach ($ids as $id) {
                 $webpage = ProjectWebpages::findById($id);
                 try {
                     $webpage->setIsRead(logged_user()->getId(), true);
                     $succ++;
                 } catch (Exception $e) {
                     $err++;
                 }
             }
             if ($succ <= 0) {
                 flash_error(lang("error markasread files", $err));
             }
         } else {
             if (array_var($_GET, 'action') == 'markasunread') {
                 $ids = explode(',', array_var($_GET, 'ids'));
                 $succ = 0;
                 $err = 0;
                 foreach ($ids as $id) {
                     $webpage = ProjectWebpages::findById($id);
                     try {
                         $webpage->setIsRead(logged_user()->getId(), false);
                         $succ++;
                     } catch (Exception $e) {
                         $err++;
                     }
                 }
                 if ($succ <= 0) {
                     flash_error(lang("error markasunread files", $err));
                 }
             } else {
                 if (array_var($_GET, 'action') == 'archive') {
                     $ids = explode(',', array_var($_GET, 'webpages'));
                     $succ = 0;
                     $err = 0;
                     foreach ($ids as $id) {
                         $web_page = ProjectWebpages::findById($id);
                         if (isset($web_page) && $web_page->canEdit(logged_user())) {
                             try {
                                 DB::beginWork();
                                 $web_page->archive();
                                 ApplicationLogs::createLog($web_page, ApplicationLogs::ACTION_ARCHIVE);
                                 DB::commit();
                                 $succ++;
                             } catch (Exception $e) {
                                 DB::rollback();
                                 $err++;
                             }
                         } else {
                             $err++;
                         }
                     }
                     if ($succ > 0) {
                         flash_success(lang("success archive objects", $succ));
                     }
                     if ($err > 0) {
                         flash_error(lang("error archive objects", $err));
                     }
                 }
             }
         }
     }
     $res = ProjectWebpages::instance()->listing(array("order" => $order, "order_dir" => $order_dir));
     $object = array("totalCount" => $res->total, "start" => $start, "webpages" => array());
     if (isset($res->objects)) {
         $index = 0;
         $ids = array();
         foreach ($res->objects as $w) {
             $ids[] = $w->getId();
             $object["webpages"][] = array("ix" => $index++, "id" => $w->getId(), "object_id" => $w->getObjectId(), "ot_id" => $w->getObjectTypeId(), "name" => $w->getObjectName(), "description" => $w->getDescription(), "url" => $w->getUrl(), "updatedOn" => $w->getUpdatedOn() instanceof DateTimeValue ? $w->getUpdatedOn()->isToday() ? format_time($w->getUpdatedOn()) : format_datetime($w->getUpdatedOn()) : '', "updatedOn_today" => $w->getUpdatedOn() instanceof DateTimeValue ? $w->getUpdatedOn()->isToday() : 0, "updatedBy" => $w->getUpdatedByDisplayName(), "updatedById" => $w->getUpdatedById(), "memPath" => json_encode($w->getMembersToDisplayPath()));
         }
         $read_objects = ReadObjects::getReadByObjectList($ids, logged_user()->getId());
         foreach ($object["webpages"] as &$data) {
             $data['isRead'] = isset($read_objects[$data['object_id']]);
         }
     }
     ajx_extra_data($object);
 }
 /**
  * This function will return paginated result. Result is an array where first element is
  * array of returned object and second populated pagination object that can be used for
  * obtaining and rendering pagination data using various helpers.
  *
  * Items and pagination array vars are indexed with 0 for items and 1 for pagination
  * because you can't use associative indexing with list() construct
  *
  * @access public
  * @param array $arguments Query argumens (@see find()) Limit and offset are ignored!
  * @param integer $items_per_page Number of items per page
  * @param integer $current_page Current page number
  * @return array
  */
 function paginate($arguments = null, $items_per_page = 10, $current_page = 1)
 {
     if (isset($this) && instance_of($this, 'ProjectWebpages')) {
         return parent::paginate($arguments, $items_per_page, $current_page);
     } else {
         return ProjectWebpages::instance()->paginate($arguments, $items_per_page, $current_page);
         //$instance =& ProjectWebpages::instance();
         //return $instance->paginate($arguments, $items_per_page, $current_page);
     }
     // if
 }
	function canAdd(Contact $user, $context, &$notAllowedMember = ''){
		return can_add($user, $context, ProjectWebpages::instance()->getObjectTypeId(), $notAllowedMember);
	}
 /**
  * Check if specific user can add webpages to specific project
  *
  * @access public
  * @param User $user
  * @param Project $project
  * @return booelean
  */
 function canAdd(User $user, Project $project)
 {
     return can_add($user, $project, get_class(ProjectWebpages::instance()));
 }
 /**
  * Return manager instance
  *
  * @access protected
  * @param void
  * @return ProjectWebpages
  */
 function manager()
 {
     if (!$this->manager instanceof ProjectWebpages) {
         $this->manager = ProjectWebpages::instance();
     }
     return $this->manager;
 }
 /**
  * Returns array of queries that will return Dashboard Objects
  *
  * @param string $proj_ids
  * @param string $tag
  * @param boolean $count if false the query will return objects, if true it will return object count
  */
 static function getDashboardObjectQueries($project = null, $tag = null, $count = false, $trashed = false, $linkedObject = null, $order = 'updatedOn', $filterName = '', $archived = false, $filterManager = '')
 {
     if ($trashed && $trashed !== 'all') {
         $order = 'trashedOn';
     } else {
         if ($archived) {
             $order = 'archivedOn';
         }
     }
     switch ($order) {
         case 'dateCreated':
             $order_crit_companies = '`created_on`';
             $order_crit_contacts = '`created_on`';
             $order_crit_file_revisions = '`created_on`';
             $order_crit_calendar = '`created_on`';
             $order_crit_tasks = '`created_on`';
             $order_crit_milestones = '`created_on`';
             $order_crit_webpages = '`created_on`';
             $order_crit_files = '`created_on`';
             $order_crit_emails = '`received_date`';
             $order_crit_comments = '`created_on`';
             $order_crit_messages = '`created_on`';
             $order_crit_workspaces = '`created_on`';
             break;
         case 'trashedOn':
             $order_crit_companies = '`trashed_on`';
             $order_crit_contacts = '`trashed_on`';
             $order_crit_file_revisions = '`trashed_on`';
             $order_crit_calendar = '`trashed_on`';
             $order_crit_tasks = '`trashed_on`';
             $order_crit_milestones = '`trashed_on`';
             $order_crit_webpages = '`trashed_on`';
             $order_crit_files = '`trashed_on`';
             $order_crit_emails = '`trashed_on`';
             $order_crit_comments = '`trashed_on`';
             $order_crit_messages = '`trashed_on`';
             $order_crit_workspaces = '`updated_on`';
             break;
         case 'archivedOn':
             $order_crit_companies = '`archived_on`';
             $order_crit_contacts = '`archived_on`';
             $order_crit_file_revisions = '`updated_on`';
             $order_crit_calendar = '`archived_on`';
             $order_crit_tasks = '`archived_on`';
             $order_crit_milestones = '`archived_on`';
             $order_crit_webpages = '`archived_on`';
             $order_crit_files = '`archived_on`';
             $order_crit_emails = '`archived_on`';
             $order_crit_comments = '`updated_on`';
             $order_crit_messages = '`archived_on`';
             $order_crit_workspaces = '`completed_on`';
             break;
         case 'name':
             $order_crit_companies = '`name`';
             $order_crit_contacts = "TRIM(CONCAT(' ', `lastname`, `firstname`, `middlename`))";
             $order_crit_file_revisions = "'zzzzzzzzzzzzzz'";
             //Revisar
             $order_crit_calendar = '`subject`';
             $order_crit_tasks = '`title`';
             $order_crit_milestones = '`name`';
             $order_crit_webpages = '`title`';
             $order_crit_files = '`filename`';
             $order_crit_emails = '`subject`';
             $order_crit_comments = '`text`';
             $order_crit_messages = '`title`';
             $order_crit_workspaces = '`name`';
             break;
         default:
             $order_crit_companies = '`updated_on`';
             $order_crit_contacts = '`updated_on`';
             $order_crit_file_revisions = '`updated_on`';
             $order_crit_calendar = '`updated_on`';
             $order_crit_tasks = '`updated_on`';
             $order_crit_milestones = '`updated_on`';
             $order_crit_webpages = '`updated_on`';
             $order_crit_files = '`updated_on`';
             $order_crit_emails = '`received_date`';
             $order_crit_comments = '`updated_on`';
             $order_crit_messages = '`updated_on`';
             $order_crit_workspaces = '`updated_on`';
             break;
     }
     if ($project instanceof Project) {
         $proj_ids = $project->getAllSubWorkspacesQuery(true);
         $proj_cond_companies = Companies::getWorkspaceString($proj_ids);
         $proj_cond_messages = ProjectMessages::getWorkspaceString($proj_ids);
         $proj_cond_documents = ProjectFiles::getWorkspaceString($proj_ids);
         $proj_cond_emails = MailContents::getWorkspaceString($proj_ids);
         $proj_cond_events = ProjectEvents::getWorkspaceString($proj_ids);
         $proj_cond_tasks = ProjectTasks::getWorkspaceString($proj_ids);
         $proj_cond_charts = ProjectCharts::getWorkspaceString($proj_ids);
         $proj_cond_milestones = ProjectMilestones::getWorkspaceString($proj_ids);
         $proj_cond_weblinks = ProjectWebpages::getWorkspaceString($proj_ids);
         $proj_cond_contacts = Contacts::getWorkspaceString($proj_ids);
     } else {
         $proj_cond_companies = "true";
         $proj_cond_messages = "true";
         $proj_cond_documents = "true";
         $proj_cond_emails = "true";
         $proj_cond_events = "true";
         $proj_cond_tasks = "true";
         $proj_cond_charts = "true";
         $proj_cond_milestones = "true";
         $proj_cond_weblinks = "true";
         $proj_cond_contacts = "true";
     }
     if ($trashed) {
         if ($trashed === 'all') {
             $trashed_cond = '`trashed_on` >= ' . DB::escape(EMPTY_DATETIME);
         } else {
             $trashed_cond = '`trashed_on` > ' . DB::escape(EMPTY_DATETIME);
         }
         $archived_cond = '1 = 1';
         // Show all objects in trash
         $comments_arch_cond = "1 = 1";
     } else {
         $trashed_cond = '`trashed_on` = ' . DB::escape(EMPTY_DATETIME);
         if ($archived) {
             $archived_cond = "`archived_by_id` > 0";
             $comments_arch_cond = "1 = 0";
             // Don't show comments in archived objects listings
         } else {
             $archived_cond = "`archived_by_id` = 0";
             $comments_arch_cond = "1 = 1";
         }
     }
     if (isset($tag) && $tag && $tag != '') {
         $tag_str = " AND EXISTS (SELECT * FROM `" . TABLE_PREFIX . "tags` `t` WHERE `tag`= " . DB::escape($tag) . " AND `co`.`id` = `t`.`rel_object_id` AND `t`.`rel_object_manager` = `object_manager_value`) ";
     } else {
         $tag_str = ' ';
     }
     if ($linkedObject instanceof ProjectDataObject) {
         $link_id = $linkedObject->getId();
         $link_mgr = get_class($linkedObject->manager());
         $link_str = " AND EXISTS (SELECT * FROM `" . TABLE_PREFIX . "linked_objects` `t` WHERE\n\t\t\t(`t`.`object_id`=" . DB::escape($link_id) . " AND `t`.object_manager = " . DB::escape($link_mgr) . " AND `co`.`id` = `t`.`rel_object_id` AND `t`.`rel_object_manager` = `object_manager_value`) OR\n\t\t\t(`t`.`rel_object_id`=" . DB::escape($link_id) . " AND `t`.rel_object_manager = " . DB::escape($link_mgr) . " AND `co`.`id` = `t`.`object_id` AND `t`.`object_manager` = `object_manager_value`)) ";
     } else {
         $link_str = ' ';
     }
     $tag_str .= $link_str;
     $res = array();
     /** If the name of the query ends with Comments it is assumed to be a list of Comments **/
     $cfn = '';
     if ($filterName != '') {
         $cfn = " AND text LIKE '%" . $filterName . "%'";
     }
     // Notes
     if (module_enabled('notes')) {
         $fn = '';
         if ($filterName != '') {
             $fn = " AND title LIKE '%" . $filterName . "%'";
         }
         $permissions = ' AND ( ' . permissions_sql_for_listings(ProjectMessages::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "ProjectMessages") {
             $res['ProjectMessages'] = "SELECT  'ProjectMessages' AS `object_manager_value`, `id` AS `oid`, {$order_crit_messages} AS `order_value` FROM `" . TABLE_PREFIX . "project_messages` `co` WHERE " . $trashed_cond . " AND {$archived_cond} AND " . $proj_cond_messages . str_replace('= `object_manager_value`', "= 'ProjectMessages'", $tag_str) . $permissions . $fn;
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['ProjectMessagesComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'ProjectMessages' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "project_messages` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND " . $proj_cond_messages . str_replace('= `object_manager_value`', "= 'ProjectMessages'", $tag_str) . $permissions . $cfn . ")";
         }
     }
     // Events
     if (module_enabled("calendar")) {
         $fn = '';
         if ($filterName != '') {
             $fn = " AND subject LIKE '%" . $filterName . "%'";
         }
         $permissions = ' AND ( ' . permissions_sql_for_listings(ProjectEvents::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "ProjectEvents") {
             $res['ProjectEvents'] = "SELECT  'ProjectEvents' AS `object_manager_value`, `id` AS `oid`, {$order_crit_calendar} AS `order_value` FROM `" . TABLE_PREFIX . "project_events` `co` WHERE  " . $trashed_cond . " AND {$archived_cond} AND " . $proj_cond_events . str_replace('= `object_manager_value`', "= 'ProjectEvents'", $tag_str) . $permissions . $fn;
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['ProjectEventsComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'ProjectEvents' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "project_events` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND " . $proj_cond_events . str_replace('= `object_manager_value`', "= 'ProjectEvents'", $tag_str) . $permissions . $cfn . ")";
         }
     }
     // Documents
     if (module_enabled("documents")) {
         $fn = '';
         if ($filterName != '') {
             $fn = " AND filename LIKE '%" . $filterName . "%'";
         }
         $permissions = ' AND ( ' . permissions_sql_for_listings(ProjectFiles::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         $typestring = array_var($_GET, "typestring");
         if ($typestring) {
             $typecond = " AND  ((SELECT count(*) FROM `" . TABLE_PREFIX . "project_file_revisions` `pfr` WHERE `" . "pfr`.`type_string` LIKE " . DB::escape($typestring) . " AND `" . "co`.`id` = `pfr`.`file_id`) > 0)";
         } else {
             $typecond = "";
         }
         if ($filterManager == '' || $filterManager == "ProjectFiles") {
             $res['ProjectFiles'] = "SELECT  'ProjectFiles' AS `object_manager_value`, `id` as `oid`, {$order_crit_files} AS `order_value` FROM `" . TABLE_PREFIX . "project_files` `co` WHERE " . $trashed_cond . " AND {$archived_cond} AND " . $proj_cond_documents . str_replace('= `object_manager_value`', "= 'ProjectFiles'", $tag_str) . $permissions . $typecond . $fn;
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['ProjectFilesComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'ProjectFiles' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "project_files` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND " . $proj_cond_documents . str_replace('= `object_manager_value`', "= 'ProjectFiles'", $tag_str) . $permissions . $cfn . ")";
         }
         if ($trashed) {
             $file_rev_docs = "SELECT `id` FROM `" . TABLE_PREFIX . "project_files` `co` WHERE `trashed_by_id` = 0 AND " . $proj_cond_documents . str_replace('= `object_manager_value`', "= 'ProjectFiles'", $tag_str) . $permissions . $typecond;
             $res['FileRevisions'] = "SELECT 'ProjectFileRevisions' AS `object_manager_value`, `id` AS `oid`, {$order_crit_file_revisions} AS `order_value` FROM `" . TABLE_PREFIX . "project_file_revisions` `co` WHERE {$trashed_cond} AND `file_id` IN (" . $file_rev_docs . ")";
         }
     }
     // Tasks and Milestones
     if (module_enabled("tasks")) {
         $fn = '';
         if ($filterName != '') {
             $fn = " AND title LIKE '%" . $filterName . "%'";
         }
         $completed = $trashed || $archived ? '' : 'AND `completed_on` = ' . DB::escape(EMPTY_DATETIME);
         $permissions = ' AND ( ' . permissions_sql_for_listings(ProjectTasks::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "ProjectTasks") {
             $res['ProjectTasks'] = "SELECT  'ProjectTasks' AS `object_manager_value`, `id` AS `oid`, {$order_crit_tasks} AS `order_value` FROM `" . TABLE_PREFIX . "project_tasks` `co` WHERE `is_template` = false {$completed} AND " . $trashed_cond . " AND {$archived_cond} AND `is_template` = false AND " . $proj_cond_tasks . str_replace('= `object_manager_value`', "= 'ProjectTasks'", $tag_str) . $permissions . $fn;
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['ProjectTasksComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'ProjectTasks' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "project_tasks` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND `is_template` = false AND " . $proj_cond_tasks . str_replace('= `object_manager_value`', "= 'ProjectTasks'", $tag_str) . $permissions . $cfn . ")";
         }
         $fn = '';
         if ($filterName != '') {
             $fn = " AND name LIKE '%" . $filterName . "%'";
         }
         $permissions = ' AND ( ' . permissions_sql_for_listings(ProjectMilestones::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "ProjectMilestones") {
             $res['ProjectMilestones'] = "SELECT  'ProjectMilestones' AS `object_manager_value`, `id` AS `oid`, {$order_crit_milestones} AS `order_value` FROM `" . TABLE_PREFIX . "project_milestones` `co` WHERE " . $trashed_cond . " AND {$archived_cond} AND `is_template` = false AND " . $proj_cond_milestones . str_replace('= `object_manager_value`', "= 'ProjectMilestones'", $tag_str) . $permissions . $fn;
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['ProjectMilestonesComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'ProjectMilestones' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "project_milestones` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND `is_template` = false AND " . $proj_cond_milestones . str_replace('= `object_manager_value`', "= 'ProjectMilestones'", $tag_str) . $permissions . $cfn . ")";
         }
     }
     // Weblinks
     if (module_enabled("weblinks")) {
         $fn = '';
         if ($filterName != '') {
             $fn = " AND title LIKE '%" . $filterName . "%'";
         }
         $permissions = ' AND ( ' . permissions_sql_for_listings(ProjectWebpages::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "ProjectWebpages") {
             $res['ProjectWebPages'] = "SELECT  'ProjectWebPages' AS `object_manager_value`, `id` AS `oid`, {$order_crit_webpages} AS `order_value` FROM `" . TABLE_PREFIX . "project_webpages` `co` WHERE " . $trashed_cond . " AND {$archived_cond} AND " . $proj_cond_weblinks . str_replace('= `object_manager_value`', "= 'ProjectWebpages'", $tag_str) . $permissions . $fn;
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['ProjectWebPagesComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'ProjectWebpages' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "project_webpages` `co` WHERE " . $trashed_cond . " AND {$comments_arch_cond} AND " . $proj_cond_weblinks . str_replace('= `object_manager_value`', "= 'ProjectWebpages'", $tag_str) . $permissions . $cfn . ")";
         }
     }
     // Email
     if (module_enabled("email")) {
         $fn = '';
         if ($filterName != '') {
             $fn = " AND subject LIKE '%" . $filterName . "%'";
         }
         $permissions = ' AND ( ' . permissions_sql_for_listings(MailContents::instance(), ACCESS_LEVEL_READ, logged_user(), $project instanceof Project ? $project->getId() : 0, '`co`') . ')';
         if ($filterManager == '' || $filterManager == "MailContents") {
             $res['MailContents'] = "SELECT  'MailContents' AS `object_manager_value`, `id` AS `oid`, {$order_crit_emails} AS `order_value` FROM `" . TABLE_PREFIX . "mail_contents` `co` WHERE (" . $trashed_cond . " AND {$archived_cond} AND `is_deleted` = 0 AND " . $proj_cond_emails . str_replace('= `object_manager_value`', "= 'MailContents'", $tag_str) . $permissions . ") {$fn}";
         }
         if ($filterManager == '' || $filterManager == "Comments") {
             $res['MailContentsComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'MailContents' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "mail_contents` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND " . $proj_cond_emails . str_replace('= `object_manager_value`', "= 'MailContents'", $tag_str) . $permissions . $cfn . ")";
         }
     }
     // Conacts and Companies
     if (module_enabled("contacts")) {
         $fn = '';
         $fn2 = '';
         if ($filterName != '') {
             $fn = " AND firstname LIKE '%" . $filterName . "%'";
             $fn2 = " AND name LIKE '%" . $filterName . "%'";
         }
         // companies
         $permissions = ' AND ( ' . permissions_sql_for_listings(Companies::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "Companies") {
             $res['Companies'] = "SELECT  'Companies' AS `object_manager_value`, `id` as `oid`, {$order_crit_companies} AS `order_value` FROM `" . TABLE_PREFIX . "companies` `co` WHERE " . $trashed_cond . " AND {$archived_cond} AND " . $proj_cond_companies . str_replace('= `object_manager_value`', "= 'Companies'", $tag_str) . $permissions . $fn2;
         }
         $res['CompaniesComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'Companies' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "companies` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND " . $proj_cond_documents . str_replace('= `object_manager_value`', "= 'Companies'", $tag_str) . $permissions . $cfn . ")";
         // contacts
         $permissions = ' AND ( ' . permissions_sql_for_listings(Contacts::instance(), ACCESS_LEVEL_READ, logged_user(), '`project_id`', '`co`') . ')';
         if ($filterManager == '' || $filterManager == "Contacts") {
             $res['Contacts'] = "SELECT 'Contacts' AS `object_manager_value`, `id` AS `oid`, {$order_crit_contacts} AS `order_value` FROM `" . TABLE_PREFIX . "contacts` `co` WHERE {$trashed_cond} AND {$archived_cond} AND {$proj_cond_contacts} " . str_replace('= `object_manager_value`', "= 'Contacts'", $tag_str) . $permissions . $fn;
         }
         $res['ContactsComments'] = "SELECT  'Comments' AS `object_manager_value`, `id` AS `oid`, {$order_crit_comments} AS `order_value` FROM `" . TABLE_PREFIX . "comments` WHERE {$trashed_cond} AND `rel_object_manager` = 'Contacts' AND `rel_object_id` IN (SELECT `co`.`id` FROM `" . TABLE_PREFIX . "contacts` `co` WHERE `trashed_by_id` = 0 AND {$comments_arch_cond} AND " . $proj_cond_documents . str_replace('= `object_manager_value`', "= 'Contacts'", $tag_str) . $permissions . $cfn . ")";
     }
     // Workspaces (only for archived objects view)
     if ($archived) {
         if ($filterManager == '' || $filterManager == "Projects") {
             $res['Projects'] = "SELECT  'Projects' AS `object_manager_value`, `id` AS `oid`, {$order_crit_workspaces} AS `order_value` FROM `" . TABLE_PREFIX . "projects` `co` WHERE `completed_on` <> " . DB::escape(EMPTY_DATETIME) . " AND `id` IN (" . logged_user()->getWorkspacesQuery() . ")";
         }
     }
     if ($count) {
         foreach ($res as $p => $q) {
             $res[$p] = "SELECT count(*) AS `quantity`, '{$p}' AS `objectName` FROM ( {$q} ) `table_alias`";
         }
     }
     return $res;
 }
 /**
 * Return manager instance
 *
 * @access protected
 * @param void
 * @return ProjectWebpages
 */
 function manager() {
   if(!($this->manager instanceof ProjectWebpages)) $this->manager = ProjectWebpages::instance();
   return $this->manager;
 } // manager