Newer
Older
* Function which will give the basic query to find duplicates
* @param <String> $module
* @param <String> $tableColumns
* @param <String> $selectedColumns
* @param <Boolean> $ignoreEmpty

Prasad
committed
* @param <Array> $columnTypes

Prasad
committed
function getQueryForDuplicates($module, $tableColumns, $selectedColumns = '', $ignoreEmpty = false,$requiredTables = array(),$columnTypes = null) {
if(is_array($tableColumns)) {
$tableColumnsString = implode(',', $tableColumns);
}
$selectClause = "SELECT " . $this->table_name . "." . $this->table_index . " AS recordid," . $tableColumnsString;
// Select Custom Field Table Columns if present
if (isset($this->customFieldTable))
$query .= ", " . $this->customFieldTable[0] . ".* ";
$fromClause .= " INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = $this->table_name.$this->table_index";
if($this->tab_name) {
foreach($this->tab_name as $tableName) {
if($tableName != 'vtiger_crmentity' && $tableName != $this->table_name && $tableName != 'vtiger_inventoryproductrel' && in_array($tableName,$requiredTables)) {
if($this->tab_name_index[$tableName]) {
$fromClause .= " INNER JOIN " . $tableName . " ON " . $tableName . '.' . $this->tab_name_index[$tableName] .
" = $this->table_name.$this->table_index";
}
}
}
}
$whereClause = " WHERE vtiger_crmentity.deleted = 0";
$whereClause .= $this->getListViewSecurityParameter($module);
if($ignoreEmpty) {
foreach($tableColumns as $tableColumn){

Prasad
committed
if ($columnTypes && ($columnTypes[$tableColumn] == "date" || $columnTypes[$tableColumn] == "datetime")) {
$whereClause .= " AND ($tableColumn IS NOT NULL) ";
} else {
$whereClause .= " AND ($tableColumn IS NOT NULL AND $tableColumn != '') ";
}
if (isset($selectedColumns) && trim($selectedColumns) != '') {
$sub_query = "SELECT $selectedColumns FROM $this->table_name AS t " .
" INNER JOIN vtiger_crmentity AS crm ON crm.crmid = t." . $this->table_index;
// Consider custom table join as well.
if (isset($this->customFieldTable)) {
$sub_query .= " LEFT JOIN " . $this->customFieldTable[0] . " tcf ON tcf." . $this->customFieldTable[1] . " = t.$this->table_index";
}
$sub_query .= " WHERE crm.deleted=0 GROUP BY $selectedColumns HAVING COUNT(*)>1";
} else {
$sub_query = "SELECT $tableColumnsString $fromClause $whereClause GROUP BY $tableColumnsString HAVING COUNT(*)>1";
}
$i = 1;
foreach($tableColumns as $tableColumn){
$tableInfo = explode('.', $tableColumn);
$duplicateCheckClause .= " ifnull($tableColumn,'null') = ifnull(temp.$tableInfo[1],'null')";
if (php7_count($tableColumns) != $i++) $duplicateCheckClause .= " AND ";
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
$query = $selectClause . $fromClause .
" LEFT JOIN vtiger_users_last_import ON vtiger_users_last_import.bean_id=" . $this->table_name . "." . $this->table_index .
" INNER JOIN (" . $sub_query . ") AS temp ON " . $duplicateCheckClause .
$whereClause .
" ORDER BY $tableColumnsString," . $this->table_name . "." . $this->table_index . " ASC";
return $query;
}
/**
* Function to get relation query for get_activities
*/
function get_activities($id, $cur_tab_id, $rel_tab_id, $actions=false) {
global $currentModule;
$this_module = $currentModule;
$related_module = vtlib_getModuleNameById($rel_tab_id);
$userNameSql = getSqlForNameInDisplayFormat(array('first_name' => 'vtiger_users.first_name', 'last_name' => 'vtiger_users.last_name'), 'Users');
$query = "SELECT CASE WHEN (vtiger_users.user_name not like '') THEN $userNameSql ELSE vtiger_groups.groupname END AS user_name,
vtiger_crmentity.*, vtiger_activity.activitytype, vtiger_activity.subject, vtiger_activity.date_start, vtiger_activity.time_start,
vtiger_activity.recurringtype, vtiger_activity.due_date, vtiger_activity.time_end, vtiger_activity.visibility, vtiger_seactivityrel.crmid AS parent_id,
CASE WHEN (vtiger_activity.activitytype = 'Task') THEN (vtiger_activity.status) ELSE (vtiger_activity.eventstatus) END AS status
FROM vtiger_activity
INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_activity.activityid
LEFT JOIN vtiger_seactivityrel ON vtiger_seactivityrel.activityid = vtiger_activity.activityid
LEFT JOIN vtiger_cntactivityrel ON vtiger_cntactivityrel.activityid = vtiger_activity.activityid
LEFT JOIN vtiger_users ON vtiger_users.id = vtiger_crmentity.smownerid
LEFT JOIN vtiger_groups ON vtiger_groups.groupid = vtiger_crmentity.smownerid
WHERE vtiger_crmentity.deleted = 0 AND vtiger_activity.activitytype <> 'Emails'
AND vtiger_seactivityrel.crmid = ".$id;
$return_value = GetRelatedList($this_module, $related_module, '', $query, '', '');
if($return_value == null) $return_value = Array();
return $return_value;
}
function get_comments($relatedRecordId = false) {
$current_user = vglobal('current_user');
$moduleName = $this->moduleName;
if($moduleName != 'ModComments') {
return false;
}
$queryGenerator = new EnhancedQueryGenerator($moduleName, $current_user);
if(is_object($this->column_fields)) {
$fields = $this->column_fields->getColumnFieldNames();
} else if(is_array($this->column_fields)) {
$fields = array_keys($this->column_fields);
}
array_push($fields, 'id');
$queryGenerator->setFields($fields);
$query = $queryGenerator->getQuery();
if($relatedRecordId){
$query .= " AND related_to = ".$relatedRecordId." ORDER BY vtiger_crmentity.createdtime DESC";
}
return $query;
}
class TrackableObject implements ArrayAccess, IteratorAggregate {
private $storage;
private $trackingEnabled = true;
function __construct($value = array()) {
$this->storage = $value;
}
#[\ReturnTypeWillChange]
return isset($this->storage[$key]) || array_key_exists($key, $this->storage);
#[\ReturnTypeWillChange]
if(is_array($value)) $value = empty($value) ? "" : (array_key_exists(0, $value) ? $value[0] : ""); //it is an associative (if array without a key 0) modified to prevent warning of Undefined array key 0 .
if($this->tracking && $this->trackingEnabled) {
$olderValue = $this->offsetGet($key);
// decode_html only expects string
$olderValue = is_string($olderValue) ? decode_html($olderValue) : $olderValue ;
//same logic is used in vtEntityDelta to check for delta
if((empty($olderValue) && !empty($value)) || ($olderValue !== $value)) {
$this->changed[] = $key;
}
}
$this->storage[$key] = $value;
}
#[\ReturnTypeWillChange]
public function offsetUnset($key) {
unset($this->storage[$key]);
}
#[\ReturnTypeWillChange]
return isset($this->storage[$key]) || array_key_exists($key, $this->storage) ? $this->storage[$key] : null;
#[\ReturnTypeWillChange]
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
public function getIterator() {
$iterator = new ArrayObject($this->storage);
return $iterator->getIterator();
}
function getChanged() {
return $this->changed;
}
function startTracking() {
if($this->tracking && $this->trackingEnabled) return;
$this->tracking = true;
$this->changed = array();
}
function restartTracking() {
$this->tracking = true;
$this->startTracking();
}
function pauseTracking() {
$this->tracking = false;
}
function resumeTracking() {
if($this->trackingEnabled)
$this->tracking = true;
}
function getColumnFields() {
return $this->storage;
}
function getColumnFieldNames(){
return array_keys($this->storage);
}