Open Monograph Press  3.3.0
PKPContextQueryBuilder.inc.php
1 <?php
16 
17 use Illuminate\Database\Capsule\Manager as Capsule;
19 
21 
23  protected $db;
24 
26  protected $dbSettings;
27 
29  protected $dbIdColumn;
30 
32  protected $isEnabled = null;
33 
35  protected $userId;
36 
38  protected $searchPhrase = null;
39 
47  public function filterByIsEnabled($isEnabled) {
48  $this->isEnabled = $isEnabled;
49  return $this;
50  }
51 
63  public function filterByUserId($userId) {
64  $this->userId = $userId;
65  return $this;
66  }
67 
75  public function searchPhrase($phrase) {
76  $this->searchPhrase = $phrase;
77  return $this;
78  }
79 
83  public function getCount() {
84  return $this
85  ->getQuery()
86  ->select('c.' . $this->dbIdColumn)
87  ->get()
88  ->count();
89  }
90 
94  public function getIds() {
95  return $this
96  ->getQuery()
97  ->select('c.' . $this->dbIdColumn)
98  ->pluck('c.' . $this->dbIdColumn)
99  ->toArray();
100  }
101 
110  public function getManySummary() {
111  return $this
112  ->getQuery()
113  ->select([
114  'c.' . $this->dbIdColumn . ' as id',
115  'c.enabled',
116  'cst.setting_value as name',
117  'c.path as urlPath',
118  'c.seq',
119  ])
120  ->leftJoin($this->dbSettings . ' as cst', function($q) {
121  $q->where('cst.' . $this->dbIdColumn, '=', Capsule::raw('c.' . $this->dbIdColumn))
122  ->where('cst.setting_name', '=', 'name')
123  ->where('cst.locale', '=', Capsule::raw('c.primary_locale'));
124  })
125  ->groupBy([
126  'c.' . $this->dbIdColumn,
127  'cst.setting_value',
128  ])
129  ->get()
130  ->toArray();
131  }
132 
136  public function getQuery() {
137  $this->columns[] = 'c.*';
138  $q = Capsule::table($this->db . ' as c')
139  ->leftJoin($this->dbSettings . ' as cs', 'cs.' . $this->dbIdColumn, '=', 'c.' . $this->dbIdColumn)
140  ->groupBy('c.' . $this->dbIdColumn);
141 
142  if (!empty($this->isEnabled)) {
143  $q->where('c.enabled', '=', 1);
144  } elseif ($this->isEnabled === false) {
145  $q->where('c.enabled', '!=', 1);
146  }
147 
148  if (!empty($this->userId)) {
149  $q->leftJoin('user_groups as ug', 'ug.context_id', '=', 'c.' . $this->dbIdColumn)
150  ->leftJoin('user_user_groups as uug', 'uug.user_group_id', '=', 'ug.user_group_id')
151  ->where(function($q) {
152  $q->where('uug.user_id', '=', $this->userId)
153  ->where(function($q) {
154  $q->where('ug.role_id', '=', ROLE_ID_MANAGER)
155  ->orWhere('c.enabled', '=', 1);
156  });
157  });
158  }
159 
160  // search phrase
161  if (!empty($this->searchPhrase)) {
162  $words = explode(' ', $this->searchPhrase);
163  if (count($words)) {
164  foreach ($words as $word) {
165  $q->where(function($q) use ($word) {
166  $q->where(function($q) use ($word) {
167  $q->where('cs.setting_name', 'name');
168  $q->where('cs.setting_value', 'LIKE', "%{$word}%");
169  })
170  ->orWhere(function($q) use ($word) {
171  $q->where('cs.setting_name', 'description');
172  $q->where('cs.setting_value', 'LIKE', "%{$word}%");
173  })
174  ->orWhere(function($q) use ($word) {
175  $q->where('cs.setting_name', 'acronym');
176  $q->where('cs.setting_value', 'LIKE', "%{$word}%");
177  })
178  ->orWhere(function($q) use ($word) {
179  $q->where('cs.setting_name', 'abbreviation');
180  $q->where('cs.setting_value', 'LIKE', "%{$word}%");
181  });
182  });
183  }
184  }
185  }
186 
187  // Add app-specific query statements
188  \HookRegistry::call('Context::getContexts::queryObject', array(&$q, $this));
189 
190  $q->select($this->columns);
191 
192  return $q;
193  }
194 }
PKP\Services\QueryBuilders
PKP\Services\QueryBuilders\PKPContextQueryBuilder\searchPhrase
searchPhrase($phrase)
Definition: PKPContextQueryBuilder.inc.php:93
PKP\Services\QueryBuilders\PKPContextQueryBuilder\getIds
getIds()
Definition: PKPContextQueryBuilder.inc.php:112
PKP\Services\QueryBuilders\PKPContextQueryBuilder\$dbIdColumn
$dbIdColumn
Definition: PKPContextQueryBuilder.inc.php:38
PKP\Services\QueryBuilders\PKPContextQueryBuilder
Definition: PKPContextQueryBuilder.inc.php:20
PKP\Services\QueryBuilders\PKPContextQueryBuilder\$isEnabled
$isEnabled
Definition: PKPContextQueryBuilder.inc.php:44
PKP\Services\QueryBuilders\PKPContextQueryBuilder\$dbSettings
$dbSettings
Definition: PKPContextQueryBuilder.inc.php:32
PKP\Services\QueryBuilders\PKPContextQueryBuilder\getQuery
getQuery()
Definition: PKPContextQueryBuilder.inc.php:154
PKP\Services\QueryBuilders\PKPContextQueryBuilder\filterByIsEnabled
filterByIsEnabled($isEnabled)
Definition: PKPContextQueryBuilder.inc.php:65
PKP\Services\QueryBuilders\PKPContextQueryBuilder\filterByUserId
filterByUserId($userId)
Definition: PKPContextQueryBuilder.inc.php:81
PKP\Services\QueryBuilders\PKPContextQueryBuilder\$searchPhrase
$searchPhrase
Definition: PKPContextQueryBuilder.inc.php:56
PKP\Services\QueryBuilders\PKPContextQueryBuilder\getManySummary
getManySummary()
Definition: PKPContextQueryBuilder.inc.php:128
PKP\Services\QueryBuilders\PKPContextQueryBuilder\$userId
$userId
Definition: PKPContextQueryBuilder.inc.php:50
PKP\Services\QueryBuilders\PKPContextQueryBuilder\$db
$db
Definition: PKPContextQueryBuilder.inc.php:26
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
PKP\Services\QueryBuilders\PKPContextQueryBuilder\getCount
getCount()
Definition: PKPContextQueryBuilder.inc.php:101
PKP\Services\QueryBuilders\Interfaces\EntityQueryBuilderInterface
Definition: EntityQueryBuilderInterface.inc.php:19