protected function validJoinHelper($modelClassName, $attributeName, $relatedAttributeName)
 {
     $searchAttributeData = array();
     $searchAttributeData['clauses'] = array(1 => array('attributeName' => $attributeName, 'relatedAttributeName' => $relatedAttributeName, 'operatorType' => OperatorRules::TYPE_IS_NOT_NULL, 'value' => null));
     $searchAttributeData['structure'] = '1';
     $searchAttributeDataAndClassNames = array(array($modelClassName => $searchAttributeData));
     $sql = RedBeanModelsDataProvider::makeUnionSql($searchAttributeDataAndClassNames, null, true);
     $result = ZurmoRedBean::GetAll($sql);
     $this->assertTrue(is_array($result));
 }
 public function testUnionQuery()
 {
     //regular user no elevation
     $user = new User();
     $user->username = '******';
     $user->firstName = 'aaa';
     $user->lastName = 'aaa';
     $saved = $user->save();
     $this->assertTrue($saved);
     $aUser = User::getByUsername('aaa');
     Yii::app()->user->userModel = $aUser;
     $mungeIds = AllPermissionsOptimizationUtil::getMungeIdsByUser($aUser);
     $model = new OwnedSecurableTestItem();
     $model->member = 'test4';
     $model->owner = $aUser;
     $this->assertTrue($model->save());
     Yii::app()->user->userModel = User::getByUsername('super');
     $this->assertEquals(4, OwnedSecurableTestItem::getCount());
     $model = new OwnedSecurableTestItem2();
     $model->member = 'test4';
     $model->owner = $aUser;
     $this->assertTrue($model->save());
     $this->assertEquals(2, OwnedSecurableTestItem2::getCount());
     Yii::app()->user->userModel = User::getByUsername('aaa');
     $quote = DatabaseCompatibilityUtil::getQuote();
     //Test search attribute data across multiple models.
     $aFakePost = array('member' => 'test4');
     $aMetadataAdapter = new SearchDataProviderMetadataAdapter(new OwnedSecurableTestItem(false), $aUser->id, $aFakePost);
     $bFakePost = array('member' => 'test4');
     $bMetadataAdapter = new SearchDataProviderMetadataAdapter(new OwnedSecurableTestItem2(false), $aUser->id, $bFakePost);
     $modelClassNamesAndSearchAttributeData = array(array('OwnedSecurableTestItem' => $aMetadataAdapter->getAdaptedMetadata()), array('OwnedSecurableTestItem2' => $bMetadataAdapter->getAdaptedMetadata()));
     $unionSql = RedBeanModelsDataProvider::makeUnionSql($modelClassNamesAndSearchAttributeData, null, false, 2, 7);
     $compareWhere = "({$quote}ownedsecurabletestitem{$quote}.{$quote}member{$quote} like 'test4%')";
     $compareWhere2 = "({$quote}ownedsecurabletestitem2{$quote}.{$quote}member{$quote} like 'test4%')";
     $compareSubsetSql = "(";
     $compareSubsetSql .= "select distinct {$quote}ownedsecurabletestitem{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'OwnedSecurableTestItem' modelClassName ";
     $compareSubsetSql .= "from ({$quote}ownedsecurabletestitem{$quote}, {$quote}ownedsecurableitem{$quote}) ";
     $compareSubsetSql .= "left join {$quote}ownedsecurabletestitem_read{$quote} on ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem_read{$quote}.{$quote}securableitem_id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurableitem{$quote}.{$quote}securableitem_id{$quote} ";
     $compareSubsetSql .= "and {$quote}munge_id{$quote} in ('" . join("', '", $mungeIds) . "') ";
     $compareSubsetSql .= "where ({$compareWhere}) and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = {$aUser->id} ";
     $compareSubsetSql .= "OR {$quote}ownedsecurabletestitem_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) ";
     // Not Coding Standard
     $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem{$quote}.{$quote}ownedsecurableitem_id{$quote}";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "UNION (";
     $compareSubsetSql .= "select distinct {$quote}ownedsecurabletestitem2{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'OwnedSecurableTestItem2' modelClassName ";
     $compareSubsetSql .= "from ({$quote}ownedsecurabletestitem2{$quote}, {$quote}ownedsecurableitem{$quote}) ";
     $compareSubsetSql .= "left join {$quote}ownedsecurabletestitem2_read{$quote} on ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem2_read{$quote}.{$quote}securableitem_id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurableitem{$quote}.{$quote}securableitem_id{$quote} ";
     $compareSubsetSql .= "and {$quote}munge_id{$quote} in ('" . join("', '", $mungeIds) . "') ";
     $compareSubsetSql .= "where ({$compareWhere2}) and ({$quote}ownedsecurableitem{$quote}.{$quote}owner__user_id{$quote} = {$aUser->id} ";
     $compareSubsetSql .= "OR {$quote}ownedsecurabletestitem2_read{$quote}.{$quote}munge_id{$quote} IS NOT NULL) ";
     // Not Coding Standard
     $compareSubsetSql .= "and {$quote}ownedsecurableitem{$quote}.{$quote}id{$quote} = ";
     $compareSubsetSql .= "{$quote}ownedsecurabletestitem2{$quote}.{$quote}ownedsecurableitem_id{$quote}";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= 'limit 7 offset 2';
     $this->assertEquals($compareSubsetSql, $unionSql);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelsDataProvider('anId', null, false, $modelClassNamesAndSearchAttributeData);
     $data = $dataProvider->getData();
     //Test results are correct
     $this->assertEquals(2, count($data));
     $this->assertEquals('OwnedSecurableTestItem', get_class($data[0]));
     $this->assertEquals('OwnedSecurableTestItem2', get_class($data[1]));
     //Make sure union count query produces the same count.
     $this->assertEquals(2, $dataProvider->calculateTotalItemCount());
 }
 /**
  * Given a partial term, search across modules that support global search.
  * @param $partialTerm
  * @param $pageSize
  * @param User $user
  * @param null $scopeData
  * @return array
  */
 public static function getGlobalSearchResultsByPartialTerm($partialTerm, $pageSize, User $user, $scopeData = null)
 {
     assert('is_string($partialTerm)');
     assert('is_int($pageSize)');
     assert('$user->id > 0');
     assert('$scopeData == null || is_array($scopeData)');
     $modelClassNamesAndSearchAttributeData = static::makeModelClassNamesAndSearchAttributeData($partialTerm, $user, $scopeData);
     if (empty($modelClassNamesAndSearchAttributeData)) {
         return array(static::makeNoResultsFoundResultsData());
     }
     $dataProvider = new RedBeanModelsDataProvider('anId', null, false, $modelClassNamesAndSearchAttributeData, array('pagination' => array('pageSize' => $pageSize)));
     $data = $dataProvider->getData();
     if (empty($data)) {
         return array(static::makeNoResultsFoundResultsData());
     }
     $autoCompleteResults = array();
     foreach ($data as $model) {
         $autoCompleteResults[] = static::makeModelResultsData($model);
     }
     return $autoCompleteResults;
 }
 /**
  * @depends testUnionSqlAcrossMultipleModelsUsingDifferentOrderBys
  */
 public function testUnionSqlAcrossMultipleModelsUsingOneOfFilterWhichMakesSelectDistinct()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     $searchAttributeDataForJ['clauses'][1] = array('attributeName' => 'jMember', 'operatorType' => 'oneOf', 'value' => array('a', 'b', 'c'));
     $searchAttributeDataForJ['structure'] = '1';
     $searchAttributeDataForI['clauses'][1] = array('attributeName' => 'ks', 'relatedAttributeName' => 'kMember', 'operatorType' => 'oneOf', 'value' => array('d', "e'd", 'f'));
     $searchAttributeDataForI['structure'] = '1';
     $modelClassNamesAndSearchAttributeData = array(array('I' => $searchAttributeDataForI), array('J' => $searchAttributeDataForJ));
     $modelClassNamesAndSortAttributes = array('I' => 'name', 'J' => 'jMember');
     $unionSql = RedBeanModelsDataProvider::makeUnionSql($modelClassNamesAndSearchAttributeData, $modelClassNamesAndSortAttributes, true, 2, 7);
     $compareSubsetSql = "(";
     $compareSubsetSql .= "select distinct {$quote}i{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'I' modelClassName, {$quote}h{$quote}.{$quote}name{$quote} orderByColumn ";
     $compareSubsetSql .= "from ({$quote}i{$quote}, {$quote}h{$quote}) ";
     $compareSubsetSql .= "left join {$quote}k{$quote} on {$quote}k{$quote}.{$quote}i_id{$quote} = ";
     $compareSubsetSql .= "{$quote}i{$quote}.{$quote}id{$quote} ";
     $compareSubsetSql .= "where ({$quote}k{$quote}.{$quote}kmember{$quote} IN('d','e\\'d','f'))";
     // Not Coding Standard
     $compareSubsetSql .= " and {$quote}h{$quote}.{$quote}id{$quote} = {$quote}i{$quote}.{$quote}h_id{$quote}";
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "UNION (";
     $compareSubsetSql .= "select {$quote}j{$quote}.{$quote}id{$quote} id";
     $compareSubsetSql .= ", 'J' modelClassName, {$quote}j{$quote}.{$quote}jmember{$quote} orderByColumn ";
     $compareSubsetSql .= "from {$quote}j{$quote} ";
     $compareSubsetSql .= "where ({$quote}j{$quote}.{$quote}jmember{$quote} IN('a','b','c'))";
     // Not Coding Standard
     $compareSubsetSql .= ") ";
     $compareSubsetSql .= "order by orderByColumn desc ";
     $compareSubsetSql .= 'limit 7 offset 2';
     $this->assertEquals($compareSubsetSql, $unionSql);
     //Make sure the sql runs properly.
     $dataProvider = new RedBeanModelsDataProvider('anId', $modelClassNamesAndSortAttributes, true, $modelClassNamesAndSearchAttributeData);
     $data = $dataProvider->getData();
 }
 /**
  * Given a partial term, search across modules that support global search.
  * @param string  $partialTerm
  * @param integer $pageSize
  * @param User    $user
  */
 public static function getGlobalSearchResultsByPartialTerm($partialTerm, $pageSize, User $user, $scopeData = null)
 {
     assert('is_string($partialTerm)');
     assert('is_int($pageSize)');
     assert('$user->id > 0');
     assert('$scopeData == null || is_array($scopeData)');
     $modelClassNamesAndSearchAttributeData = self::makeModelClassNamesAndSearchAttributeData($partialTerm, $user, $scopeData);
     if (empty($modelClassNamesAndSearchAttributeData)) {
         return array(array('href' => '', 'label' => Zurmo::t('Core', 'No results found'), 'iconClass' => ''));
     }
     $dataProvider = new RedBeanModelsDataProvider('anId', null, false, $modelClassNamesAndSearchAttributeData);
     $data = $dataProvider->getData();
     if (empty($data)) {
         return array(array('href' => '', 'label' => Zurmo::t('Core', 'No results found'), 'iconClass' => ''));
     }
     $autoCompleteResults = array();
     foreach ($data as $model) {
         $moduleClassName = ModelStateUtil::resolveModuleClassNameByStateOfModel($model);
         $moduleLabel = $moduleClassName::getModuleLabelByTypeAndLanguage('Singular');
         $route = Yii::app()->createUrl($moduleClassName::getDirectoryName() . '/default/details/', array('id' => $model->id));
         $autoCompleteResults[] = array('href' => $route, 'label' => strval($model), 'iconClass' => 'autocomplete-icon-' . $moduleClassName);
     }
     return $autoCompleteResults;
 }