Xare-a-Link /system/application/models/xare_model.php

Language PHP Lines 314
MD5 Hash 8f0b1e2d62604456307311f764e9f1fe
Repository https://github.com/xupisco/Xare-a-Link.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
class xaRe_model extends Model {

	function xaRe_model() {
		parent::Model();
	}

	function getContent($content = "", $parents = "") {
		$this->db->select("c.id, c.user_id, parent, name, name_url, short_desc, link, views, clicks, comments, related, pagerank, score, users, c.date_added");
	    if(is_numeric($content)) {
            $this->db->where("c.id = $content");
        } else {
            $this->db->where("name_url = '$content'");
            $this->db->where("parent = '$parents'");
        }
	    $uid = $this->session->userdata("uid");
        if($uid !== false) {
            $this->db->select("ul.id AS ul_id");
            $this->db->join("users_links as ul", "ul.content_id = c.id and ul.user_id = $uid", "left");
        }
        $q = $this->db->get("contents as c");
		return ($q->num_rows() > 0) ? $q->row() : false;
	}

	function getParentIDsFromPath($path = "") {
		if(!strlen($path)) return "0";
		$apath = split("/", $path);
		$step = "";
		$parents = "";
		foreach($apath as $dir) {
			if($dir != "xare" || $dir != "s") {
				$xtra = (!strlen($step)) ? " AND parent = '0'" : " AND parent = '$step'";
				$q = $this->db->query("SELECT id from contents WHERE name_url = '$dir' $xtra");
				$parents .= $q->row()->id.",";
				if(!$step) {
	                $step .= $q->row()->id;
				} else {
					$step = $q->row()->id.",".$step;
				}
			}
		}
		return implode(",", array_reverse(explode(",", substr($parents, 0, -1))));
	}

	// Related:
	//  0 - Qualquer um
	//  1 - Sem sub-itens
	//  2 - Com sub-itens (diretório)

	function getContentByParent($parent = false, $related = 0, $sort = "related, name", $st = "ASC", $sr = 0, $qs = false) {
	    $this->db->select("c.id, name, parent_name, name_url, short_desc, link, views, clicks, comments, related, pagerank, score, users, c.date_added, if(related, concat('0_', name), concat('1_', name)) as tmpsorter");
	    if($related) {
	       $rel = ($related == 1) ? "related = 0" : "related <> 0";
	       $this->db->where($rel);
	    }
	    if($parent !== false) {
		    if(is_numeric($parent)) {
		        $this->db->where("parent = $parent");
		    } else {
		        $this->db->where("name_url = '$parent'");
		    }
	    }
	    if($qs) {
	    	if(substr($qs, 0, 2) != "u:") {
	    		$qs = str_replace("s:", "", $qs);
                $this->db->where("(name like '%$qs%' or short_desc like '%$qs%')");
	    	} else {
	    		$qs = str_replace("u:", "", $qs);
	    		$this->db->where("c.id IN (SELECT content_id from users_links WHERE user_id = $qs)");
	    	}
	    }
        $uid = $this->session->userdata("uid");
	    if($uid !== false) {
            $this->db->select("ul.id AS ul_id");
            $this->db->join("users_links as ul", "ul.content_id = c.id and ul.user_id = $uid", "left");
        }
	    $sort = ($sort == "date") ? "c.date_added" : $sort;
	    $sort = ($sort == "related") ? "tmpsorter" : $sort;
	    $st = ($sort == "tmpsorter") ? "ASC" : $st;
        $this->db->orderby($sort, $st);
        $this->db->limit(25, $sr);
	    $q =  $this->db->get("contents as c");
        return $q;
	}

	function getCommentsByContentID($id) {
		$this->db->select("*");
		$this->db->orderby("date_added DESC");
		$this->db->where("content_id", $id);
		$q = $this->db->get("comments");
		return ($q->num_rows() > 0) ?  $q : false;
	}

	function getRelationFromContentID($id = 0) {
        $parents = $this->db->query("SELECT parent FROM contents WHERE id = $id")->row()->parent;
        $arr = array();
        foreach(array_reverse(explode(",", $parents)) as $parent) {
            $q = $this->db->query("SELECT name, name_url FROM contents WHERE id = $parent");
            if($q->result()) $arr[] = array($q->row()->name, $q->row()->name_url);
        }
		return $arr;
	}

	function updateContentViews($id = 0) {
		$this->db->query("UPDATE contents SET views = views + 1 WHERE id = $id");
		return true;
	}

    function updateContentClicks($id = 0) {
        $this->db->query("UPDATE contents SET clicks = clicks + 1 WHERE id = $id");
        return true;
    }

    function updateContentComments($id = 0) {
        $this->db->query("UPDATE contents SET comments = comments + 1 WHERE id = $id");
        return true;
    }

	function addDir($name, $parent) {
		$post = array(
		    'user_id' => $this->session->userdata('uid'),
            'parent' => $parent,
            'name' => $name,
		    'name_url' => url_title($name),
		    'short_desc' => "",
		    'link' => 0,
		    'views' => 0,
            'clicks' => 0,
            'comments' => 0,
		    'pagerank' => 0,
		    'score' => 0,
		    'related' => 0,
            'date_added' => time()
        );
        $this->db->insert('contents', $post);
        return $this->db->insert_id();
	}

	function addLink($data) {
		$post = array(
            'user_id' => $this->session->userdata('uid'),
            'parent' => $data['parent'],
		    'parent_name' => $data['parent_name'],
            'name' => $data['name'],
            'name_url' => url_title($data['name']),
            'short_desc' => $data['desc'],
            'link' => $data['link'],
            'views' => 0,
            'clicks' => 0,
            'comments' => 0,
            'pagerank' => $data['pagerank'],
            'score' => 0,
            'related' => 0,
            'date_added' => time()
        );
        $this->db->insert('contents', $post);
        return $this->db->insert_id();
	}

	function editLink($data, $id) {
		$post = array(
            'parent' => $data['parent'],
            'parent_name' => $data['parent_name'],
            'name' => $data['name'],
            'name_url' => url_title($data['name']),
            'short_desc' => $data['desc'],
            'link' => $data['link'],
            'pagerank' => $data['pagerank']
        );
        $this->db->where("id", $id);
        $this->db->update('contents', $post);

        $rel_parents = $id.",".$data['parent'];
        $rel_cparents = $id.",".$data['c_parents'];

        if($rel_parents != $rel_cparents) {
            $this->db->query("UPDATE contents set parent = replace(CONCAT(',',parent,','), ',$rel_cparents,', '$rel_parents') where CONCAT(',',parent,',') like '%,$rel_cparents,%'");
        }
        return $id;
	}

	function addComment($data) {
        $post = array(
            'user_id' => $this->session->userdata('uid'),
            'content_id' => $data['cid'],
            'foo_user' => $data['name'].":".$data['email'],
            'comment' => $data['cmm'],
            'date_added' => time()
        );
        $this->db->insert('comments', $post);
        $this->updateContentComments($data['cid']);
        return $this->db->insert_id();
	}

	function addRelation($ids) {
		$this->db->query("UPDATE contents SET related = related + 1 WHERE ID IN ($ids)");
		return true;
	}

	function updateRelation($is, $was) {
		$this->db->query("UPDATE contents SET related = related - 1 WHERE ID IN ($was)");
		$this->db->query("UPDATE contents SET related = related + 1 WHERE ID IN ($is)");
		return true;
	}

	function CR_Comment($data) {
		$this->db->select("id");
		$this->db->where("content_id", $data['cid']);
		$this->db->where("comment", $data['cmm']);
		$q = $this->db->get("comments");
		return ($q->num_rows() > 0) ? false : true;
	}

	function validateURL($url) {
		$url = (strpos($url, "http://") === false) ? "http://".$url : $url;
		$this->db->select("id");
		$this->db->where("link", $url);
		$q = $this->db->get("contents");
		return ($q->num_rows() > 0) ? false : true;
	}

	function verifyFav($id) {
	    $this->db->select("id");
	    $this->db->where("user_id", $this->session->userdata('uid'));
	    $this->db->where("content_id", $id);
	    $q = $this->db->get("users_links");
	    return ($q->num_rows() > 0) ? true : false;
	}

	function userSaveLink($id) {
        if(!$this->verifyFav($id)) {
    	    $post = array(
                'user_id' => $this->session->userdata('uid'),
                'content_id' => $id,
                'date_added' => time()
            );
            $this->db->insert('users_links', $post);
            $ret = $this->db->insert_id();
            $this->db->query("UPDATE contents SET users = users + 1 WHERE id = $id");
            return $ret;
        } else {
            return "1";
        }
	}

	function userRemoveLink($id) {
        $uid = $this->session->userdata("uid");
        $this->db->query("UPDATE contents SET users = users - 1 WHERE id = $id");
   	    $this->db->query("DELETE FROM users_links where content_id = $id AND user_id = $uid");
        return true;
	}
	
	function getUIDByLogin($login) {
		$q = $this->db->query("SELECT id FROM users WHERE login = '$login'");
		return ($q->num_rows() > 0) ? $q->row()->id : false;
	}
	
	function getContentTags($cid) {
		$this->db->select("count(t.tag) as rel, t.tag, t.tag_url");
		$this->db->where("tm.tag_id = t.id AND tm.content_id = $cid");
		$this->db->groupby("t.tag");
		$this->db->orderby("RAND()");
		return $this->db->get("tags AS t, tagmap AS tm")->result();
	}
	
	function getUserTags($uid, $str = 0) {
		$this->db->select("count(t.tag) as rel, t.tag, t.tag_url");
		$this->db->where("tm.tag_id = t.id");
		$this->db->where("tm.user_id = $uid");
		$this->db->groupby("t.tag");
		if($str) $this->db->where("t.tag LIKE '$str%'");
		return $this->db->get("tags AS t, tagmap AS tm")->result();
	}

	function getTagsByContentAndUser($cid = 0, $uid) {
		$this->db->select("t.tag, t.tag_url");
		$this->db->join("tagmap AS tm", "tm.tag_id = t.id", "LEFT");
		$this->db->where("tm.content_id = $cid AND tm.user_id = $uid");
		return $this->db->get("tags AS t")->result();
	}
	
	function checkBaseTags($tags) {
		$tags_id = array();
		foreach($tags as &$val) {
			$val = utf8_decode($val);
			if(strlen($val)) {
				$q = $this->db->query("SELECT id from tags where tag = '$val'");
				if(!$q->num_rows()) {
					$post = array("tag" => $val, "tag_url" => url_title($val));
					$add = $this->db->insert("tags", $post);
					$tags_id[] = $this->db->insert_id();
				} else {
					$tags_id[] = $q->row()->id;
				}
			}
		}
		return $tags_id;
	}
	
	function clearContentTags($cid, $uid) {
		$this->db->where("content_id = $cid AND user_id = $uid");
		$this->db->delete("tagmap");
		return true;
	}
	
	function updateContentTags($cid, $uid, $tags) {
		foreach($tags as $id) {
			$post = array("tag_id" => $id, "content_id" => $cid, "user_id" => $uid);
			$this->db->insert("tagmap", $post);
		}
		return true;
	}
}
?>
Back to Top