Skip to content

Commit

Permalink
Improved getting related comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rskrzypczak committed Jun 23, 2021
1 parent 234b44f commit 659246f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/QueryGenerator.php
Expand Up @@ -423,16 +423,21 @@ public function addRelatedField($field)
* Set source record.
*
* @param int $sourceRecord
*
* @return $this
*/
public function setSourceRecord($sourceRecord)
{
$this->sourceRecord = $sourceRecord;
return $this;
}

/**
* Appends a JOIN part to the query.
*
* @param array $join
*
* @return $this
*/
public function addJoin($join)
{
Expand Down
2 changes: 1 addition & 1 deletion config/version.php
@@ -1,7 +1,7 @@
<?php

return [
'appVersion' => '6.1.382',
'appVersion' => '6.1.383',
'patchVersion' => '2021.06.23',
'lib_roundcube' => '0.1.29',
];
42 changes: 41 additions & 1 deletion modules/ModComments/relations/GetRelatedRecord.php
Expand Up @@ -21,11 +21,51 @@ public function getRelationType(): int
return Vtiger_Relation_Model::RELATION_O2M;
}

/**
* Field custom list.
*
* @var array
*/
public $customFields = [
'children_count' => [
'label' => 'LBL_CHILDREN_COUNT',
'uitype' => 7
]
];

/**
* Field list.
*
* @return array
*/
public function getFields()
{
$fields = [];
$sourceModule = $this->relationModel->getParentModuleModel();
if ('Occurrences' !== $sourceModule->getName()) {
$sourceModule = $this->relationModel->getRelationModuleModel();
}
foreach ($this->customFields as $fieldName => $data) {
$field = new \Vtiger_Field_Model();
$field->set('name', $fieldName)->set('column', $fieldName)->set('table', 'vtiger_modcomments')->set('fromOutsideList', true)->setModule($sourceModule);
foreach ($data as $key => $value) {
$field->set($key, $value);
}
$fields[$fieldName] = $field;
}
return $fields;
}

/** {@inheritdoc} */
public function getQuery()
{
$parentId = $this->relationModel->get('parentRecord')->getId();
$queryGenerator = $this->relationModel->getQueryGenerator();
$queryGenerator->addCondition('related_to', $this->relationModel->get('parentRecord')->getId(), 'eid');
$queryGenerator->addCondition('related_to', $parentId, 'eid');
foreach (array_keys($this->customFields) as $fieldName) {
$subQuery = (new \App\QueryGenerator('ModComments'))->setFields(['id'])->setSourceRecord($parentId)->createQuery()->select((new \yii\db\Expression('COUNT(1)')))->andWhere(['parent_comments' => new yii\db\Expression('id')])->groupBy(['parent_comments']);
$queryGenerator->setCustomColumn([$fieldName => $subQuery]);
}
}

/** {@inheritdoc} */
Expand Down

0 comments on commit 659246f

Please sign in to comment.