Sort order list view by 'logged in user' in SuiteCRM - sugarcrm

Removing default sort order in list view and sorting by 'Logged in user' in SuiteCRM.

Add the following code in custom/modules/Prospects(your module)/views/view.list.php
function listViewProcess() {
global $current_user;
$user_name = $current_user->user_name;
$id = $current_user->id;
$this->processSearchForm();
$this->params['custom_order_by'] = ' ORDER BY FIELD(assigned_user_id, "'.$id.'") DESC';
$this->lv->setup($this->seed, 'include/ListView/ListViewGeneric.tpl', $this->where, $this->params);
$savedSearchName = empty($_REQUEST['saved_search_select_name']) ? '' : (' - ' . $_REQUEST['saved_search_select_name']);
echo $this->lv->display();
}
custom_order_by will be considered as second order by field
so declare
$ret_array['order_by']=''; in include/ListView/ListViewData.php
before
$main_query = $ret_array['select'] . $params['custom_select'] . $ret_array['from'] . $params['custom_from'] . $ret_array['inner_join']. $ret_array['where'] . $params['custom_where'] . $ret_array['order_by'] . $params['custom_order_by'];

Without customizing the code in include/listView/ListViewDate.php, just add the following code in custom/modules/(your module)/views/view.list.php
function listViewProcess() {
global $current_user;
$user_name = $current_user->user_name;
$id = $current_user->id;
$this->processSearchForm();
$this->params['overrideOrder']='1';
$this->params['orderBy']='1';
$this->params['custom_order_by'] = ' ORDER BY FIELD(accounts.assigned_user_id, "'.$id.'") DESC';
$this->lv->setup($this->seed, 'include/ListView/ListViewGeneric.tpl', $this->where, $this->params);
$savedSearchName = empty($_REQUEST['saved_search_select_name']) ? '' : (' - ' . $_REQUEST['saved_search_select_name']);
echo $this->lv->display();
}

Related

Laravel 8 Old Value in select

Im trying to get the selected category while validating inputs,
Please need help
in blade:
{{__("-- Please Select --")}}
category_id === $category->id)
$selected = 'selected';
printf("%s", $category->id, $selected, $prefix . ' ' . $category->name);
$traverse($category->children, $prefix . '-');
}
};
$traverse($tour_category);
?>
Thanks

Moodle Filters - GROUP BY

How can we get to work the moodle filters when GROUP BY - select form is used as filter mform field. I wasn't getting the desired output when I select the course. GROUP BY is not consider when sql_like function is called:
require_once($CFG->dirroot.'/filter_form.php');
$mform = new filter_form();
$coursefilter = '';
if ($formdata = $mform->get_data()) {
$coursefilter = $formdata->course;
}
$mform->set_data($formdata);
$mform->display();
$reporttable = new html_table();
$reporttable->head = array('course', 'users');
$reporttable->attributes['class'] = 'table';
$sql = "SELECT c.fullname, count(cc.userid) AS 'completed'
FROM {course_completions} cc JOIN {course} ON c.id = cc.course WHERE cc.timestarted > 0
GROUP BY c.fullname ";
$params = array();
if (!empty($coursefilter)) {
$params['fullname'] = '%' . $DB->sql_like_escape($coursefilter) . '%';
$sql .= " AND " . $DB->sql_like('c.fullname', ':fullname', false);
}
$mds = $DB->get_records_sql($sql, $params);
foreach ($mds as $m) {
$reporttable->data[] = new html_table_row(array(implode(array($m->fullname. $m->lastname)),
$m->completed ));
}
echo html_writer::table($reporttable);
Add the GROUP BY after the filter.
if (!empty($coursefilter)) {
$params['fullname'] = '%' . $DB->sql_like_escape($coursefilter) . '%';
$sql .= " AND " . $DB->sql_like('c.fullname', ':fullname', false);
}
$sql .= " GROUP BY c.fullname ";

PHP preg_replace supplement link URL pattern

I have the following URLs structure:
http://example.com/item/example-descriptions/6454986
http://example.com/item/example-bla-bla-bla/6545455
http://example.com/item/example-other-url/5454555
I need to add text to the numbers to get so (add "/demo/" and "?id=test")
http://example.com/item/example-descriptions/demo/6454986?id=test
http://example.com/item/example-bla-bla-bla/demo/6545455?id=test
http://example.com/item/example-other-url/demo/5454555?id=test
Here are a couple of ways imperfect:
$myurl = 'http://example.com/item/example-descriptions/6454986';
if (substr_count($myurl, 'example.com')){
$url = "$myurl.html";
$url = preg_replace('/^(.*)\/([^.]+)\.html$/','$1/demo/$2?id=test', $url);
echo "$url";
} else {
echo "$myurl";
}
and
$myurl = 'http://example.com/item/example-descriptions/6454986';
if (substr_count($myurl, 'example.com')){
$url = explode('/', $myurl);
echo "" . $url[0] . "/" . $url[1] . "/" . $url[2] . "/" . $url[3] . "/" . $url[4] . "/demo/" . $url[5] . "/?id=test";
} else {
echo "$myurl";
}
Help me improve the code.
You can use parse_url:
if (parse_url($myurl, PHP_URL_HOST) == 'example.com') {
$arr = explode('/', $myurl);
$arr[] = 'demo/' . array_pop($arr) . '?id=test';
$myurl = implode('/', $arr);
}

how to set a value for Zend_Form_Element_Submit (zendframework 1)

I have am having trouble setting the basic values of a zend form element submit button (Zendframework1). I basically want to set a unique id number in it and then retrieve this number once the button has been submitted.
Below is my code. you will nots that I tried to use the setValue() method but this did not work.
$new = new Zend_Form_Element_Submit('new');
$new
->setDecorators($this->_buttonDecorators)
->setValue($tieredPrice->sample_id)
->setLabel('New');
$this->addElement($new);
I would also appreciate any advice on what I use to receive the values. i.e what method I will call to retrieve the values?
The label is used as the value on submit buttons, and this is what is submitted. There isn't a way to have another value submitted as part of the button as well - you'd either need to change the submit name or value (= label).
What you probably want to do instead is add a hidden field to the form and give that your numeric value instead.
It's a little bit tricky but not impossible :
You need to implement your own view helper and use it with your element.
At first, you must add a custom view helper path :
How to add a view helper directory (zend framework)
Implement your helper :
class View_Helper_CustomSubmit extends Zend_View_Helper_FormSubmit
{
public function customSubmit($name, $value = null, $attribs = null)
{
if( array_key_exists( 'value', $attribs ) ) {
$value = $attribs['value'];
unset( $attribs['value'] );
}
$info = $this->_getInfo($name, $value, $attribs);
extract($info); // name, value, attribs, options, listsep, disable, id
// check if disabled
$disabled = '';
if ($disable) {
$disabled = ' disabled="disabled"';
}
if ($id) {
$id = ' id="' . $this->view->escape($id) . '"';
}
// XHTML or HTML end tag?
$endTag = ' />';
if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
$endTag= '>';
}
// Render the button.
$xhtml = '<input type="submit"'
. ' name="' . $this->view->escape($name) . '"'
. $id
. ' value="' . $this->view->escape( $value ) . '"'
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag;
return $xhtml;
}
}
So, you assign the helper to the element :
$submit = $form->createElement( 'submit', 'submitElementName' );
$submit->setAttrib( 'value', 'my value' );
$submit->helper = 'customSubmit';
$form->addELement( $submit );
This way, you can retrieve the value of the submitted form :
$form->getValue( 'submitElementName' );

Zend Framework Complex Where Statement

This method is published as offical example
->where("price < $minimumPrice OR price > $maximumPrice")
is such method safe?
want to write it as
->where("price < ? OR price > ?", $minimumPrice, $maximumPrice)
are there any poissibility?
and I can't split it into 2 where statements because plan to write query
->where("1 OR 2")
->where("3 OR 4")
Try this:
$query->where('(price < ?', $minPrice)
->orWhere('price > ?)', $maxPrice)
->where('some = ?', $some_other_variable);
will result:
where ((price < $minPrice) OR (price > $maxPrice)) AND (some = $some_other_variable)
Note the double (( )) in OR part
If I have complex WHERE clauses I use the db adapters' ->quoteInto() method like:
$where = '('
. $dbAdapter->quoteInto('price1 < ?', $price1)
. ' OR '
. $dbAdapter->quoteInto('price1 > ?', $price1)
. ')'
. ' AND '
. '('
. $dbAdapter->quoteInto('price2 < ?', $price2)
. ' OR '
. $dbAdapter->quoteInto('price2 > ?', $price2)
. ')'
;
$select->where($where);
Some times you will want to make SQL queries which have parenthesis around multiple where conditions that would be easily parsed with foreach, but you do not want to be bothered about string manipulation. For example, you would have a list of users with id's and that have to be of certain type, you can try this:
$select = $this->select();
$subWhere = $this->select();
foreach(array_keys($idArr) as $key => $value) {
$subWhere->orWhere('id=?', $value);
}
$select->where(implode(' ', $subWhere->getPart('WHERE')))->where('type=?', 'customer');
This will result in "SELECT * FROM table WHERE ((id=X) OR (id=Y) OR (id=Z)...) AND (type='customer');"
The idea developed a bit further, you could extend the Zend_Db_Table_Abstract:
public function subWhere($col, $binds, $operands, $andOr = 'OR' )
{
$subWhere = $this->select();
if(strtolower($andOr) == 'or') {
foreach($binds as $key => $value) {
$subWhere->orWhere($col.$operands[$key].'?', $value);
}
return implode(' ', $subWhere->getPart('WHERE'));
}
elseif (strtolower($andOr) == 'and') {
foreach ($binds as $key => $value) {
$subWhere->where($col.$operands[$key].'?', $value);
}
return implode(' ', $subWhere->getPart('WHERE'));
}
else {
return false;
}
}
And use it as:
$this->select()->where($this->subWhere($col, $binds, $operands));
Of course you should allow mixed $cols, $operands = array() defaulting as '=?' etc. but for the sake of simplicity I left that out. But I believe we should use native SQL functions like IN(), BETWEEN ... AND ..., NOT BETWEEN ... AND ...? Zend Framework doesn't make your life very easy though.
$select->where($db->quoteInto('field1 < ? OR', $minPrice)
. $db->quoteInto('field1 > ?', $maxPrice))
->where($db->quoteInto('field2 < ? OR', $value2)
. $db->quoteInto('field2 > ?', $value3));