Share
Embed
So, you wanna build an add-on...
An introduction to building an ExpressionEngine Add-on.
Lodewijk Schutte
October 17, 2012
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
70 slides
-
So, you wanna build an add-on… By Lodewijk Schutte ~
Low, October 2012 -
1. Analyze the Brief 2. Create Scaffold 3. Fill in
the Gaps 4. Optimize & Simplify 5. Document it The Plan -
A member can like and unlike an entry » many-to-many
member-entry relationship: DB table » Add/remove records to/from this table » A form per entry Show number of likes for each entry » Get like-count for each entry Show liked entries for a logged in member » Show channel entries based on likes by member Show entries with most likes » Show channel entries sorted by number of likes -
Overview of recently liked entries » List of entry +
member + date » DB table needs a timestamp Overview of members that liked an entry » For a single entry, show members that liked it Overview of entries liked by a member » For a single member, show entries that he/she liked Keep like-counts accurate » When an entry is deleted, remove its likes » When a member is deleted, remove his/her likes -
- Define features - Think of a name - Sketch
out database tables, if needed 1. Analyze the Brief -
- Define features - Think of a name - Sketch
out database tables, if needed - Create dummy template code 1. Analyze the Brief -
{exp:channel:entries/channel="products"} / <h2>{title}</h2> / {exp:low_likes:show/entry_id="{entry_id}"} / / <p> / /
/ {total_likes}/Like{if/total_likes/!=/1}s{/if} / / </p> / {/exp:low_likes:show} {/exp:channel:entries} -
{exp:channel:entries/channel="products"} / <h2>{title}</h2> / {exp:low_likes:show/entry_id="{entry_id}"/form="yes"} / / <p> / /
/ {total_likes}/Like{if/total_likes/!=/1}s{/if} / / / {if/has_form} / / / / <button/type="submit"> / / / / / {if/is_liked}Unlike{if:else}Like{/if} / / / / </button> / / / {/if} / / </p> / {/exp:low_likes:show} {/exp:channel:entries} -
low_likes » Module upd.low_likes.php mod.low_likes.php mcp.low_likes.php language/english/low_likes_lang.php css/low_likes.css views/mcp_index.php views/mcp_entries.php
views/mcp_members.php » Extension ext.low_likes.php » Package constants config.php -
Classes & Methods » low_likes_upd install() uninstall() update() » low_likes
show() entries() popular() toggle_like() » low_likes_mcp index() entries() members() » low_likes_ext member_delete() delete_entries_loop() channel_entries_query_result() -
- Keep the EE & CI docs ready 3. Fill
in the Gaps -
- Keep the EE & CI docs ready - Outline
methods with comments 3. Fill in the Gaps -
- Keep the EE & CI docs ready - Outline
methods with comments - Create and test install/uninstall routine 3. Fill in the Gaps -
ext.low_likes.php » Add property to move (un)installation to module public/$required_by/=/array('module');
upd.low_likes.php » Install method Create/new/table Add/row/to/exp_modules/table Add/row/to/exp_actions/table Add/rows/to/exp_extensions/table » Uninstall method Remove/rows/from/exp_modules,/exp_actions,/ exp_extensions/and/exp_module_member_groups/tables Drop/table » Update method Nothing/yet... -
- Keep the EE & CI docs ready - Outline
methods with comments - Create and test install/uninstall routine - Create the rest 3. Fill in the Gaps -
How to draw an owl Step 1: draw some circles
Step 2: draw the rest of the owl -
{exp:channel:entries/channel="products"} / <h2>{title}</h2> / {exp:low_likes:show/entry_id="{entry_id}"/form="yes"} / / <p> / /
/ {total_likes}/Like{if/total_likes/!=/1}s{/if} / / / {if/has_form} / / / / <button/type="submit"> / / / / / {if/is_liked}Unlike{if:else}Like{/if} / / / / </button> / / / {/if} / / </p> / {/exp:low_likes:show} {/exp:channel:entries} -
/** /*/Show/Likes/for/entry,/possibly/wrapped/in/a/form/tag /*/ public/function/show() { ///////Get/the/tagdata ///////Check/for/entry/ID/parameter,/bail/out/if/not/present ///////Query/database/for/this/entry's/likes ///////Create/variables/array /
///////Parse/the/vars/in/the/tagdata / ///////Wrap/tagdata/in/a/form/if/needed ///////Return/final/output } -
/** /*/Show/Likes/for/entry,/possibly/wrapped/in/a/form/tag /*/ public/function/show() { ///////Get/the/tagdata ////$tagdata/=/$thisE>EEE>TMPLE>tagdata; ///////Check/for/entry/ID/parameter,/bail/out/if/not/present ////if/(/!/($entry_id/=/$thisE>EEE>TMPLE> ////////////fetch_param('entry_id')))
////{ ///////////Add/message/to/template/log ////////$thisE>EEE>TMPLE>log_item('Low/Likes:/no/entry_id'); ////////return/$tagdata; ////} ///////Query/database/for/this/entry's/likes ///////Create/variables/array / -
///////Query/database/for/this/entry's/likes ////$likes/=/array(); ////$query/=/$thisE>EEE>dbE>select('member_id') ///////////E>from('low_likes') ///////////E>where('entry_id',/$entry_id) ///////////E>get(); ////foreach/($queryE>result()/AS/$row) ////{ ////////$likes[]/=/$rowE>member_id; ////}/
///////Create/variables/array / ///////Parse/the/vars/in/the/tagdata / ///////Wrap/tagdata/in/a/form/if/needed ///////Return/final/output } -
///////Query/database/for/this/entry's/likes ////$likes/=/array(); ////$query/=/$thisE>EEE>dbE>select('member_id') ///////////E>from('low_likes') ///////////E>where('entry_id',/$entry_id) ///////////E>get(); ////foreach/($queryE>result()/AS/$row) ////{ ////////$likes[]/=/$rowE>member_id; ////}/
///////Create/variables/array ////$member_id/=/$thisE>EEE>sessionE>userdata('member_id'); ////$form/=/(($thisE>EEE>TMPLE>fetch_param('form')/==/'yes') //////////////&&/$member_id); ////$vars/=/array( ////////'total_likes'/=>/count($likes), ////////'is_liked'////=>/in_array($member_id,/$likes), -
////}/ ///////Create/variables/array ////$member_id/=/$thisE>EEE>sessionE>userdata('member_id'); ////$form/=/(($thisE>EEE>TMPLE>fetch_param('form')/==/'yes') //////////////&&/$member_id); ////$vars/=/array( ////////'total_likes'/=>/count($likes), ////////'is_liked'////=>/in_array($member_id,/$likes), ////////'has_form'////=>/$form ////);
/ ///////Parse/the/vars/in/the/tagdata ////$tagdata/=/$thisE>EEE>TMPLE>parse_variables_row($tagdata, ///////////////////$vars); / ///////Wrap/tagdata/in/a/form/if/needed ///////Return/final/output } -
///////Wrap/tagdata/in/a/form/if/needed ////if/($form) ////{ ///////////Initiate/data/array/for/form/creation ////////$fd/=/array( ////////////'id'////=>/$thisE>EEE>TMPLE>fetch_param('form_id'), ////////////'class'/=>/$thisE>EEE>TMPLE>fetch_param('form_class') ////////); ///////////Define/default/hidden/fields ////////$fd['hidden_fields']/=/array(
////////////'ACT'/=>/$thisE>EEE>functionsE>fetch_action_id( /////////////////////'Low_likes',/'toggle_like'), ////////////'EID'/=>/$entry_id ////////); ///////////Wrap/form/around/tagdata ////////$tagdata/=/$thisE>EEE>functionsE>form_declaration($fd) /////////////////./$tagdata /////////////////./'</form>'; ////} -
///Wrap/tagdata/in/a/form/if/needed if/($form) { ///////Initiate/data/array/for/form/creation ////$fd/=/array( ////////'id'////=>/$thisE>EEE>TMPLE>fetch_param('form_id'), ////////'class'/=>/$thisE>EEE>TMPLE>fetch_param('form_class') ////); ///////Define/default/hidden/fields ////$fd['hidden_fields']/=/array(
////////'ACT'/=>/$thisE>EEE>functionsE>fetch_action_id( /////////////////'Low_likes',/'toggle_like'), ////////'EID'/=>/$entry_id ////); ///////Wrap/form/around/tagdata ////$tagdata/=/$thisE>EEE>functionsE>form_declaration($fd) /////////////./$tagdata /////////////./'</form>'; } -
///////Wrap/tagdata/in/a/form/if/needed ////if/($form) ////{ ///////////Initiate/data/array/for/form/creation ////////$fd/=/array( ////////////'id'////=>/$thisE>EEE>TMPLE>fetch_param('form_id'), ////////////'class'/=>/$thisE>EEE>TMPLE>fetch_param('form_class') ////////); ///////////Define/default/hidden/fields ////////$fd['hidden_fields']/=/array(
////////////'ACT'/=>/$thisE>EEE>functionsE>fetch_action_id( /////////////////////'Low_likes',/'toggle_like'), ////////////'EID'/=>/$entry_id ////////); ///////////Wrap/form/around/tagdata ////////$tagdata/=/$thisE>EEE>functionsE>form_declaration($fd) /////////////////./$tagdata /////////////////./'</form>'; ////} -
///////////Initiate/data/array/for/form/creation ////////$fd/=/array( ////////////'id'////=>/$thisE>EEE>TMPLE>fetch_param('form_id'), ////////////'class'/=>/$thisE>EEE>TMPLE>fetch_param('form_class') ////////); ///////////Define/default/hidden/fields ////////$fd['hidden_fields']/=/array( ////////////'ACT'/=>/$thisE>EEE>functionsE>fetch_action_id( /////////////////////'Low_likes',/'toggle_like'), ////////////'EID'/=>/$entry_id
////////); ///////////Wrap/form/around/tagdata ////////$tagdata/=/$thisE>EEE>functionsE>form_declaration($fd) /////////////////./$tagdata /////////////////./'</form>'; ////} ///////Return/final/output ////return/$tagdata; } -
/** /*/ACT:/(un)like/posted/entry /*/ public/function/toggle_like() { ///////Get/entry_id/from/POST,/member_id/from/session ////$entry_id/=/$thisE>EEE>inputE>post('EID'); ////$member_id/=/$thisE>EEE>sessionE>userdata('member_id'); ///////Only/continue/if/both/are/valid ////if/($entry_id/&&/$member_id)
////{ ///////////Create/data/array/for/DB/queries ////////$data/=/array( ////////////'entry_id'//=>/$entry_id, ////////////'member_id'/=>/$member_id ////////); ///////////Is/entry/liked/by/member? ////////$thisE>EEE>dbE>where($data); ////////$liked/=/$thisE>EEE>dbE>count_all_results('low_likes'); -
///////////If/liked,/delete/entry;/else,/insert/entry ////////if/($liked) ////////{ ////////////$thisE>EEE>dbE>delete('low_likes',/$data); ////////} ////////else ////////{ ////////////$data['like_date']/=/$thisE>EEE>localizeE>now; ////////////$thisE>EEE>dbE>insert('low_likes',/$data); ////////}
///////////Cater/for/Ajax/requests ////////if/(AJAX_REQUEST) ////////{ ////////////die($liked/?/'E1'/:/'1'); ////////} ////} //////// ///////Return/to/previous/page ////$thisE>EEE>functionsE>redirect( ////////$thisE>EEE>sessionE>tracker[1] ////); -
{exp:channel:entries/channel="products"/limit="25"} / <h2>{title}</h2> / {exp:low_likes:show/entry_id="{entry_id}"/form="yes"} / / <p> / /
/ {total_likes}/Like{if/total_likes/!=/1}s{/if} / / / {if/has_form} / / / / <button/type="submit"> / / / / / {if/is_liked}Unlike{if:else}Like{/if} / / / / </button> / / / {/if} / / </p> / {/exp:low_likes:show} {/exp:channel:entries} -
0.0005 SELECT `member_id` FROM (`exp_low_likes`) WHERE `entry_id` = '29570' 0.0004
SELECT `member_id` FROM (`exp_low_likes`) WHERE `entry_id` = '29533' 0.0003 SELECT `member_id` FROM (`exp_low_likes`) WHERE `entry_id` = '29532' 0.0003 SELECT `member_id` FROM (`exp_low_likes`) WHERE `entry_id` = '29530' 0.0003 SELECT `member_id` FROM (`exp_low_likes`) WHERE `entry_id` = '29531' 0.0004 SELECT `member_id` FROM (`exp_low_likes`) WHERE `entry_id` = '29529' 0.0004 SELECT `member_id` -
/** /*/Add/likeEvars/to/channel:entries/data /*/ public/function/channel_entries_query_result($obj,/$entries) { ///////Get/the/latest/version/of/$entries ////if/($thisE>EEE>extensionsE>last_call/!==/FALSE) ////{ ////////$entries/=/$thisE>EEE>extensionsE>last_call; ////}
///////Is/there/a/low_likes/tag/here? ////if/(/!/strpos($thisE>EEE>TMPLE>tagdata,/'exp:low_likes:')) ////{ ////////return/$entries; ////} ///////Initiate/likes/for/all/given/entry_ids ////$likes/=/array(); ////foreach/($entries/AS/$row) ////{ -
////foreach/($entries/AS/$row) ////{ ////////$likes[$row['entry_id']]/=/array(); ////} ///////Query/DB/for/these/entry_ids ////$query/=/$thisE>EEE>dbE>select('entry_id,/member_id') ///////////E>from('low_likes') ///////////E>where_in('entry_id',/array_keys($likes)) ///////////E>get(); ////foreach/($queryE>result()/AS/$row)
////{ ////////$likes[$rowE>entry_id][]/=/$rowE>member_id; ////} ///////Set/cache ////$thisE>EEE>sessionE>set_cache('Low',/'likes',/$likes); ///////Return/query ////return/$entries; } -
///////Query/database/for/this/entry's/likes ///////$likes/=/array(); ///////$query/=/$thisE>EEE>dbE>select('member_id') //////////////E>from('low_likes') //////////////E>where('entry_id',/$entry_id) //////////////E>get(); ///////foreach/($queryE>result()/AS/$row) ///////{ ////////$likes[]/=/$rowE>member_id; ///////}
///////Get/likeEdata/from/cache ////$likes/=/$thisE>EEE>sessionE>cache('Low',/'likes'); ///////Gets/for/this/entry/only ////$likes/=/(array)/@$likes[$entry_id]; -
0.0003 SELECT `entry_id`, `member_id` FROM (`exp_low_likes`) WHERE `entry_id` IN (29570,
29533, 29532, 29530, 29531, 29529, 29528, 29527, 29526, 29525, 29524, 29523, 29522, 29521, 29520, 29519, 29518, 29517, 29516, 29515, 29514, 29513, 29512, 29511, 29510) -
/** /*/Show/entries/liked/by/logged/in/member /*/ public/function/entries() { ///////Get/entry/ids/for/logged/in/member/from/DB ///////Set/fixed_order/parameter/based/on/ids ///////Load/&/call/the/Channel/module } /**
/*/Show/entries/with/most/likes /*/ public/function/popular() { ///////Get/entry/ids/and/member/count/from/DB ///////Set/fixed_order/parameter/based/on/ids ///////Load/&/call/the/Channel/module } -
/** /*/Show/entries/liked/by/logged/in/member /*/ public/function/entries() { ///////Get/entry/ids/for/logged/in/member/from/DB ///////Set/fixed_order/parameter/based/on/ids ///////Load/&/call/the/Channel/module } /**
/*/Show/entries/with/most/likes /*/ public/function/popular() { ///////Get/entry/ids/and/member/count/from/DB ///////Set/fixed_order/parameter/based/on/ids ///////Load/&/call/the/Channel/module } -
/** /*/Show/entries/liked/by/logged/in/member /*/ public/function/entries() { ///////Get/entry/ids/for/logged/in/member/from/DB } /** /*/Show/entries/with/most/likes /*/
public/function/popular() { ///////Get/entry/ids/and/member/count/from/DB } /** /*/Load/and/call/Channel::entries() /*/ private/function/_get_entries($entry_ids) { ///////Set/fixed_order/parameter/based/on/ids ///////Load/&/call/the/Channel/module -
/*/ public/function/entries() { ///////Get/entry/ids/for/logged/in/member/from/DB } /** /*/Show/entries/with/most/likes /*/ public/function/popular() {
///////Get/entry/ids/and/member/count/from/DB } /** /*/Load/and/call/Channel::entries() /*/ private/function/_get_entries($entry_ids) { ///////Set/fixed_order/parameter/based/on/ids ///////Load/&/call/the/Channel/module } -
Other ways to not repeat yourself » Use a “base”
class and inheritance » Create your own libraries, models, and helpers » Use “shortcuts” for frequently requested config items Simplify, but also keep it simple » Choose readability over elegance » Separate add-on data from the core