/** * Return categories that have documents $user can see * * Only if $user is administrator or can see private objects all categories * are returned * * @param User $user * @return array */ function findAll($user) { if ($user->isAdministrator() || $user->canSeePrivate()) { return DocumentCategories::find(array('order' => 'name')); } else { $document_categories_table = TABLE_PREFIX . 'document_categories'; $documents_table = TABLE_PREFIX . 'documents'; return DocumentCategories::findBySQL("SELECT DISTINCT {$document_categories_table}.* FROM {$document_categories_table}, {$documents_table} WHERE {$document_categories_table}.id = {$documents_table}.category_id AND {$documents_table}.visibility >= ? ORDER BY {$document_categories_table}.name", array(VISIBILITY_NORMAL)); } // if }
/** * Render select Document Category helper * * Params: * * - Standard select box attributes * - value - ID of selected role * - optional - Wether value is optional or not * - can_create_new - Can the user create new category or not, default is true * * @param array $params * @param Smarty $smarty * @return string */ function smarty_function_select_document_category($params, &$smarty) { static $ids = array(); $user = array_var($params, 'user', null, true); if (!instance_of($user, 'User')) { return new InvalidParamError('user', $user, '$user is expected to be a valid User object', true); } // if $value = array_var($params, 'value', null, true); $can_create_new = array_var($params, 'can_create_new', true, true); $id = array_var($params, 'id', null, true); if (empty($id)) { $counter = 1; do { $id = "select_document_category_dropdown_{$counter}"; $counter++; } while (in_array($id, $ids)); } // if $ids[] = $id; $params['id'] = $id; $options = array(); $categories = DocumentCategories::findAll($user); if (is_foreachable($categories)) { foreach ($categories as $category) { $option_attributes = array('class' => 'object_option'); if ($value == $category->getId()) { $option_attributes['selected'] = true; } // if $options[] = option_tag($category->getName(), $category->getId(), $option_attributes); } // foreach } // if if ($can_create_new) { $params['add_object_url'] = assemble_url('document_categories_quick_add'); $params['object_name'] = 'document_category'; $params['add_object_message'] = lang('Please insert new document category name'); $logged_user = get_logged_user(); if (instance_of($logged_user, 'User') && DocumentCategory::canAdd($logged_user)) { $options[] = option_tag('', ''); $options[] = option_tag(lang('New Category...'), '', array('class' => 'new_object_option')); } // if } // if return select_box($options, $params) . '<script type="text/javascript">$("#' . $id . '").new_object_from_select();</script>'; }
/** * Render link to a specific category * * Parameters: * * - category_id - integer, return parent category ID. * * @param array $params * @param Smarty $smarty * @return string */ function smarty_function_in_category_link($params, &$smarty) { $category_id = array_var($params, 'category_id'); $user = array_var($params, 'user', null); $category = DocumentCategories::findById($category_id); if (instance_of($category, 'DocumentCategory')) { if ($category->canView($user)) { return '<a href="' . $category->getViewUrl() . '">' . clean($category->getName()) . '</a>'; } else { return clean($category->getName()); } // if } // if return '<span class="unknown_category_link unknown_object_link">' . lang('Unknown Category') . '</span>'; }
/** * Constructor method * * @param string $request * @return DocumentsCategoriesController */ function __construct($request) { parent::__construct($request); if (!$this->logged_user->isAdministrator() && !$this->logged_user->getSystemPermission('can_use_documents')) { $this->httpError(HTTP_ERR_FORBIDDEN); } // if $this->wireframe->addBreadCrumb(lang('Documents'), assemble_url('documents')); $category_id = $this->request->getId('category_id'); if ($category_id) { $this->active_document_category = DocumentCategories::findById($category_id); } // if if (instance_of($this->active_document_category, 'DocumentCategory')) { $this->wireframe->addBreadCrumb($this->active_document_category->getName(), $this->active_document_category->getViewUrl()); if (Document::canAdd($this->logged_user)) { $add_text_url = assemble_url('documents_add_text', array('category_id' => $this->active_document_category->getId())); $upload_file_url = assemble_url('documents_upload_file', array('category_id' => $this->active_document_category->getId())); } // if } else { $this->active_document_category = new DocumentCategory(); if (Document::canAdd($this->logged_user)) { $add_text_url = assemble_url('documents_add_text'); $upload_file_url = assemble_url('documents_upload_file'); } // if } // if if (Document::canAdd($this->logged_user)) { $this->wireframe->addPageAction(lang('New Text Document'), $add_text_url); $this->wireframe->addPageAction(lang('Upload File'), $upload_file_url); } else { $add_text_url = null; $upload_file_url = null; } // if $this->smarty->assign(array('document_categories_url' => $this->logged_user->isAdministrator() ? assemble_url('document_categories') : null, 'add_category_url' => DocumentCategory::canAdd($this->logged_user) ? assemble_url('document_categories_add') : null, 'add_text_url' => $add_text_url, 'upload_file_url' => $upload_file_url, 'active_document_category' => $this->active_document_category, 'categories' => DocumentCategories::findAll($this->logged_user))); }
/** * Returns true if $user can add documents * * @param User $user * @return boolean */ function canAdd($user) { return $user->isAdministrator() || $user->getSystemPermission('can_add_documents') && (bool) DocumentCategories::findAll($user); }