Fixed issue #13670: Performance optimization: change while(list() to foreach() (thank...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_querygenerator.php
old mode 100755 (executable)
new mode 100644 (file)
index 727b8b7..1a22c9f
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 2001-2008 Christian Jul Jensen (christian@typo3.com)
+*  (c) 2001-2009 Christian Jul Jensen (christian@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -249,8 +249,7 @@ class t3lib_queryGenerator  {
                $fieldListArr = array();
                if (is_array($TCA[$this->table]))       {
                        t3lib_div::loadTCA($this->table);
-                       reset($TCA[$this->table]['columns']);
-                       while(list($fN)=each($TCA[$this->table]['columns']))    {
+                       foreach ($TCA[$this->table]['columns'] as $fN => $value)
                                $fieldListArr[]=$fN;
                        }
                        $fieldListArr[]='uid';
@@ -283,20 +282,19 @@ class t3lib_queryGenerator        {
                        $this->fieldList = $fieldList ? $fieldList : $this->makeFieldList();
 
                        $fieldArr = t3lib_div::trimExplode(',',$this->fieldList,1);
-                       reset($fieldArr);
-                       while(list(,$fN)=each($fieldArr))       {
+                       foreach ($fieldArr as $fN) {
                                $fC = $TCA[$this->table]['columns'][$fN];
                                $this->fields[$fN] = $fC['config'];
                                $this->fields[$fN]['exclude'] = $fC['exclude'];
                                if (is_array($fC) && $fC['label'])      {
-                                       $this->fields[$fN]['label'] = ereg_replace(':$','',trim($GLOBALS['LANG']->sL($fC['label'])));
+                                       $this->fields[$fN]['label'] = rtrim(trim($GLOBALS['LANG']->sL($fC['label'])), ':');
                                        switch ($this->fields[$fN]['type'])     {
                                                case 'input':
-                                                       if (eregi('int|year', $this->fields[$fN]['eval']))      {
+                                                       if (preg_match('/int|year/i', $this->fields[$fN]['eval']))      {
                                                                $this->fields[$fN]['type']='number';
-                                                       } elseif (eregi('time', $this->fields[$fN]['eval']))    {
+                                                       } elseif (preg_match('/time/i', $this->fields[$fN]['eval']))    {
                                                                $this->fields[$fN]['type'] = 'time';
-                                                       } elseif (eregi('date', $this->fields[$fN]['eval']))    {
+                                                       } elseif (preg_match('/date/i', $this->fields[$fN]['eval']))    {
                                                                $this->fields[$fN]['type']='date';
                                                        } else {
                                                                $this->fields[$fN]['type']='text';
@@ -411,9 +409,8 @@ class t3lib_queryGenerator  {
         */
        function setAndCleanUpExternalLists($name,$list,$force='')      {
                $fields = array_unique(t3lib_div::trimExplode(',',$list.','.$force,1));
-               reset($fields);
                $reList=array();
-               while(list(,$fN)=each($fields)) {
+               foreach ($fields as $fN) {
                        if ($this->fields[$fN])         $reList[]=$fN;
                }
                $this->extFieldLists[$name]=implode(',',$reList);
@@ -537,10 +534,9 @@ class t3lib_queryGenerator {
                        if(!$queryConfig[0] || !$queryConfig[0]['type']) $queryConfig[0] = array('type'=>'FIELD_');
                }
                        // Traverse:
-               reset($queryConfig);
                $c=0;
                $arrCount=0;
-               while(list($key,$conf)=each($queryConfig))      {
+               foreach ($queryConfig as $key => $conf) {
                        if(substr($conf['type'],0,6)=='FIELD_') {
                                $fName = substr($conf['type'],6);
                                $fType = $this->fields[$fName]['type'];
@@ -589,10 +585,9 @@ class t3lib_queryGenerator {
                $codeArr=array();
                if (!is_array($queryConfig))    $queryConfig=$this->queryConfig;
 
-               reset($queryConfig);
                $c=0;
                $arrCount=0;
-               while(list($key,$conf)=each($queryConfig))      {
+               foreach ($queryConfig as $key => $conf) {
                        $subscript = $parent.'['.$key.']';
                        $lineHTML = '';
                        $lineHTML.=$this->mkOperatorSelect($this->name.$subscript,$conf['operator'],$c,($conf['type']!='FIELD_'));
@@ -626,7 +621,7 @@ class t3lib_queryGenerator  {
                                case 'date':
                                        $lineHTML.=$this->mkTypeSelect($this->name.$subscript.'[type]',$fName);
                                        $lineHTML.=$this->mkCompSelect($this->name.$subscript.'[comparison]',$conf['comparison'],$conf['negate']?1:0);
-                                       $lineHTML.='<input type="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
+                                       $lineHTML.='<input type="checkbox" class="checkbox"' . ($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
 
                                        if ($conf['comparison']==100 || $conf['comparison']==101)       {       // between
                                                $lineHTML.='<input type="text" name="'.$this->name.$subscript.'[inputValue]_hr'.'" value="'.strftime('%e-%m-%Y', $conf['inputValue']).'" '.$GLOBALS['TBE_TEMPLATE']->formWidth(10).' onChange="typo3form.fieldGet(\''.$this->name.$subscript.'[inputValue]\', \'date\', \'\', 0,0);"><input type="hidden" value="'.htmlspecialchars($conf['inputValue']).'" name="'.$this->name.$subscript.'[inputValue]'.'">';
@@ -642,7 +637,7 @@ class t3lib_queryGenerator  {
                                        $lineHTML.=$this->mkTypeSelect($this->name.$subscript.'[type]', $fName);
                                        $lineHTML.=$this->mkCompSelect($this->name.$subscript.'[comparison]', $conf['comparison'], $conf['negate']?1:0);
 
-                                       $lineHTML.='<input type="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
+                                       $lineHTML.='<input type="checkbox" class="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
                                        if ($conf['comparison']==100 || $conf['comparison']==101)       {       // between:
                                                $lineHTML.='<input type="text" name="'.$this->name.$subscript.'[inputValue]_hr'.'" value="'.strftime('%H:%M %e-%m-%Y', $conf['inputValue']).'" '.$GLOBALS['TBE_TEMPLATE']->formWidth(10).' onChange="typo3form.fieldGet(\''.$this->name.$subscript.'[inputValue]\', \'datetime\', \'\', 0,0);"><input type="hidden" value="'.htmlspecialchars($conf['inputValue']).'" name="'.$this->name.$subscript.'[inputValue]'.'">';
                                                $lineHTML.='<input type="text" name="'.$this->name.$subscript.'[inputValue1]_hr'.'" value="'.strftime('%H:%M %e-%m-%Y', $conf['inputValue1']).'" '.$GLOBALS['TBE_TEMPLATE']->formWidth(10).' onChange="typo3form.fieldGet(\''.$this->name.$subscript.'[inputValue1]\', \'datetime\', \'\', 0,0);"><input type="hidden" value="'.htmlspecialchars($conf['inputValue1']).'" name="'.$this->name.$subscript.'[inputValue1]'.'">';
@@ -658,7 +653,7 @@ class t3lib_queryGenerator  {
                                case 'relation':
                                        $lineHTML.=$this->mkTypeSelect($this->name.$subscript.'[type]', $fName);
                                        $lineHTML.=$this->mkCompSelect($this->name.$subscript.'[comparison]', $conf['comparison'], $conf['negate']?1:0);
-                                       $lineHTML.='<input type="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
+                                       $lineHTML.='<input type="checkbox" class="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
                                        if ($conf['comparison']==68 || $conf['comparison']==69 || $conf['comparison']==162 || $conf['comparison']==163) {
                                                $lineHTML.='<select name="'.$this->name.$subscript.'[inputValue]'.'[]" style="vertical-align:top;" size="5" multiple>';
                                        } elseif ($conf['comparison']==66 || $conf['comparison']==67)   {
@@ -677,7 +672,7 @@ class t3lib_queryGenerator  {
                                case 'files':
                                        $lineHTML.= $this->mkTypeSelect($this->name.$subscript.'[type]', $fName);
                                        $lineHTML.= $this->mkCompSelect($this->name.$subscript.'[comparison]', $conf['comparison'], $conf['negate']?1:0);
-                                       $lineHTML.= '<input type="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
+                                       $lineHTML.= '<input type="checkbox" class="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
                                        if ($conf['comparison']==68 || $conf['comparison']==69) {
                                                $lineHTML .= '<select name="'.$this->name.$subscript.'[inputValue]'.'[]" style="vertical-align:top;" size="5" multiple>';
                                        } else {
@@ -692,13 +687,13 @@ class t3lib_queryGenerator        {
                                case 'boolean':
                                        $lineHTML .= $this->mkTypeSelect($this->name.$subscript.'[type]', $fName);
                                        $lineHTML .= $this->mkCompSelect($this->name.$subscript.'[comparison]', $conf['comparison'], $conf['negate']?1:0);
-                                       $lineHTML .= '<input type="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
+                                       $lineHTML .= '<input type="checkbox" class="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
                                        $lineHTML .= '<input type="hidden" value="1" name="'.$this->name.$subscript.'[inputValue]'.'"'.$GLOBALS['TBE_TEMPLATE']->formWidth(10).'>';
                                break;
                                default:
                                        $lineHTML .= $this->mkTypeSelect($this->name.$subscript.'[type]', $fName);
                                        $lineHTML .= $this->mkCompSelect($this->name.$subscript.'[comparison]', $conf['comparison'], $conf['negate']?1:0);
-                                       $lineHTML .= '<input type="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
+                                       $lineHTML .= '<input type="checkbox" class="checkbox"'.($conf['negate']?' checked':'').' name="'.$this->name.$subscript.'[negate]'.'" onClick="submit();">';
                                        if ($conf['comparison']==37 || $conf['comparison']==36) {       // between:
                                                $lineHTML.='<input type="text" value="'.htmlspecialchars($conf['inputValue']).'" name="'.$this->name.$subscript.'[inputValue]'.'"'.$GLOBALS['TBE_TEMPLATE']->formWidth(5).'>
                                                <input type="text" value="'.htmlspecialchars($conf['inputValue1']).'" name="'.$this->name.$subscript.'[inputValue1]'.'"'.$GLOBALS['TBE_TEMPLATE']->formWidth(5).'>';    // onChange='submit();'
@@ -710,16 +705,16 @@ class t3lib_queryGenerator        {
                        if($fType != 'ignore') {
                                $lineHTML .= $this->updateIcon();
                                if ($loopcount) {
-                                       $lineHTML .= '<input type="image" border="0" src="'.$GLOBALS['BACK_PATH'].'gfx/garbage.gif" class="absmiddle" width="11" height="12" hspace="3" vspace="3" title="Remove condition" name="qG_del'.$subscript.'">';
+                                       $lineHTML .= '<input type="image" border="0" ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/garbage.gif', 'width="11" height="12"') . 'title="Remove condition" name="qG_del'.$subscript.'">';
                                }
-                               $lineHTML .= '<input type="image" border="0" src="'.$GLOBALS['BACK_PATH'].'gfx/add.gif" class="absmiddle" width="12" height="12" hspace="3" vspace="3" title="Add condition" name="qG_ins'.$subscript.'">';
-                               if($c!=0) $lineHTML.= '<input type="image" border="0" src="'.$GLOBALS['BACK_PATH'].'gfx/pil2up.gif" class="absmiddle" width="12" height="7" hspace="3" vspace="3" title="Move up" name="qG_up'.$subscript.'">';
+                               $lineHTML .= '<input type="image" border="0" ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/add.gif', 'width="12" height="12"') . ' title="Add condition" name="qG_ins'.$subscript.'">';
+                               if($c!=0) $lineHTML.= '<input type="image" border="0" ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/pil2up.gif', 'width="12" height="7"') . ' title="Move up" name="qG_up'.$subscript.'">';
 
                                if($c!=0 && $fType!='newlevel') {
-                                       $lineHTML.= '<input type="image" border="0" src="'.$GLOBALS['BACK_PATH'].'gfx/pil2right.gif" class="absmiddle" height="12" width="7" hspace="3" vspace="3" title="New level" name="qG_nl'.$subscript.'">';
+                                       $lineHTML.= '<input type="image" border="0" ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/pil2right.gif', 'height="12" width="7"') . ' title="New level" name="qG_nl'.$subscript.'">';
                                }
                                if($fType=='newlevel') {
-                                       $lineHTML.= '<input type="image" border="0" src="'.$GLOBALS['BACK_PATH'].'gfx/pil2left.gif" class="absmiddle" height="12" width="7" hspace="3" vspace="3" title="Collapse new level" name="qG_remnl'.$subscript.'">';
+                                       $lineHTML.= '<input type="image" border="0" ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/pil2left.gif',  'height="12" width="7"') . ' title="Collapse new level" name="qG_remnl'.$subscript.'">';
                                }
 
                                $codeArr[$arrCount]['html'] = $lineHTML;
@@ -905,7 +900,7 @@ class t3lib_queryGenerator  {
                                        } else {
                                                $where_clause = 'uid';
                                                if (!$GLOBALS['SOBE']->MOD_SETTINGS['show_deleted'])    {
-                                                       $where_clause .= t3lib_BEfunc::deleteClause($from_table);
+                                                       $where_clause .= t3lib_BEfunc::deleteClause($from_table);
                                                }
                                        }
                                        $orderBy = 'uid';
@@ -953,12 +948,11 @@ class t3lib_queryGenerator        {
  * @return     [type]          ...
  */
        function printCodeArray($codeArr,$l=0)  {
-               reset($codeArr);
                $line='';
                if ($l)         $indent='<td style="vertical-align:top;"><img height="1" width="50"></td>';
                $lf=$l*30;
                $bgColor = t3lib_div::modifyHTMLColor($GLOBALS['TBE_TEMPLATE']->bgColor2,$lf,$lf,$lf);
-               while(list($k,$v)=each($codeArr))       {
+               foreach ($codeArr as $k => $v) {
                        $line.= '<tr>'.$indent.'<td bgcolor="'.$bgColor.'"'.$this->noWrap.'>'.$v['html'].'</td></tr>';
                        if ($this->enableQueryParts)    {$line.= '<tr>'.$indent.'<td>'.$this->formatQ($v['query']).'</td></tr>';}
                        if (is_array($v['sub']))        {
@@ -976,7 +970,7 @@ class t3lib_queryGenerator  {
         * @return      [type]          ...
         */
        function formatQ($str)  {
-               return '<font size="1" face="verdana" color="maroon"><i>'.$str.'</i></font>';
+               return '<font size="1" face="verdana" color="maroon"><i>' . htmlspecialchars($str) . '</i></font>';
        }
 
        /**
@@ -1013,8 +1007,7 @@ class t3lib_queryGenerator        {
        function mkTypeSelect($name,$fieldName,$prepend='FIELD_')       {
                $out='<select name="'.$name.'" onChange="submit();">';
                $out.='<option value=""></option>';
-               reset($this->fields);
-               while(list($key,)=each($this->fields)) {
+               foreach ($this->fields as $key => $value) {
                        if (!$fieldValue['exclude'] || $GLOBALS['BE_USER']->check('non_exclude_fields', $this->table.':'.$key)) {
                                $label = $this->fields[$key]['label'];
                                $label_alt = $this->fields[$key]['label_alt'];
@@ -1032,9 +1025,8 @@ class t3lib_queryGenerator        {
         * @return      [type]          ...
         */
        function verifyType($fieldName) {
-               reset($this->fields);
                $first = '';
-               while(list($key,)=each($this->fields)) {
+               foreach ($this->fields as $key => $value) {
                        if (!$first)    $first = $key;
                        if ($key==$fieldName) return $key;
                }
@@ -1069,10 +1061,9 @@ class t3lib_queryGenerator       {
         */
        function mkFieldToInputSelect($name,$fieldName) {
                $out='<input type="Text" value="'.htmlspecialchars($fieldName).'" name="'.$name.'"'.$GLOBALS['TBE_TEMPLATE']->formWidth().'>'.$this->updateIcon();
-               $out.='<a href="#" onClick="document.forms[0][\''.$name.'\'].value=\'\';return false;"><img src="'.$GLOBALS['BACK_PATH'].'gfx/garbage.gif" class="absmiddle" width="11" height="12" hspace="3" vspace="3" title="Clear list" border="0"></a>';
+               $out.='<a href="#" onClick="document.forms[0][\''.$name.'\'].value=\'\';return false;"><img ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/garbage.gif', 'width="11" height="12"') . ' class="absmiddle" title="Clear list" border="0"></a>';
                $out.='<BR><select name="_fieldListDummy" size="5" onChange="document.forms[0][\''.$name.'\'].value+=\',\'+this.value">';
-               reset($this->fields);
-               while(list($key,)=each($this->fields)) {
+               foreach ($this->fields as $key => $value) {
                        if (!$fieldValue['exclude'] || $GLOBALS['BE_USER']->check('non_exclude_fields', $this->table.':'.$key)) {
                                $label = $this->fields[$key]['label'];
                                $label_alt = $this->fields[$key]['label_alt'];
@@ -1094,8 +1085,7 @@ class t3lib_queryGenerator        {
                global $TCA;
                $out='<select name="'.$name.'" onChange="submit();">';
                $out.='<option value=""></option>';
-               reset($TCA);
-               while(list($tN)=each($TCA)) {
+               foreach ($TCA as $tN => $value) {
                        if ($GLOBALS['BE_USER']->check('tables_select',$tN))    {
                                $out.='<option value="'.$tN.'"'.($tN==$cur ? ' selected':'').'>'.$GLOBALS['LANG']->sl($TCA[$tN]['ctrl']['title']).'</option>';
                        }
@@ -1178,18 +1168,17 @@ class t3lib_queryGenerator      {
                $qs = '';
                        // Since we don't traverse the array using numeric keys in the upcoming whileloop make sure it's fresh and clean
                ksort($queryConfig);
-               reset($queryConfig);
                $first=1;
-               while(list($key,$conf) = each($queryConfig)) {
+               foreach ($queryConfig as $key => $conf) {
                        switch($conf['type']) {
                                case 'newlevel':
-                                       $qs.=chr(10).$pad.trim($conf['operator']).' ('.$this->getQuery($queryConfig[$key]['nl'],$pad.'   ').chr(10).$pad.')';
+                                       $qs.=LF.$pad.trim($conf['operator']).' ('.$this->getQuery($queryConfig[$key]['nl'],$pad.'   ').LF.$pad.')';
                                break;
                                case 'userdef':
-                                       $qs.=chr(10).$pad.getUserDefQuery($conf,$first);
+                                       $qs.=LF.$pad.getUserDefQuery($conf,$first);
                                break;
                                default:
-                                       $qs.=chr(10).$pad.$this->getQuerySingle($conf,$first);
+                                       $qs.=LF.$pad.$this->getQuerySingle($conf,$first);
                                break;
                        }
                        $first=0;
@@ -1278,7 +1267,7 @@ class t3lib_queryGenerator        {
         * @return      [type]          ...
         */
        function updateIcon()   {
-               return '<input type="image" border="0" src="'.$GLOBALS['BACK_PATH'].'gfx/refresh_n.gif" class="absmiddle" width="14" height="14" hspace="3" vspace="3" title="Update" name="just_update">';
+               return '<input type="image" border="0" ' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/refresh_n.gif',  'width="14" height="14"') . ' title="Update" name="just_update">';
        }
 
        /**
@@ -1333,9 +1322,8 @@ class t3lib_queryGenerator        {
                        if ($this->extFieldLists['queryOrder']) {
                                $descParts = explode(',',$modSettings['queryOrderDesc'].','.$modSettings['queryOrder2Desc']);
                                $orderParts = explode(',',$this->extFieldLists['queryOrder']);
-                               reset($orderParts);
                                $reList=array();
-                               while(list($kk,$vv)=each($orderParts))  {
+                               foreach ($orderParts as $kk => $vv) {
                                        $reList[]=$vv.($descParts[$kk]?' DESC':'');
                                }
                                $this->extFieldLists['queryOrder_SQL'] = implode(',',$reList);
@@ -1416,7 +1404,7 @@ class t3lib_queryGenerator        {
                                ';
                        }
                }
-               $out='<table border="0" cellpadding="3" cellspacing="1">'.$out.'</table>';
+               $out='<table border="0" cellpadding="3" cellspacing="1" class="qg-make-query">'.$out.'</table>';
                $out.=$this->JSbottom($this->formName);
                return $out;
        }
@@ -1482,7 +1470,9 @@ class t3lib_queryGenerator        {
                                $qString .= ' AND pid IN ('.$webMountPageTree.')';
                        }
                }
-               $fieldlist = $this->extFieldLists['queryFields'].',pid,deleted';
+               $fieldlist = $this->extFieldLists['queryFields'] .
+                       ',pid' .
+                       ($GLOBALS['TCA'][$this->table]['ctrl']['delete'] ? ',' . $GLOBALS['TCA'][$this->table]['ctrl']['delete'] : '');
                if (!$GLOBALS['SOBE']->MOD_SETTINGS['show_deleted'])    {
                        $qString .= t3lib_BEfunc::deleteClause($this->table);
                }