function fetchClassListByGroups( $groupFilter, $groupFilterType = 'include' )
    {
        $notIn = ( $groupFilterType == 'exclude' );

        if ( is_array( $groupFilter ) && count( $groupFilter ) > 0 )
        {
            $db = eZDB::instance();
            $groupFilter = $db->generateSQLINStatement( $groupFilter, 'ccg.group_id', $notIn );

            $classNameFilter = eZContentClassName::sqlFilter( 'cc' );
            $version = eZContentClass::VERSION_STATUS_DEFINED;

            $sql = "SELECT DISTINCT cc.*, $classNameFilter[nameField] " .
                   "FROM ezcontentclass cc, ezcontentclass_classgroup ccg, $classNameFilter[from] " .
                   "WHERE cc.version = $version" .
                   "      AND cc.id = ccg.contentclass_id" .
                   "      AND $groupFilter" .
                   "      AND $classNameFilter[where] " .
                   "ORDER BY $classNameFilter[nameField] ASC";

            $rows = $db->arrayQuery( $sql );
            $classes = eZPersistentObject::handleRows( $rows, 'eZContentClass', true );
        }
        else
        {
            $classes = eZContentClass::fetchList( eZContentClass::VERSION_STATUS_DEFINED, true, false, array( 'name' => 'asc' ) );
        }

        return array( 'result' => $classes );
    }
 static function sqlAppendFilter($languageTable = 'ezcontentclass')
 {
     $def = eZContentClassName::definition();
     $languageListTable = $def['name'];
     $sqlFilter = array('nameField' => ", {$languageListTable}.name", 'from' => ", {$languageListTable}", 'where' => "AND {$languageTable}.id = {$languageListTable}.contentclass_id AND\n                                        {$languageTable}.version = {$languageListTable}.contentclass_version AND " . eZContentLanguage::sqlFilter($languageListTable, $languageTable), 'orderBy' => "{$languageListTable}.name");
     return $sqlFilter;
 }
 static function fetchClassList($contentclass_version, $group_id, $asObject = true, $orderByArray = array('name'))
 {
     $versionCond = '';
     $orderByClause = '';
     $group_id = (int) $group_id;
     $classNameSqlFilter = eZContentClassName::sqlEmptyFilter();
     if ($contentclass_version !== null) {
         $contentclass_version = (int) $contentclass_version;
         $versionCond = "AND class_group.contentclass_version='{$contentclass_version}'\n                            AND contentclass.version='{$contentclass_version}'\n";
     }
     if ($orderByArray) {
         foreach (array_keys($orderByArray) as $key) {
             if (strcasecmp($orderByArray[$key], 'name') === 0) {
                 $classNameSqlFilter = eZContentClassName::sqlAppendFilter('contentclass');
                 $orderByArray[$key] = $classNameSqlFilter['orderBy'];
             }
         }
         $orderByClause = 'ORDER BY ' . implode(', ', $orderByArray);
     }
     $db = eZDB::instance();
     $sql = "SELECT contentclass.* {$classNameSqlFilter['nameField']}\n                FROM ezcontentclass  contentclass, ezcontentclass_classgroup class_group {$classNameSqlFilter['from']}\n                WHERE contentclass.id=class_group.contentclass_id\n                {$versionCond}\n                AND class_group.group_id='{$group_id}' {$classNameSqlFilter['where']}\n                {$orderByClause}";
     $rows = $db->arrayQuery($sql);
     return eZPersistentObject::handleRows($rows, "eZContentClass", $asObject);
 }
 static function fetchList($version = eZContentClass::VERSION_STATUS_DEFINED, $asObject = true, $user_id = false, $sorts = null, $fields = null, $classFilter = false, $limit = null)
 {
     $conds = array();
     $custom_fields = null;
     $custom_tables = null;
     $custom_conds = null;
     if (is_numeric($version)) {
         $conds["version"] = $version;
     }
     if ($user_id !== false and is_numeric($user_id)) {
         $conds["creator_id"] = $user_id;
     }
     if ($classFilter) {
         $classIDCount = 0;
         $classIdentifierCount = 0;
         $classIDFilter = array();
         $classIdentifierFilter = array();
         foreach ($classFilter as $classType) {
             if (is_numeric($classType)) {
                 $classIDFilter[] = $classType;
                 $classIDCount++;
             } else {
                 $classIdentifierFilter[] = $classType;
                 $classIdentifierCount++;
             }
         }
         if ($classIDCount > 1) {
             $conds['id'] = array($classIDFilter);
         } else {
             if ($classIDCount == 1) {
                 $conds['id'] = $classIDFilter[0];
             }
         }
         if ($classIdentifierCount > 1) {
             $conds['identifier'] = array($classIdentifierFilter);
         } else {
             if ($classIdentifierCount == 1) {
                 $conds['identifier'] = $classIdentifierFilter[0];
             }
         }
     }
     if ($sorts && isset($sorts['name'])) {
         $nameFiler = eZContentClassName::sqlFilter('ezcontentclass');
         $custom_tables = array($nameFiler['from']);
         $custom_conds = "AND " . $nameFiler['where'];
         $custom_fields = array($nameFiler['nameField']);
         $sorts[$nameFiler['orderBy']] = $sorts['name'];
         unset($sorts['name']);
     }
     return eZPersistentObject::fetchObjectList(eZContentClass::definition(), $fields, $conds, $sorts, $limit, $asObject, false, $custom_fields, $custom_tables, $custom_conds);
 }
 function buildSortSQL($sortArray)
 {
     $sortCount = 0;
     $sortList = false;
     if (isset($sortArray) and is_array($sortArray) and count($sortArray) > 0) {
         $sortList = $sortArray;
         if (count($sortList) > 1 and !is_array($sortList[0])) {
             $sortList = array($sortList);
         }
     }
     $attributeJoinCount = 0;
     $attributeFromSQL = "";
     $attributeWereSQL = "";
     $selectSQL = '';
     if ($sortList !== false) {
         $sortingFields = '';
         foreach ($sortList as $sortBy) {
             if (is_array($sortBy) and count($sortBy) > 0) {
                 if ($sortCount > 0) {
                     $sortingFields .= ', ';
                 }
                 $sortField = $sortBy[0];
                 switch ($sortField) {
                     case 'path':
                         $sortingFields .= 'path_string';
                         break;
                     case 'published':
                         $sortingFields .= 'ezcontentobject.published';
                         break;
                     case 'modified':
                         $sortingFields .= 'ezcontentobject.modified';
                         break;
                     case 'section':
                         $sortingFields .= 'ezcontentobject.section_id';
                         break;
                     case 'depth':
                         $sortingFields .= 'depth';
                         break;
                     case 'class_identifier':
                         $sortingFields .= 'ezcontentclass.identifier';
                         $selectSQL .= ', ezcontentclass.identifier';
                         break;
                     case 'class_name':
                         $classNameFilter = eZContentClassName::sqlFilter();
                         $selectSQL .= ", " . $classNameFilter['nameField'] . " AS class_name";
                         $sortingFields .= "class_name";
                         $attributeFromSQL .= " INNER JOIN {$classNameFilter['from']} ON ({$classNameFilter['where']})";
                         break;
                     case 'priority':
                         $sortingFields .= 'ezcontentobject_tree.priority';
                         break;
                     case 'name':
                         $sortingFields .= 'ezcontentobject_name.name';
                         break;
                     case 'attribute':
                         $sortClassID = $sortBy[2];
                         // Look up datatype for sorting
                         if (!is_numeric($sortClassID)) {
                             $sortClassID = eZContentObjectTreeNode::classAttributeIDByIdentifier($sortClassID);
                         }
                         $sortDataType = $sortClassID === false ? false : eZContentObjectTreeNode::sortKeyByClassAttributeID($sortClassID);
                         $sortKey = false;
                         if ($sortDataType == 'string') {
                             $sortKey = 'sort_key_string';
                         } else {
                             $sortKey = 'sort_key_int';
                         }
                         $sortingFields .= "a{$attributeJoinCount}.{$sortKey}";
                         $attributeFromSQL .= " INNER JOIN ezcontentobject_attribute as a{$attributeJoinCount} ON (a{$attributeJoinCount}.contentobject_id = ezcontentobject.id AND a{$attributeJoinCount}.version = ezcontentobject_name.content_version)";
                         $attributeWereSQL .= " AND a{$attributeJoinCount}.contentclassattribute_id = {$sortClassID}";
                         $selectSQL .= ", a{$attributeJoinCount}.{$sortKey}";
                         $attributeJoinCount++;
                         break;
                     default:
                         eZDebug::writeWarning('Unknown sort field: ' . $sortField, __METHOD__);
                         continue;
                 }
                 $sortOrder = true;
                 // true is ascending
                 if (isset($sortBy[1])) {
                     $sortOrder = $sortBy[1];
                 }
                 $sortingFields .= $sortOrder ? " ASC" : " DESC";
                 ++$sortCount;
             }
         }
     }
     // Should we sort?
     if ($sortCount == 0) {
         $sortingFields = " ezcontentobject.published ASC";
     }
     return array('sortingFields' => $sortingFields, 'selectSQL' => $selectSQL, 'fromSQL' => $attributeFromSQL, 'whereSQL' => $attributeWereSQL);
 }
 static function remove($contentClass)
 {
     eZContentClassName::removeClassName($contentClass->attribute('id'), $contentClass->attribute('version'));
 }
 function canCreateClassList($asObject = false, $includeFilter = true, $groupList = false, $fetchID = false)
 {
     $ini = eZINI::instance();
     $groupArray = array();
     $languageCodeList = eZContentLanguage::fetchLocaleList();
     $allowedLanguages = array('*' => array());
     $user = eZUser::currentUser();
     $accessResult = $user->hasAccessTo('content', 'create');
     $accessWord = $accessResult['accessWord'];
     $classIDArray = array();
     $classList = array();
     $fetchAll = false;
     if ($accessWord == 'yes') {
         $fetchAll = true;
         $allowedLanguages['*'] = $languageCodeList;
     } else {
         if ($accessWord == 'no') {
             // Cannot create any objects, return empty list.
             return $classList;
         } else {
             foreach ($accessResult['policies'] as $policy) {
                 $policyArray = $this->classListFromPolicy($policy, $languageCodeList);
                 if (empty($policyArray)) {
                     continue;
                 }
                 // Wildcard on all classes
                 if ($policyArray['classes'] == '*') {
                     $fetchAll = true;
                     $allowedLanguages['*'] = array_unique(array_merge($allowedLanguages['*'], $policyArray['language_codes']));
                     // we remove individual class ids that are overriden in all languages by the wildcard (#EZP-20933)
                     foreach ($allowedLanguages as $classId => $classLanguageCodes) {
                         if ($classId == '*') {
                             continue;
                         }
                         if (!count(array_diff($classLanguageCodes, $allowedLanguages['*']))) {
                             unset($allowedLanguages[$classId]);
                         }
                     }
                 } else {
                     if (is_array($policyArray['classes']) && $this->hasCurrentSubtreeLimitation($policy)) {
                         foreach ($policyArray['classes'] as $class) {
                             if (isset($allowedLanguages[$class])) {
                                 $allowedLanguages[$class] = array_unique(array_merge($allowedLanguages[$class], $policyArray['language_codes']));
                             } else {
                                 // we don't add class identifiers that are already covered by the 'all classes' in a language
                                 if (!empty($allowedLanguages['*'])) {
                                     if (!count(array_diff($policyArray['language_codes'], $allowedLanguages['*']))) {
                                         continue;
                                     }
                                 }
                                 $allowedLanguages[$class] = $policyArray['language_codes'];
                             }
                         }
                         $classIDArray = array_merge($classIDArray, array_diff($policyArray['classes'], $classIDArray));
                     }
                 }
             }
         }
     }
     $db = eZDB::instance();
     $filterTableSQL = '';
     $filterSQL = '';
     // Create extra SQL statements for the class group filters.
     if (is_array($groupList)) {
         if (count($groupList) == 0) {
             return $classList;
         }
         $filterTableSQL = ', ezcontentclass_classgroup ccg';
         $filterSQL = " AND" . "      cc.id = ccg.contentclass_id AND" . "      ";
         $filterSQL .= $db->generateSQLINStatement($groupList, 'ccg.group_id', !$includeFilter, true, 'int');
     }
     $classNameFilter = eZContentClassName::sqlFilter('cc');
     if ($fetchAll) {
         // If $asObject is true we fetch all fields in class
         $fields = $asObject ? "cc.*, {$classNameFilter['nameField']}" : "cc.id, {$classNameFilter['nameField']}";
         $rows = $db->arrayQuery("SELECT DISTINCT {$fields} " . "FROM ezcontentclass cc{$filterTableSQL}, {$classNameFilter['from']} " . "WHERE cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " {$filterSQL} AND {$classNameFilter['where']} " . "ORDER BY {$classNameFilter['nameField']} ASC");
         $classList = eZPersistentObject::handleRows($rows, 'eZContentClass', $asObject);
     } else {
         // If the constrained class list is empty we are not allowed to create any class
         if (count($classIDArray) == 0) {
             return $classList;
         }
         $classIDCondition = $db->generateSQLINStatement($classIDArray, 'cc.id');
         // If $asObject is true we fetch all fields in class
         $fields = $asObject ? "cc.*, {$classNameFilter['nameField']}" : "cc.id, {$classNameFilter['nameField']}";
         $rows = $db->arrayQuery("SELECT DISTINCT {$fields} " . "FROM ezcontentclass cc{$filterTableSQL}, {$classNameFilter['from']} " . "WHERE {$classIDCondition} AND" . "      cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " {$filterSQL} AND {$classNameFilter['where']} " . "ORDER BY {$classNameFilter['nameField']} ASC");
         $classList = eZPersistentObject::handleRows($rows, 'eZContentClass', $asObject);
     }
     if ($asObject) {
         foreach ($classList as $key => $class) {
             $id = $class->attribute('id');
             if (isset($allowedLanguages[$id])) {
                 $languageCodes = array_unique(array_merge($allowedLanguages['*'], $allowedLanguages[$id]));
             } else {
                 $languageCodes = $allowedLanguages['*'];
             }
             $classList[$key]->setCanInstantiateLanguages($languageCodes);
         }
     }
     eZDebugSetting::writeDebug('kernel-content-class', $classList, "class list fetched from db");
     return $classList;
 }
    /**
     * Finds all classes that the current user can create objects from and returns.
     * It is also possible to filter the list event more with $includeFilter and $groupList.
     *
     * @param bool $asObject If true then it return eZContentClass objects, if not it will be an associative array
     * @param bool $includeFilter If true then it will include only from class groups defined in $groupList, if not it will exclude those groups.
     * @param bool $groupList An array with class group IDs that should be used in filtering, use false if you do not wish to filter at all.
     * @param bool $fetchID A unique name for the current fetch, this must be supplied when filtering is used if you want caching to work.
     * @return array|eZPersistentObject[]
     */
    function canCreateClassList( $asObject = false, $includeFilter = true, $groupList = false, $fetchID = false )
    {
        $ini = eZINI::instance();
        $groupArray = array();
        $languageCodeList = eZContentLanguage::fetchLocaleList();
        $allowedLanguages = array( '*' => array() );

        $user = eZUser::currentUser();
        $accessResult = $user->hasAccessTo( 'content' , 'create' );
        $accessWord = $accessResult['accessWord'];

        $classIDArray = array();
        $classList = array();
        $fetchAll = false;
        if ( $accessWord == 'yes' )
        {
            $fetchAll = true;
            $allowedLanguages['*'] = $languageCodeList;
        }
        else if ( $accessWord == 'no' )
        {
            // Cannot create any objects, return empty list.
            return $classList;
        }
        else
        {
            $policies = $accessResult['policies'];
            foreach ( $policies as $policyKey => $policy )
            {
                $policyArray = $this->classListFromPolicy( $policy, $languageCodeList );
                if ( empty( $policyArray ) )
                {
                    continue;
                }
                $classIDArrayPart = $policyArray['classes'];
                $languageCodeArrayPart = $policyArray['language_codes'];
                // No class limitation for this policy AND no previous limitation(s)
                if ( $classIDArrayPart == '*' && empty( $classIDArray ) )
                {
                    $fetchAll = true;
                    $allowedLanguages['*'] = array_unique( array_merge( $allowedLanguages['*'], $languageCodeArrayPart ) );
                }
                else if ( is_array( $classIDArrayPart ) )
                {
                    $fetchAll = false;
                    foreach( $classIDArrayPart as $class )
                    {
                        if ( isset( $allowedLanguages[$class] ) )
                        {
                            $allowedLanguages[$class] = array_unique( array_merge( $allowedLanguages[$class], $languageCodeArrayPart ) );
                        }
                        else
                        {
                            $allowedLanguages[$class] = $languageCodeArrayPart;
                        }
                    }
                    $classIDArray = array_merge( $classIDArray, array_diff( $classIDArrayPart, $classIDArray ) );
                }
            }
        }

        $db = eZDB::instance();

        $filterTableSQL = '';
        $filterSQL = '';
        // Create extra SQL statements for the class group filters.
        if ( is_array( $groupList ) )
        {
            if ( count( $groupList ) == 0 )
            {
                return $classList;
            }

            $filterTableSQL = ', ezcontentclass_classgroup ccg';
            $filterSQL = ( " AND" .
                           "      cc.id = ccg.contentclass_id AND" .
                           "      " );
            $filterSQL .= $db->generateSQLINStatement( $groupList, 'ccg.group_id', !$includeFilter, true, 'int' );
        }

        $classNameFilter = eZContentClassName::sqlFilter( 'cc' );

        if ( $fetchAll )
        {
            // If $asObject is true we fetch all fields in class
            $fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
            $rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
                                     "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
                                     "WHERE cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " $filterSQL AND $classNameFilter[where] " .
                                     "ORDER BY $classNameFilter[nameField] ASC" );
            $classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
        }
        else
        {
            // If the constrained class list is empty we are not allowed to create any class
            if ( count( $classIDArray ) == 0 )
            {
                return $classList;
            }

            $classIDCondition = $db->generateSQLINStatement( $classIDArray, 'cc.id' );
            // If $asObject is true we fetch all fields in class
            $fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
            $rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
                                     "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
                                     "WHERE $classIDCondition AND" .
                                     "      cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " $filterSQL AND $classNameFilter[where] " .
                                     "ORDER BY $classNameFilter[nameField] ASC" );
            $classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
        }

        if ( $asObject )
        {
            foreach ( $classList as $key => $class )
            {
                $id = $class->attribute( 'id' );
                if ( isset( $allowedLanguages[$id] ) )
                {
                    $languageCodes = array_unique( array_merge( $allowedLanguages['*'], $allowedLanguages[$id] ) );
                }
                else
                {
                    $languageCodes = $allowedLanguages['*'];
                }
                $classList[$key]->setCanInstantiateLanguages( $languageCodes );
            }
        }

        eZDebugSetting::writeDebug( 'kernel-content-class', $classList, "class list fetched from db" );
        return $classList;
    }