Changeset 1113 for trunk/include/functions.inc.php
- Timestamp:
- Mar 30, 2006, 2:37:07 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions.inc.php
r1109 r1113 6 6 // +-----------------------------------------------------------------------+ 7 7 // | branch : BSF (Best So Far) 8 // | file : $ RCSfile$8 // | file : $Id$ 9 9 // | last update : $Date$ 10 10 // | last modifier : $Author$ … … 786 786 787 787 /** 788 * Prepends and appends a string at each value of the given array.789 *790 * @param array791 * @param string prefix to each array values792 * @param string suffix to each array values793 */794 function prepend_append_array_items($array, $prepend_str, $append_str)795 {796 array_walk(797 $array,798 create_function('&$s', '$s = "'.$prepend_str.'".$s."'.$append_str.'";')799 );800 801 return $array;802 }803 804 /**805 * returns search rules stored into a serialized array in "search"806 * table. Each search rules set is numericaly identified.807 *808 * @param int search_id809 * @return array810 */811 function get_search_array($search_id)812 {813 if (!is_numeric($search_id))814 {815 die('Search id must be an integer');816 }817 818 $query = '819 SELECT rules820 FROM '.SEARCH_TABLE.'821 WHERE id = '.$search_id.'822 ;';823 list($serialized_rules) = mysql_fetch_row(pwg_query($query));824 825 return unserialize($serialized_rules);826 }827 828 /**829 * returns the SQL clause from a search identifier830 *831 * Search rules are stored in search table as a serialized array. This array832 * need to be transformed into an SQL clause to be used in queries.833 *834 * @param int search_id835 * @return string836 */837 function get_sql_search_clause($search_id)838 {839 $search = get_search_array($search_id);840 841 // SQL where clauses are stored in $clauses array during query842 // construction843 $clauses = array();844 845 foreach (array('file','name','comment','keywords','author') as $textfield)846 {847 if (isset($search['fields'][$textfield]))848 {849 $local_clauses = array();850 foreach ($search['fields'][$textfield]['words'] as $word)851 {852 array_push($local_clauses, $textfield." LIKE '%".$word."%'");853 }854 855 // adds brackets around where clauses856 $local_clauses = prepend_append_array_items($local_clauses, '(', ')');857 858 array_push(859 $clauses,860 implode(861 ' '.$search['fields'][$textfield]['mode'].' ',862 $local_clauses863 )864 );865 }866 }867 868 if (isset($search['fields']['allwords']))869 {870 $fields = array('file', 'name', 'comment', 'keywords', 'author');871 // in the OR mode, request bust be :872 // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')873 // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))874 //875 // in the AND mode :876 // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%')877 // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%'))878 $word_clauses = array();879 foreach ($search['fields']['allwords']['words'] as $word)880 {881 $field_clauses = array();882 foreach ($fields as $field)883 {884 array_push($field_clauses, $field." LIKE '%".$word."%'");885 }886 // adds brackets around where clauses887 array_push(888 $word_clauses,889 implode(890 "\n OR ",891 $field_clauses892 )893 );894 }895 896 array_walk(897 $word_clauses,898 create_function('&$s','$s="(".$s.")";')899 );900 901 array_push(902 $clauses,903 "\n ".904 implode(905 "\n ".906 $search['fields']['allwords']['mode'].907 "\n ",908 $word_clauses909 )910 );911 }912 913 foreach (array('date_available', 'date_creation') as $datefield)914 {915 if (isset($search['fields'][$datefield]))916 {917 array_push(918 $clauses,919 $datefield." = '".$search['fields'][$datefield]['date']."'"920 );921 }922 923 foreach (array('after','before') as $suffix)924 {925 $key = $datefield.'-'.$suffix;926 927 if (isset($search['fields'][$key]))928 {929 array_push(930 $clauses,931 932 $datefield.933 ($suffix == 'after' ? ' >' : ' <').934 ($search['fields'][$key]['inc'] ? '=' : '').935 " '".$search['fields'][$key]['date']."'"936 937 );938 }939 }940 }941 942 if (isset($search['fields']['cat']))943 {944 if ($search['fields']['cat']['sub_inc'])945 {946 // searching all the categories id of sub-categories947 $cat_ids = get_subcat_ids($search['fields']['cat']['words']);948 }949 else950 {951 $cat_ids = $search['fields']['cat']['words'];952 }953 954 $local_clause = 'category_id IN ('.implode(',', $cat_ids).')';955 array_push($clauses, $local_clause);956 }957 958 // adds brackets around where clauses959 $clauses = prepend_append_array_items($clauses, '(', ')');960 961 $where_separator =962 implode(963 "\n ".$search['mode'].' ',964 $clauses965 );966 967 $search_clause = $where_separator;968 969 if (isset($forbidden))970 {971 $search_clause.= "\n AND ".$forbidden;972 }973 974 return $search_clause;975 }976 977 /**978 788 * Returns webmaster mail address depending on $conf['webmaster_id'] 979 789 *
Note: See TracChangeset
for help on using the changeset viewer.