{"id":4,"date":"2008-09-30T10:43:47","date_gmt":"2008-09-30T15:43:47","guid":{"rendered":"http:\/\/danielansari.com\/wordpress\/?p=4"},"modified":"2016-10-10T12:31:51","modified_gmt":"2016-10-10T17:31:51","slug":"adding-friends-access-level-to-elgg-10","status":"publish","type":"post","link":"http:\/\/www.danielansari.com\/wordpress\/2008\/09\/adding-friends-access-level-to-elgg-10\/","title":{"rendered":"Adding &#8220;Friends&#8221; access level to Elgg 1.1"},"content":{"rendered":"<p>Out of the box, <a href=\"http:\/\/www.elgg.org\/\">Elgg<\/a> 1.1 has three access levels: private, logged-in users, and public.<\/p>\n<p>A few people have asked about how to add a further access level: friends. Currently, the only way to give friends access to an item is to create a friends collection, then specify that collection for the access level. The problem with this approach is that every you add or remove a friend, you need to update your collection manually.<\/p>\n<p>The Elgg development philosophy is <em>not<\/em> to modify the core files, but instead to write plugins, if you need to modify any of the core functionality. However, in this case, there are no Elgg events that we can hook into, as access level is such a fundamental part of the core. Thus, we are forced to modify a couple of the core files.<\/p>\n<p>Note that this won&#8217;t work for MySQL versions prior to 4.1, as they do not support subqueries.<\/p>\n<p>Add line 147 in <code>engine\/lib\/access.php<\/code>:\n<link rel=\"stylesheet\" href=\"http:\/\/www.danielansari.com\/wordpress\/wp-content\/plugins\/codeviewer\/codeviewer.css\" type=\"text\/css\" media=\"all\" \/>\n<ol class=\"codelist\">\n<li value=\"145\" class=\"tab2 odd\"><code><span style=\"color: #000000; font-weight: bold;\">function<\/span> get_access_sql_suffix<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$table_prefix<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">&quot;&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><\/code><\/li>\n<li value=\"146\" class=\"tab2 even\"><code><span style=\"color: #009900;\">&#123;<\/span><\/code><\/li>\n<li value=\"147\" class=\"tab3 odd\"><code><a href=\"http:\/\/www.php.net\/global\"><span style=\"color: #990000;\">global<\/span><\/a> <span style=\"color: #000088;\">$CONFIG<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"148\" class=\"tab3 even\"><code><a href=\"http:\/\/www.php.net\/global\"><span style=\"color: #990000;\">global<\/span><\/a> <span style=\"color: #000088;\">$ENTITY_SHOW_HIDDEN_OVERRIDE<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"149\" class=\"odd\">&nbsp;<\/li>\n<\/ol>\n<p>Change line 167 in <code>engine\/lib\/access.php<\/code>:\n<link rel=\"stylesheet\" href=\"http:\/\/www.danielansari.com\/wordpress\/wp-content\/plugins\/codeviewer\/codeviewer.css\" type=\"text\/css\" media=\"all\" \/>\n<ol class=\"codelist\">\n<li value=\"166\" class=\"tab4 even\"><code><span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/www.php.net\/empty\"><span style=\"color: #990000;\">empty<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$sql<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><\/code><\/li>\n<li value=\"167\" class=\"tab5 odd\"><code><span style=\"color: #000088;\">$sql<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #0000ff;\">&quot; (<span style=\"color: #006699; font-weight: bold;\">{$table_prefix}<\/span>access_id in <span style=\"color: #006699; font-weight: bold;\">{$access}<\/span> or (<span style=\"color: #006699; font-weight: bold;\">{$table_prefix}<\/span>access_id = 0 and <span style=\"color: #006699; font-weight: bold;\">{$table_prefix}<\/span>owner_guid = <span style=\"color: #006699; font-weight: bold;\">$owner<\/span>) or (<span style=\"color: #006699; font-weight: bold;\">{$table_prefix}<\/span>access_id = -1 and (<span style=\"color: #006699; font-weight: bold;\">{$table_prefix}<\/span>owner_guid = <span style=\"color: #006699; font-weight: bold;\">$owner<\/span> or <span style=\"color: #006699; font-weight: bold;\">{$table_prefix}<\/span>owner_guid in (select guid_two from <span style=\"color: #006699; font-weight: bold;\">{$CONFIG-&gt;dbprefix}<\/span>entity_relationships er where er.guid_one = <span style=\"color: #006699; font-weight: bold;\">$owner<\/span> and er.relationship = 'friend'))))&quot;<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<\/ol>\n<p>Change line 201 to the following:\n<link rel=\"stylesheet\" href=\"http:\/\/www.danielansari.com\/wordpress\/wp-content\/plugins\/codeviewer\/codeviewer.css\" type=\"text\/css\" media=\"all\" \/>\n<ol class=\"codelist\">\n<li value=\"201\" class=\"tab4 odd\"><code><span style=\"color: #000088;\">$tmp_access_array<\/span> <span style=\"color: #339933;\">=<\/span> <a href=\"http:\/\/www.php.net\/array\"><span style=\"color: #990000;\">array<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #cc66cc;\">0<\/span> <span style=\"color: #339933;\">=&gt;<\/span> elgg_echo<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;PRIVATE&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #339933;\">-<\/span><span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #339933;\">=&gt;<\/span> elgg_echo<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;FRIENDS&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">1<\/span> <span style=\"color: #339933;\">=&gt;<\/span> elgg_echo<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;LOGGED_IN&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">2<\/span> <span style=\"color: #339933;\">=&gt;<\/span> elgg_echo<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">&quot;PUBLIC&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<\/ol>\n<p>The last change in core is to add line 190 in <code>languages\/en.php<\/code>:\n<link rel=\"stylesheet\" href=\"http:\/\/www.danielansari.com\/wordpress\/wp-content\/plugins\/codeviewer\/codeviewer.css\" type=\"text\/css\" media=\"all\" \/>\n<ol class=\"codelist\">\n<li value=\"187\" class=\"tab3 odd\"><code><span style=\"color: #0000ff;\">'PRIVATE'<\/span> <span style=\"color: #339933;\">=&gt;<\/span> <span style=\"color: #0000ff;\">&quot;Private&quot;<\/span><span style=\"color: #339933;\">,<\/span><\/code><\/li>\n<li value=\"188\" class=\"tab3 even\"><code><span style=\"color: #0000ff;\">'LOGGED_IN'<\/span> <span style=\"color: #339933;\">=&gt;<\/span> <span style=\"color: #0000ff;\">&quot;Logged in users&quot;<\/span><span style=\"color: #339933;\">,<\/span><\/code><\/li>\n<li value=\"189\" class=\"tab3 odd\"><code><span style=\"color: #0000ff;\">'PUBLIC'<\/span> <span style=\"color: #339933;\">=&gt;<\/span> <span style=\"color: #0000ff;\">&quot;Public&quot;<\/span><span style=\"color: #339933;\">,<\/span><\/code><\/li>\n<li value=\"190\" class=\"tab3 even\"><code><span style=\"color: #0000ff;\">'FRIENDS'<\/span> <span style=\"color: #339933;\">=&gt;<\/span> <span style=\"color: #0000ff;\">&quot;Friends&quot;<\/span><span style=\"color: #339933;\">,<\/span><\/code><\/li>\n<li value=\"191\" class=\"tab3 odd\"><code><span style=\"color: #0000ff;\">'access'<\/span> <span style=\"color: #339933;\">=&gt;<\/span> <span style=\"color: #0000ff;\">&quot;Access&quot;<\/span><span style=\"color: #339933;\">,<\/span><\/code><\/li>\n<\/ol>\n<p>Finally, the pages module needs to respect the new access level, so modify <code>mod\/pages\/start.php<\/code> by adding lines 252-256, and 262-269:\n<link rel=\"stylesheet\" href=\"http:\/\/www.danielansari.com\/wordpress\/wp-content\/plugins\/codeviewer\/codeviewer.css\" type=\"text\/css\" media=\"all\" \/>\n<ol class=\"codelist\">\n<li value=\"249\" class=\"tab4 odd\"><code><span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$write_permission<\/span><span style=\"color: #339933;\">!=<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/www.php.net\/in_array\"><span style=\"color: #990000;\">in_array<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$write_permission<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #000088;\">$list<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><\/code><\/li>\n<li value=\"250\" class=\"tab5 even\"><code><span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #000000; font-weight: bold;\">true<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"251\" class=\"odd\">&nbsp;<\/li>\n<li value=\"252\" class=\"tab4 even\"><code><span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$write_permission<\/span> <span style=\"color: #339933;\">==<\/span> <span style=\"color: #339933;\">-<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span><\/code><\/li>\n<li value=\"253\" class=\"tab5 odd\"><code><span style=\"color: #000088;\">$friends<\/span> <span style=\"color: #339933;\">=<\/span> get_friends<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$params<\/span><span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #0000ff;\">'entity'<\/span><span style=\"color: #009900;\">&#93;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"254\" class=\"tab5 even\"><code><span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span><a href=\"http:\/\/www.php.net\/in_array\"><span style=\"color: #990000;\">in_array<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$user<\/span><span style=\"color: #339933;\">-&gt;<\/span><span style=\"color: #004000;\">guid<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$friends<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span><\/code><\/li>\n<li value=\"255\" class=\"tab6 odd\"><code><span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #000000; font-weight: bold;\">true<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"256\" class=\"tab4 even\"><code><span style=\"color: #009900;\">&#125;<\/span><\/code><\/li>\n<li value=\"257\" class=\"odd\">&nbsp;<\/li>\n<li value=\"258\" class=\"tab3 even\"><code><span style=\"color: #009900;\">&#125;<\/span><\/code><\/li>\n<li value=\"259\" class=\"tab2 odd\"><code><span style=\"color: #009900;\">&#125;<\/span><\/code><\/li>\n<li value=\"260\" class=\"tab1 even\"><code><span style=\"color: #009900;\">&#125;<\/span><\/code><\/li>\n<li value=\"261\" class=\"odd\">&nbsp;<\/li>\n<li value=\"262\" class=\"tab1 even\"><code><span style=\"color: #000000; font-weight: bold;\">function<\/span> get_friends<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$entity<\/span><span style=\"color: #009900;\">&#41;<\/span><\/code><\/li>\n<li value=\"263\" class=\"tab1 odd\"><code><span style=\"color: #009900;\">&#123;<\/span><\/code><\/li>\n<li value=\"264\" class=\"tab2 even\"><code><span style=\"color: #000088;\">$friends<\/span> <span style=\"color: #339933;\">=<\/span> <a href=\"http:\/\/www.php.net\/array\"><span style=\"color: #990000;\">array<\/span><\/a><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"265\" class=\"tab2 odd\"><code><span style=\"color: #000088;\">$entities<\/span> <span style=\"color: #339933;\">=<\/span> get_entities_from_relationship<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000ff;\">'friend'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000088;\">$entity<\/span><span style=\"color: #339933;\">-&gt;<\/span><span style=\"color: #004000;\">owner_guid<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #000000; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">'user'<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000ff;\">''<\/span><span style=\"color: #339933;\">,<\/span> <span style=\"color: #cc66cc;\">1000000<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"266\" class=\"tab2 even\"><code><span style=\"color: #b1b100;\">foreach<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #000088;\">$entities<\/span> <span style=\"color: #b1b100;\">as<\/span> <span style=\"color: #000088;\">$friend<\/span><span style=\"color: #009900;\">&#41;<\/span><\/code><\/li>\n<li value=\"267\" class=\"tab3 odd\"><code><span style=\"color: #000088;\">$friends<\/span><span style=\"color: #009900;\">&#91;<\/span><span style=\"color: #009900;\">&#93;<\/span> <span style=\"color: #339933;\">=<\/span> <span style=\"color: #000088;\">$friend<\/span><span style=\"color: #339933;\">-&gt;<\/span><span style=\"color: #004000;\">guid<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"268\" class=\"tab2 even\"><code><span style=\"color: #b1b100;\">return<\/span> <span style=\"color: #000088;\">$friends<\/span><span style=\"color: #339933;\">;<\/span><\/code><\/li>\n<li value=\"269\" class=\"tab1 odd\"><code><span style=\"color: #009900;\">&#125;<\/span><\/code><\/li>\n<li value=\"270\" class=\"even\">&nbsp;<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Out of the box, Elgg 1.1 has three access levels: private, logged-in users, and public. A few people have asked about how to add a further access level: friends. Currently, the only way to give friends access to an item is to create a friends collection, then specify that collection for the access level. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[5,3,4],"_links":{"self":[{"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/posts\/4"}],"collection":[{"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":16,"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":109,"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/posts\/4\/revisions\/109"}],"wp:attachment":[{"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.danielansari.com\/wordpress\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}