Mysqli errors: Trying to access array offset on value of type bool. Fixed. Please...
[fa-stable.git] / admin / db / tags_db.inc
index 2a72fd8317059f015e2d650ced751918f0837900..40602a942b0c11183582bad6a006eb9a85868f64 100644 (file)
@@ -65,7 +65,7 @@ function get_tag_type($id)
        $result = db_query($sql, "could not get tag type");
 
        $row = db_fetch_row($result);
-       return $row[0];
+       return is_array($row) ? $row[0] : false;
 }
 
 //--------------------------------------------------------------------------------------
@@ -75,11 +75,27 @@ function get_tag_name($id)
        $sql = "SELECT name FROM ".TB_PREF."tags WHERE id = ".db_escape($id);
 
        $result = db_query($sql, "could not get tag name");
-
+       
        $row = db_fetch_row($result);
-       return $row[0];
+       return $row ? $row[0] : '';
 }
 
+//----------------------------------------------------------------------------------------------------
+function get_tag_names($tags)
+{
+       $str = "";
+       if ($tags == -1)
+               return $str;
+       foreach($tags as $id)
+       {
+               $tag = get_tag_name($id);
+               if ($str == "")
+                       $str .= $tag;
+               else    
+                       $str .= ", ".$tag;
+       }
+       return $str;
+}
 //--------------------------------------------------------------------------------------
 
 function get_tag_description($id)
@@ -89,7 +105,7 @@ function get_tag_description($id)
        $result = db_query($sql, "could not get tag description");
 
        $row = db_fetch_row($result);
-       return $row[0];
+       return is_array($row) ? $row[0] : false;
 }
 
 //--------------------------------------------------------------------------------------
@@ -105,7 +121,6 @@ function delete_tag($id)
 
 function add_tag_associations($recordid, $tagids)
 {
-       _vd($tagids);
        foreach($tagids as $tagid) {
                if (!$tagid) continue;
                $sql = "INSERT INTO ".TB_PREF."tag_associations (record_id, tag_id)
@@ -126,19 +141,29 @@ function update_tag_associations($type, $recordid, $tagids)
 }
 
 //--------------------------------------------------------------------------------------
-
 // To delete tag associations, we need to specify the tag type.
 // Otherwise we may inadvertantly delete records for another type of tag
+//
 function delete_tag_associations($type, $recordid, $all=false)
 {
-       $sql = "DELETE ta FROM ".TB_PREF."tag_associations AS ta 
-                               INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
+/* multiply table DELETE syntax available since MySQL 4.0.0:
+       $sql = "DELETE ta FROM ".TB_PREF."tag_associations ta 
+                               INNER JOIN ".TB_PREF."tags tags ON tags.id = ta.tag_id 
                                WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
-       
+*/
+       // To support MySQL 3.xx we have to use multiply queries
+       $sql = "SELECT * FROM ".TB_PREF."tag_associations ta 
+                       INNER JOIN ".TB_PREF."tags tags ON tags.id = ta.tag_id 
+                       WHERE tags.type = ".db_escape($type)." AND ta.record_id = ".db_escape($recordid);
        if (!$all)
                $sql .= " AND tags.inactive = 0";
+       $result = db_query($sql, "could not select tag associations");
 
-       db_query($sql, "could not delete tag associations");
+       while($ta = db_fetch($result)) {
+               $sql2 = "DELETE FROM ".TB_PREF."tag_associations WHERE 
+                       record_id = '".$ta['record_id']."' AND tag_id=".$ta['tag_id'];
+               db_query($sql2, "could not delete tag associations");
+       }
 }
 
 //--------------------------------------------------------------------------------------
@@ -181,4 +206,17 @@ function get_tags_associated_with_record($type, $recordid)
 
 //--------------------------------------------------------------------------------------
 
-?>
\ No newline at end of file
+function is_record_in_tags($tags, $type, $recordid)
+{
+       foreach($tags as $id)
+       {
+               $sql = "SELECT ta.record_id FROM ".TB_PREF."tag_associations AS ta 
+                               INNER JOIN ".TB_PREF."tags AS tags ON tags.id = ta.tag_id 
+                               WHERE tags.type = $type AND tags.id = $id AND ta.record_id = ".db_escape($recordid);
+               $res = db_query($sql, "could not get tags associations for record");
+               if (db_num_rows($res) == 0)
+                       return false;
+       }
+       return true;
+}
+