Esempio n. 1
0
 protected function build_compound_crits_to_words(Utils_RecordBrowser_Crits $crits)
 {
     $parts = array();
     foreach ($crits->get_component_crits() as $c) {
         $words = $this->to_words_internal($c);
         if ($words['str']) {
             $parts[] = $words;
         }
     }
     if (!$parts) {
         return self::$empty_c;
     }
     $multiple = count($parts) > 1;
     foreach ($parts as $k => $p) {
         $parts[$k] = $multiple && $p['multiple'] ? $this->cc($p['str']) : $p['str'];
     }
     $join_operator = strtolower($crits->get_join_operator());
     $glue = ' ' . _V($join_operator) . ' ';
     $neg = $crits->get_negation() ? ' ' . __('Not') : '';
     $str = implode($glue, $parts);
     if ($neg) {
         if ($multiple) {
             $str = $this->cc($str);
         }
         $str = "{$neg} {$str}";
         $multiple = false;
     }
     return array('str' => $str, 'multiple' => $multiple);
 }
Esempio n. 2
0
 public function get_rules($crits)
 {
     if (!is_object($crits)) {
         $crits = \Utils_RecordBrowser_Crits::from_array($crits);
     }
     /** @var Utils_RecordBrowser_Crits $crits */
     $ret = $this->crits_to_json($crits);
     return $ret;
 }
Esempio n. 3
0
 public static function crits_to_words($tab, $crits, $html_decoration = true)
 {
     if (!is_object($crits)) {
         $crits = Utils_RecordBrowser_Crits::from_array($crits);
     }
     $crits = $crits->replace_special_values(true);
     $c2w = new Utils_RecordBrowser_CritsToWords($tab);
     $c2w->enable_html_decoration($html_decoration);
     return $c2w->to_words($crits);
 }
Esempio n. 4
0
 protected function hf_select($field, $operator, $value, $raw_sql_val, $field_def)
 {
     $commondata = isset($field_def['commondata']) && $field_def['commondata'];
     if ($commondata) {
         return $this->hf_commondata($field, $operator, $value, $raw_sql_val, $field_def);
     }
     $sql = '';
     $vals = array();
     list($field, $sub_field) = Utils_RecordBrowser_CritsSingle::parse_subfield($field);
     $multiselect = $field_def['type'] == 'multiselect';
     $tab2 = isset($field_def['ref_table']) ? $field_def['ref_table'] : false;
     $single_tab = !($tab2 == '__RECORDSETS__' || count(explode(',', $tab2)) > 1);
     if ($sub_field && $single_tab && $tab2) {
         $col2 = explode('|', $sub_field);
         if (!isset($this->subqueries_tab_ids[$tab2])) {
             $this->subqueries_tab_ids[$tab2] = 0;
         }
         $tab_alias_id = $this->subqueries_tab_ids[$tab2]++;
         $nested_tab_alias = $this->tab_alias . '_' . $tab2 . '_' . $tab_alias_id;
         $crits = new Utils_RecordBrowser_Crits();
         foreach ($col2 as $col) {
             $col = $col[0] == ':' ? $col : Utils_RecordBrowserCommon::get_field_id(trim($col));
             if ($col) {
                 $crits->_or(new Utils_RecordBrowser_CritsSingle($col, $operator, $value, false, $raw_sql_val));
             }
         }
         if (!$crits->is_empty()) {
             $subquery = Utils_RecordBrowserCommon::build_query($tab2, $crits, $this->admin_mode, array(), $nested_tab_alias);
             $on_rule = $multiselect ? "{$field} LIKE CONCAT('%\\_\\_', {$nested_tab_alias}.id, '\\_\\_%')" : "{$field} = {$nested_tab_alias}.id";
             $sql = "EXISTS (SELECT 1 FROM {$subquery['sql']} AND {$on_rule})";
             $vals = $subquery['vals'];
         }
     } else {
         if ($raw_sql_val) {
             $sql = "{$field} {$operator} {$value}";
         } elseif (!$value) {
             $sql = "{$field} IS NULL";
             if (!$single_tab || $multiselect) {
                 $sql .= " OR {$field}=''";
             }
         } else {
             if ($single_tab && !$multiselect && $operator != DB::like()) {
                 $operand = '%d';
             } else {
                 if (DB::is_postgresql()) {
                     $field .= '::varchar';
                 }
                 $operand = '%s';
             }
             if ($multiselect) {
                 $value = "%\\_\\_{$value}\\_\\_%";
                 $operator = DB::like();
             }
             $sql = "({$field} {$operator} {$operand} AND {$field} IS NOT NULL)";
             $vals[] = $value;
         }
     }
     return array($sql, $vals);
 }
Esempio n. 5
0
 protected function validate_compound(Utils_RecordBrowser_Crits $crits, $record)
 {
     if ($crits->is_empty()) {
         return true;
     }
     $or = $crits->get_join_operator() == 'OR';
     $success = $or ? false : true;
     foreach ($crits->get_component_crits() as $c) {
         $satisfied = $this->validate($c, $record);
         if ($or) {
             if ($satisfied) {
                 $success = true;
                 break;
             }
         } else {
             if (!$satisfied) {
                 $success = false;
                 break;
             }
         }
     }
     if ($crits->get_negation()) {
         $success = !$success;
     }
     return $success;
 }
Esempio n. 6
0
 public function edit_permissions_rule($id = null, $clone = false)
 {
     if (Base_AdminCommon::get_access('Utils_RecordBrowser', 'permissions') != 2) {
         return false;
     }
     if ($this->is_back()) {
         return false;
     }
     load_js('modules/Utils/RecordBrowser/edit_permissions.js');
     $all_clearances = array('' => '---') + array_flip(Base_AclCommon::get_clearance(true));
     $all_fields = array();
     $this->init();
     foreach ($this->table_rows as $k => $v) {
         $all_fields[$v['id']] = $k;
     }
     $form = $this->init_module('Libs_QuickForm');
     $theme = $this->init_module('Base_Theme');
     $counts = array('clearance' => 5);
     $actions = $this->get_permission_actions();
     $form->addElement('select', 'action', __('Action'), $actions);
     $fields_permissions = $all_fields;
     for ($i = 0; $i < $counts['clearance']; $i++) {
         $form->addElement('select', 'clearance_' . $i, __('Clearance'), $all_clearances);
     }
     $defaults = array();
     foreach ($fields_permissions as $k => $v) {
         $defaults['field_' . $k] = 1;
         $form->addElement('checkbox', 'field_' . $k, _V($this->table_rows[$v]['name']));
     }
     $theme->assign('labels', array('and' => '<span class="joint">' . __('and') . '</span>', 'or' => '<span class="joint">' . __('or') . '</span>', 'caption' => $id ? __('Edit permission rule') : __('Add permission rule'), 'clearance' => __('Clearance requried'), 'fields' => __('Fields allowed'), 'crits' => __('Criteria required'), 'add_clearance' => __('Add clearance'), 'add_or' => __('Add criteria (or)'), 'add_and' => __('Add criteria (and)')));
     $current_clearance = 0;
     $crits = array();
     if ($id !== null && $this->tab != '__RECORDSETS__' && !preg_match('/,/', $this->tab)) {
         $row = DB::GetRow('SELECT * FROM ' . $this->tab . '_access AS acs WHERE id=%d', array($id));
         $defaults['action'] = $row['action'];
         $crits = Utils_RecordBrowserCommon::unserialize_crits($row['crits']);
         if (is_array($crits)) {
             $crits = Utils_RecordBrowser_Crits::from_array($crits);
         }
         $i = 0;
         $tmp = DB::GetAll('SELECT * FROM ' . $this->tab . '_access_clearance AS acs WHERE rule_id=%d', array($id));
         foreach ($tmp as $t) {
             $defaults['clearance_' . $i] = $t['clearance'];
             $i++;
         }
         $current_clearance += $i - 1;
         $tmp = DB::GetAll('SELECT * FROM ' . $this->tab . '_access_fields AS acs WHERE rule_id=%d', array($id));
         foreach ($tmp as $t) {
             unset($defaults['field_' . $t['block_field']]);
         }
     }
     $qbi = new Utils_RecordBrowser_QueryBuilderIntegration($this->tab);
     $qb = $qbi->get_builder_module($this, $crits);
     $qb->add_to_form($form, 'qb_crits', __('Crits'), 'qb_crits_editor');
     $form->setDefaults($defaults);
     if ($form->validate()) {
         $vals = $form->exportValues();
         $action = $vals['action'];
         $clearance = array();
         for ($i = 0; $i < $counts['clearance']; $i++) {
             if ($vals['clearance_' . $i]) {
                 $clearance[] = $vals['clearance_' . $i];
             }
         }
         $crits = $qbi->json_to_crits($vals['qb_crits']);
         $blocked_fields = array();
         foreach ($fields_permissions as $k => $v) {
             if (isset($vals['field_' . $k])) {
                 continue;
             }
             $blocked_fields[] = $k;
         }
         if ($id === null || $clone) {
             Utils_RecordBrowserCommon::add_access($this->tab, $action, $clearance, $crits, $blocked_fields);
         } else {
             Utils_RecordBrowserCommon::update_access($this->tab, $id, $action, $clearance, $crits, $blocked_fields);
         }
         return false;
     }
     eval_js('utils_recordbrowser__init_clearance(' . $current_clearance . ', ' . $counts['clearance'] . ')');
     eval_js('utils_recordbrowser__crits_initialized = true;');
     $form->assign_theme('form', $theme);
     $theme->assign('fields', $fields_permissions);
     $theme->assign('counts', $counts);
     $theme->display('edit_permissions');
     Base_ActionBarCommon::add('save', __('Save'), $form->get_submit_form_href());
     Base_ActionBarCommon::add('delete', __('Cancel'), $this->create_back_href());
     return true;
 }
Esempio n. 7
0
            $desc = DB::GetOne('SELECT name FROM crm_filters_group WHERE id=%d', array($prof));
        } elseif ($prof == 'my') {
            $ret = CRM_FiltersCommon::get_my_profile();
            $desc = __('My records');
        } else {
            //all and undefined
            $ret = '';
            /*$contacts = Utils_RecordBrowserCommon::get_records('contact', array(), array(), array('last_name'=>'ASC'));
            		$contacts_select = array();
            		foreach($contacts as $v)
            			$contacts_select[] = $v['id'];
            		if($contacts_select)
            			$ret = implode(',',$contacts_select);
            		else
            			$ret = '-1';*/
            $desc = __('All records');
        }
        //		$this->set_module_variable('profile',$ret);
        $_SESSION['client']['filter_' . Acl::get_user()]['value'] = $ret;
        $_SESSION['client']['filter_' . Acl::get_user()]['desc'] = $desc;
        location(array());
    }
    public static function crits_special_values()
    {
        $perspective = trim(CRM_FiltersCommon::get(), '()');
        $perspective = empty($perspective) ? null : explode(',', $perspective);
        return new Utils_RecordBrowser_ReplaceValue('__PERSPECTIVE__', __('Perspective'), $perspective, true);
    }
}
Utils_RecordBrowser_Crits::register_special_value_callback(array('CRM_FiltersCommon', 'crits_special_values'));