Пример #1
0
 /**
  * Returns all elements in system as list
  *
  * @param $pb_root_elements_only boolean If true, then only root elements are returned; default is false
  * @param $pm_table_name_or_num mixed Optional table name or number to filter list with. If specified then only elements that have a type restriction to the table are returned. If omitted (default) then all elements, regardless of type restrictions, are returned.
  * @param $pm_type_name_or_id mixed Optional type code or type_id to restrict elements to.  If specified then only elements that have a type restriction to the specified table and type are returned. 
  * @param $pb_use_cache boolean Optional control for list cache; if true [default] then the will be cached for the request; if false the list will be generated from the database. The list is always generated at least once in the current request - there is no inter-request caching
  * @param $pa_data_types array Optional list of element data types to filter on.
  *
  * @return array A List of elements. Each list item is an array with keys set to field names; there is one additional value added with key "display_label" set to the display label of the element in the current locale
  */
 public static function getElementsAsList($pb_root_elements_only = false, $pm_table_name_or_num = null, $pm_type_name_or_id = null, $pb_use_cache = true, $pb_return_stats = false, $pb_index_by_element_code = false, $pa_data_types = null)
 {
     $o_dm = Datamodel::load();
     $vn_table_num = $o_dm->getTableNum($pm_table_name_or_num);
     $vs_cache_key = md5($pm_table_name_or_num . '/' . $pm_type_name_or_id . '/' . ($pb_root_elements_only ? '1' : '0') . '/' . ($pb_index_by_element_code ? '1' : '0') . print_R($pa_data_types, true));
     if ($pb_use_cache && ca_metadata_elements::$s_element_list_cache[$vs_cache_key]) {
         if ($pb_return_stats && isset(ca_metadata_elements::$s_element_list_cache[$vs_cache_key]['ui_counts']) || !$pb_return_stats) {
             return ca_metadata_elements::$s_element_list_cache[$vs_cache_key];
         }
     }
     if ($pb_return_stats) {
         $va_counts_by_attribute = ca_metadata_elements::getUIUsageCounts();
         $va_restrictions_by_attribute = ca_metadata_elements::getTypeRestrictionsAsList();
     }
     $vo_db = new Db();
     $va_wheres = array();
     $va_where_params = array();
     if ($pb_root_elements_only) {
         $va_wheres[] = 'cme.parent_id is NULL';
     }
     if ($vn_table_num) {
         $va_wheres[] = 'cmtr.table_num = ?';
         $va_where_params[] = (int) $vn_table_num;
         if ($pm_type_name_or_id) {
             $t_list_item = new ca_list_items();
             if (!is_numeric($pm_type_name_or_id)) {
                 $t_list_item->load(array('idno' => $pm_type_name_or_id));
             } else {
                 $t_list_item->load((int) $pm_type_name_or_id);
             }
             $va_type_ids = array();
             if ($vn_type_id = $t_list_item->getPrimaryKey()) {
                 $va_type_ids[$vn_type_id] = true;
                 if ($qr_children = $t_list_item->getHierarchy($vn_type_id, array())) {
                     while ($qr_children->nextRow()) {
                         $va_type_ids[$qr_children->get('item_id')] = true;
                     }
                 }
                 $va_wheres[] = '((cmtr.type_id = ?) OR (cmtr.include_subtypes = 1 AND cmtr.type_id IN (?)))';
                 $va_where_params[] = (int) $vn_type_id;
                 $va_where_params[] = $va_type_ids;
             }
         }
         $vs_wheres = ' WHERE ' . join(' AND ', $va_wheres);
         $qr_tmp = $vo_db->query("\n\t\t\t\tSELECT cme.*\n\t\t\t\tFROM ca_metadata_elements cme\n\t\t\t\tINNER JOIN ca_metadata_type_restrictions AS cmtr ON cme.hier_element_id = cmtr.element_id\n\t\t\t\t{$vs_wheres}\n\t\t\t", $va_where_params);
     } else {
         if (sizeof($va_wheres)) {
             $vs_wheres = ' WHERE ' . join(' AND ', $va_wheres);
         } else {
             $vs_wheres = '';
         }
         $qr_tmp = $vo_db->query("\n\t\t\t\tSELECT * \n\t\t\t\tFROM ca_metadata_elements cme\n\t\t\t\t{$vs_wheres}\n\t\t\t");
     }
     $va_return = array();
     $t_element = new ca_metadata_elements();
     $va_element_ids = array();
     while ($qr_tmp->nextRow()) {
         $vn_element_id = $qr_tmp->get('element_id');
         $vs_element_code = $qr_tmp->get('element_code');
         $vs_datatype = $qr_tmp->get('datatype');
         if (is_array($pa_data_types) && !in_array($vs_datatype, $pa_data_types)) {
             continue;
         }
         foreach ($t_element->getFields() as $vs_field) {
             $va_record[$vs_field] = $qr_tmp->get($vs_field);
         }
         $va_record['settings'] = caUnserializeForDatabase($qr_tmp->get('settings'));
         if ($pb_return_stats) {
             $va_record['ui_counts'] = $va_counts_by_attribute[$vs_code = $qr_tmp->get('element_code')];
             $va_record['restrictions'] = $va_restrictions_by_attribute[$vs_code];
         }
         $va_return[$vn_element_id] = $va_record;
     }
     // Get labels
     $va_labels = $t_element->getPreferredDisplayLabelsForIDs(array_keys($va_return));
     foreach ($va_labels as $vn_id => $vs_label) {
         $va_return[$vn_id]['display_label'] = $vs_label;
     }
     if ($pb_index_by_element_code) {
         $va_return_proc = array();
         foreach ($va_return as $vn_id => $va_element) {
             $va_return_proc[$va_element['element_code']] = $va_element;
         }
         $va_return = $va_return_proc;
     }
     return ca_metadata_elements::$s_element_list_cache[$vs_cache_key] = sizeof($va_return) > 0 ? $va_return : false;
 }