====== Creating Your mooSocial First Plugin ====== //NOTE: We’re currently working on documenting these sections. We believe the information here is accurate, however be aware we are also still working on this chapter. Additional information will be provided as we go which should make this chapter more solid.// Sample Plugin: [[https://www.moosocial.com//wiki/lib/plugins/ckgedit/fckeditor/userfiles/file/moo-note-1.0.zip|https://www.moosocial.com//wiki/lib/plugins/ckgedit/fckeditor/userfiles/file/moo-note-1.0.zip]] ====== Welcome to the mooSocial guides ====== It is designed to start from the basics, and slowly increase to more sophisticated concepts until you know everything there is to know about building awesome mooSocial plugin .\\ \\ mooSocial is built on CakePHP 2 framework and its plugins are designed base on CakePHP plugin so that you can set up a combination of controllers , models, and views and release them as a packaged . ====== Development Mode ====== To enable this setting, go to **Home> System Admin> System Setting> Sytem Mode> Production Mode> Develelopment** Mode {{:documentation:671bd2f6e1c76c98d5f7c6ade706dcad.png}} There are three modes: * Production (default): hide all errors, warnings and sql query strings. * Development 1: show all errors, warnings, model caches refreshed and flash messages halted. * Development 2: show all errors, warnings, sql query strings, model caches refreshed and flash messages halted ====== Frontend ====== ===== Creating a Plugin ===== ==== How to create plugin ==== Let’s begin to create a Note plugin .In this tutorial we will be making a simple //plugin// that will let users write //notes// and save them as prefix_notes table on the mooSocial system for each mooSocial page . To start out, we need to create plugin by go to **Home> Plugins Manager> Manage> Create New PLugin** (See image) . This tutorial demonstrates how to create controller , model , view in mooSocial {{:documentation:b9169f9a89c1c74865f000151cef4708.png}} === Plugin Name , Key === The first task in creating a mooSocial Plugin is to think about what the plugin will do, and make a unique key for your plugin. Key only contains letters, numbers and the underscore '_' with no space Example: ContactManager === Plugin Files === == Basic directory structure == After that , our plugin’s [[http://confluence.socialloft.com/download/attachments/4393735/basic-directory-structure.png?version=1&modificationDate=1416194021000|basic directory structure]] should look like this: {{:documentation:a6d3caf392f7452890df3ef70d228086.png}} == info.xml == This file contains the standard plugin information . == PluginNamePlugin.php == * install: executed during installation proccess. * install: executed during uninstallation proccess. * settingGuide: display plugin setting gude. * menu: you can add or remove plugin tabs. There are two default tabs: General and Settings. * callback_x (Optional) (x is version): executed during upgrade process. Example: you want to create a callback function for version 2.0, just create a function named callback_2_0. == Config/bootstrap.php == This file contains the configuration of your plugin . == Config/routes.php == This file contains your routers configuration plugin . \\ \\ Note : Routing is a feature that maps URLs to controller actions . It will help plugins to make pretty URLs more configurable and flexible. == Config/install/install.sql == This file contains sql query strings that will be executed during installation proccess. == Config/install/uninstall.sql == This file contains sql query strings that will be executed during uninstallation proccess. == Config/install/upgrade.xml == This file contains sql query strings that will be executed during upgrade proccess.' ==== Creating Note Database ==== Execute the following SQL statements into your database CREATE TABLE IF NOT EXISTS {PREFIX}notes ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id INT(11) NOT NULL, title VARCHAR(50), body TEXT, uri VARCHAR(150), created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL, like_count INT(11) DEFAULT '0', dislike_count INT(11) DEFAULT '0', comment_count INT(11) DEFAULT '0', FULLTEXT KEY title (title,body) )ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO {PREFIX}notes (user_id,title,body,created,modified,uri) VALUES ('1', 'The title', 'This is the post body.', NOW(), NOW(),'uri_1'), ('1', 'A title once again', 'And the post body follows.', NOW(), NOW(),'uri_2'), ('1', 'Title strikes back', 'This is really exciting! Not.', NOW(), NOW(),'uri_3'); INSERT INTO `{PREFIX}tasks` (`title`, `plugin`, `timeout`, `processes`, `semaphore`, `started_last`, `started_count`, `completed_last`, `completed_count`, `failure_last`, `failure_count`, `success_last`, `success_count`, `enable`, `class`) VALUES ('Reminder', 'note', 300, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 'Note_Task_Reminder'); ==== Create Note Model ==== Create a new model file **Plugin/Note/Model/Note.php** and copy the code below Plugin model class files go in **/app/Plugin/Note/Model/**, and the file we’ll be creating will be saved to **/app/Plugin/Note/Model/Note.php**. The completed file should look like this: array( 'rule' => 'notEmpty' ), 'body' => array( 'rule' => 'notEmpty' ) ); public $belongsTo = array( 'User' => array('counterCache' => true )); public $mooFields = array('title','href','plugin','type'); public function getHref($row) { if(isset($row['id'])) { $request = Router::getRequest(); return $request->base.'/notes/view/'.$row['id']; } return ''; } } Naming conventions are very important in CakePHP. By naming our model Note, CakePHP can automatically infer that this model will be used in the NotesController, and will be tied to a database table called notes CakePHP will dynamically create a model object for you if it cannot find a corresponding file in /app/Model. This also means that if you accidentally name your file wrong (for example, note.php or notes.php instead of Note.php), CakePHP will not recognize any of your settings and will use the defaults instead. For more on models, such as table prefixes, callbacks, and validation , see [[http://book.cakephp.org/2.0/en/models.html|http://book.cakephp.org/2.0/en/models.html]] ==== Create a Notes Controller ==== Next, we’ll create a controller for our notes. The controller is where all the business logic for notes interaction will happen . We will define functions : index , create , delete , update in NotesController . Users can access the logic there by requesting : mooSite/notes/index , mooSite/notes/create , mooSite/notes/delete , mooSite/notes/update . We’ll place this new controller in a file called **NotesController.php** inside the ** /app/Plugin/Note/Controller ** directory. Here’s what the basic controller should look like: set('notes', $this->Note->find('all')); } public function view($id = null) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } $note = $this->Note->findById($id); if (!$note) { throw new NotFoundException(__d('Note', 'Invalid note')); } $this->set('note', $note); $this->likesComments($id); } private function likesComments($id) { //comment $this->Comment = ClassRegistry::init('Comment'); $comments = $this->Comment->getComments($id, 'Note_Note'); $comment_count = $this->Comment->getCommentsCount($id, 'Note_Note'); $page = 1; $data['bIsCommentloadMore'] = $comment_count - $page * RESULTS_LIMIT; $data['comments'] = $comments; $this->set('data', $data); $this->set('comment_count', $comment_count); //like $this->Like = ClassRegistry::init('Like'); $likes = $this->Like->getLikes($id, 'Note_Note'); $dislikes = $this->Like->getDisLikes($id, 'Note_Note'); $this->set('likes', $likes); $this->set('dislikes', $dislikes); } public function add() { if ($this->request->is('post')) { $this->Note->create(); $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $this->Session->setFlash(__d('Note', 'Your note has been saved.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to add your note.')); } } public function edit($id = null) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } $note = $this->Note->findById($id); if (!$note) { throw new NotFoundException(__d('Note', 'Invalid note')); } if ($this->request->is(array('post', 'put'))) { $this->Note->id = $id; $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $this->Session->setFlash(__d('Note', 'Your note has been updated.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to update your note.')); } if (!$this->request->data) { $this->request->data = $note; } } public function delete($id) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } if ($this->Note->delete($id)) { $this->Session->setFlash( __('The note with id: %s has been deleted.', h($id)) ); return $this->redirect(array('action' => 'index')); } } } The single instruction in the action uses set() to pass data from the controller to the view (which we’ll create next). The line sets the view variable called ‘notes’ equal to the return value of the find('all') method of the Note model. Our Note model is automatically available at $this→Note because we’ve followed CakePHP’s naming conventions. ==== Creating Views ==== CakePHP views are just presentation-flavored fragments that fit inside an application’s layout. For most applications, they’re HTML mixed with PHP, but they may end up as XML, CSV, or even binary data. === Note index view ===

Html->link( __d('Note', 'Add Note'), array( 'controller' => 'notes', 'action' => 'add', ), array('class' => 'button button-action topButton button-mobi-top') ); ?>
Html->link($note['Note']['title'], array( 'controller' => 'notes', 'action' => 'view', $note['Note']['id'])); ?> Html->link(__d('Notes', 'Edit'), array( 'controller' => 'notes', 'action' => 'edit', $note['Note']['id'] ));?> | Html->link(__d('Notes', 'Delete'), array( 'controller' => 'notes', 'action' => 'delete', $note['Note']['id']), array( 'confirm' => __d('Notes', 'Are you sure?') ));?>
=== Note view === Now let’s create the view for our new ‘view’ action and place it in **/app/Plugin/Note/View/Notes/view.ctp**.

Html->link( __d('Note', 'Manage Notes'), array( 'controller' => 'notes', 'action' => 'index', ), array('class' => 'button button-action topButton button-mobi-top') ); ?> Html->link(__d('Notes', 'Edit'), array( 'controller' => 'notes', 'action' => 'edit', $note['Note']['id'] ), array('class' => 'button button-action topButton button-mobi-top') );?>

Created:

element('likes', array('item' => $note['Note'], 'type' => $note['Note']['moo_type'])); ?>

()

    element('comments');?>
element( 'comment_form', array( 'target_id' => $note['Note']['id'], 'type' => $note['Note']['moo_type'] ) ); ?>
Verify that this is working by trying the links at **[[http://yourdomainname/|http://yourdomainname/]]****note/notes/index** or manually requesting a post by accessing **[[http://yourdomainname/|http://yourdomainname/]]****note/notes/view/1** === Add view === Create add view file in **/app/Plugin/Note/View/Notes/add.ctp**. Here’s our add view:
Form->create('Note');?>

Html->link( __d('Note', 'Manage Notes'), array( 'controller' => 'notes', 'action' => 'index', ), array('class' => 'button button-action topButton button-mobi-top') ); ?>
  • Form->input('title', array( 'label' => false, 'div' => false ));?>
  • Form->input('body', array( 'rows' => '3', 'label' => false, 'div' => false ));?>
  • Form->input('reminder', array( 'label' => false, 'div' => false ));?>
Form->end(array( 'label' => __d('Note', 'Save'), 'div' => false, 'class' => 'btn btn-action' ));?>
=== Editing view: === Create edit view file in **/app/Plugin/Note/View/Notes/edit.ctp**. Here’s what the edit view of the NotesController would look like:
Form->create('Note');?> Form->input('id', array('type' => 'hidden'));?>

Html->link( __d('Note', 'Manage Notes'), array( 'controller' => 'notes', 'action' => 'index', ), array('class' => 'button button-action topButton button-mobi-top') ); ?>
  • Form->input('title', array( 'label' => false, 'div' => false ));?>
  • Form->input('body', array( 'rows' => '3', 'label' => false, 'div' => false ));?>
Form->end(array( 'label' => __d('Note', 'Save'), 'div' => false, 'class' => 'btn btn-action' ));?>
To see result, go to url **[[http://domainname/notes|http://domainname/notes]]** ===== Creating a widget ===== Widgets were originally designed to provide a simple and easy-to-use way of giving design and structure control of the mooSocial Theme to the user, which is now available on properly "widgetized" mooSocial Themes to include the content in the mooSocial design and structure. Widgets require no code experience or expertise. They can be added, removed, and rearranged on the mooSocial Administration **Site Manager> Themes Manager> Layout Editor ** panel. ==== Create an action ==== Controller actions are responsible for converting the request parameters into a response for the browser/user making the request. MooSocial uses conventions to automate this process and remove some boilerplate code you would otherwise need to write. Update **/app/Plugin/Note/Controller/noteController.php** like this set('info', $info); } else { $this->set('info', null); } } public function index() { $this->set('notes', $this->Note->find('all')); } public function view($id = null) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } $note = $this->Note->findById($id); if (!$note) { throw new NotFoundException(__d('Note', 'Invalid note')); } $this->set('note', $note); $this->likesComments($id); } private function likesComments($id) { //comment $this->Comment = ClassRegistry::init('Comment'); $comments = $this->Comment->getComments($id, 'Note_Note'); $comment_count = $this->Comment->getCommentsCount($id, 'Note_Note'); $page = 1; $data['bIsCommentloadMore'] = $comment_count - $page * RESULTS_LIMIT; $data['comments'] = $comments; $this->set('data', $data); $this->set('comment_count', $comment_count); //like $this->Like = ClassRegistry::init('Like'); $likes = $this->Like->getLikes($id, 'Note_Note'); $dislikes = $this->Like->getDisLikes($id, 'Note_Note'); $this->set('likes', $likes); $this->set('dislikes', $dislikes); } public function add() { if ($this->request->is('post')) { $this->Note->create(); $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $this->Session->setFlash(__d('Note', 'Your note has been saved.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to add your note.')); } } public function edit($id = null) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } $note = $this->Note->findById($id); if (!$note) { throw new NotFoundException(__d('Note', 'Invalid note')); } if ($this->request->is(array('post', 'put'))) { $this->Note->id = $id; $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $this->Session->setFlash(__d('Note', 'Your note has been updated.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to update your note.')); } if (!$this->request->data) { $this->request->data = $note; } } public function delete($id) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } if ($this->Note->delete($id)) { $this->Session->setFlash( __('The note with id: %s has been deleted.', h($id)) ); return $this->redirect(array('action' => 'index')); } } public function myNotes(){ $notes = $this->Note->find('all', array( 'conditions' => array('uri' => $this->request->uri), 'limit' => 1, 'order' => array('Note.id' => 'DESC') )); return $notes; } public function ajax_add(){ if ($this->request->is('post')) { $this->Note->create(); $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $note = $this->Note->findById($this->Note->getLastInsertId()); echo json_encode(array('result' => $note)); exit; } echo json_encode(array('error' => _d("Note", "Something went wrong! Please try again."))); exit; } } } ==== Create an element ==== Many applications have small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout. MooSocial can help you repeat parts of your website that need to be reused. These reusable parts are called Elements. Ads, help boxes, navigational controls, extra menus, login forms, and callouts are often implemented in MooSocial as elements. An element is basically a mini-view that can be included in other views, in layouts, and even within other elements. Elements can be used to make a view more readable, placing the rendering of repeating elements in its own file. They can also help you re-use content fragments in your application. Create new ctp file in **/app/Plugin/Note/View/Elements/myNotes.ctp** > New in version 2.2.1> Your ctp file must be place at /app/Plugin/{plugin_name}/View/Widgets/ instead of /app/Plugin/{plugin}/View/Elements/ requestAction(array('plugin' => 'Note', 'controller' => 'notes', 'action' => 'myNotes'), array('uri' => $this->here));?>

Form->create('Note');?> Form->input('uri', array( 'value' => $this->here, 'label' => false, 'div' => false, 'type' => 'hidden' ));?>
  • Form->input('title', array( 'label' => false, 'div' => false ));?>
  • Form->input('body', array( 'rows' => '3', 'label' => false, 'div' => false, 'style' => 'overflow: hidden;width: 100%;' ));?>
Form->end(array( 'label' => __d('Note', 'Add'), 'div' => false, 'class' => 'btn btn-action', 'id' => 'btnAddNote' ));?>
Html->scriptStart(array('inline' => false)); ?> // Html->scriptEnd(); ?>
> New in version 2.2.1> You don't have to use $this→requestAction() method to get the need variables anymore e.g: . Instead, create a file in //app/Plugin/{plugin_name}/Controller/Widgets/{your_element_name}Widget.php//, for example: //app/Plugin/Note/Controller/Widgets/myNotesWidget.php// with the content like the below code: Note = MooCore::getInstance()->getModel('Note'); $notes = $this->Note->find('all', array( 'conditions' => array('uri' => $controller->request->here), 'limit' => 1, 'order' => array('Note.id' => 'DESC') )); $controller->set('notes',$notes); } ==== Create widget database ==== * Run the following sql query INSERT INTO {PREFIX}core_blocks(name, path_view,params,is_active) VALUES('My Notes', 'myNotes','[{"label":"Title","input":"text","value":"My Notes","name":"title"},{"label":"plugin","input":"hidden","value":"Note","name":"plugin"}]',1) * Go to **Home> Site Manager> Themes Manager> Layout Editor**, take a look at "Avaiable Blocks" (See image) {{:documentation:95fc9fba8f98a2fa3666374b9500fd7e.png}} * Note: before export your plugin, you need to implement sql query above into **YourPlugin/Config/install/install.txt. ** (See [[http://confluence.socialloft.com/display/Production/Creating+Your+mooSocial+First+Plugin#CreatingYourmooSocialFirstPlugin-Howtoinstallplugin|the flowchart]] for more details) ==== How to use widget ==== * Select page you want to show the widget. (See image) {{:documentation:adb6978734c0de7259f8cd30cdeac972.png}} * Drag and drop widget into the section and click Save Changes. There are three sections in a page: left, center and right. In this example we will select a page named "Blog Browse Page". In this example we will select Blogs Browse Page (See image) {{:documentation:5ea8a3a48211d1b9b7b24ceb98b4acad.png}} * At frontend go to [[http://domainname/blogs.|http://domainname/blogs.]] The page should look like this (See image) {{:documentation:78436c0dd590a1305c170224cb14ff89.png}} ====== Backend ====== ===== How to create plugin tabs ===== * Update **/app/Plugin/Note/Controller/NotesController.php** set('info', $info); } else { $this->set('info', null); } } public function index() { $this->set('notes', $this->Note->find('all')); } public function view($id = null) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } $note = $this->Note->findById($id); if (!$note) { throw new NotFoundException(__d('Note', 'Invalid note')); } $this->set('note', $note); $this->likesComments($id); } private function likesComments($id) { //comment $this->Comment = ClassRegistry::init('Comment'); $comments = $this->Comment->getComments($id, 'Note_Note'); $comment_count = $this->Comment->getCommentsCount($id, 'Note_Note'); $page = 1; $data['bIsCommentloadMore'] = $comment_count - $page * RESULTS_LIMIT; $data['comments'] = $comments; $this->set('data', $data); $this->set('comment_count', $comment_count); //like $this->Like = ClassRegistry::init('Like'); $likes = $this->Like->getLikes($id, 'Note_Note'); $dislikes = $this->Like->getDisLikes($id, 'Note_Note'); $this->set('likes', $likes); $this->set('dislikes', $dislikes); } public function add() { if ($this->request->is('post')) { $this->Note->create(); $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $this->Session->setFlash(__d('Note', 'Your note has been saved.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to add your note.')); } } public function edit($id = null) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } $note = $this->Note->findById($id); if (!$note) { throw new NotFoundException(__d('Note', 'Invalid note')); } if ($this->request->is(array('post', 'put'))) { $this->Note->id = $id; $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $this->Session->setFlash(__d('Note', 'Your note has been updated.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to update your note.')); } if (!$this->request->data) { $this->request->data = $note; } } public function delete($id) { if (!$id) { throw new NotFoundException(__d('Note', 'Invalid note')); } if ($this->Note->delete($id)) { $this->Session->setFlash( __('The note with id: %s has been deleted.', h($id)) ); return $this->redirect(array('action' => 'index')); } } public function myNotes(){ $notes = $this->Note->find('all', array( 'conditions' => array('uri' => $this->request->uri), 'limit' => 1, 'order' => array('Note.id' => 'DESC') )); return $notes; } public function ajax_add(){ if ($this->request->is('post')) { $this->Note->create(); $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { $note = $this->Note->findById($this->Note->getLastInsertId()); echo json_encode(array('result' => $note)); exit; } echo json_encode(array('error' => _d("Note", "Something went wrong! Please try again."))); exit; } } } Create admin_infos.ctp in **/app/Plugin/Note/View/Notes/** Html->css(array('jquery-ui', 'footable.core.min'), null, array('inline' => false)); echo $this->Html->script(array('jquery-ui', 'footable'), array('inline' => false)); $this->startIfEmpty('sidebar-menu'); echo $this->element('admin/adminnav', array('cmenu' => 'Note')); $this->end(); ?> Moo->renderMenu('Note', 'Infos'); ?>

name;?>

name;?>

version;?>

author;?>

website;?>

description;?>

* Update** /app/Plugin/Note/NotePlugin.php ** like this array('plugin' => 'note', 'controller' => 'notes', 'action' => 'admin_index'), 'Settings' => array('plugin' => 'note', 'controller' => 'note_settings', 'action' => 'admin_index'), 'Infos' => array('plugin' => 'note', 'controller' => 'notes', 'action' => 'admin_infos'), ); } /* Example for version 1.0: This function will be executed when plugin is upgraded (Optional) public function callback_1_0(){} */ } To see the result go to **Home> Plugins Manager> Note**, select table Infos. ===== How to add and display plugin settings ===== * Add the code below into **/app/Plugin/Note/Controller/NoteSettingsController.php** QuickSettings->run($this, array("Note"), $id); } } * Update **/app/Plugin/Note/View/NoteSettings/admin_index.php** like this Html->css(array('jquery-ui', 'footable.core.min'), null, array('inline' => false)); echo $this->Html->script(array('jquery-ui', 'footable'), array('inline' => false)); $this->startIfEmpty('sidebar-menu'); echo $this->element('admin/adminnav', array('cmenu' => 'Note')); $this->end(); ?>
Moo->renderMenu('Note', 'Settings');?>
element('admin/setting');?>
* To create setting, go to **Home> System admin> System Settings**, select tab system mode. At Production Mode select Development 1 and click Save Seetings (See image) {{:documentation:9079c2b76101d76cf51c97bd92861541.png}} * Now, the Create New Setting button appears (See image) {{:documentation:e6674457ca8046071caa652803181f84.png}} * Click button "Create New Setting" to create a new setting (See image) {{:documentation:1346ff2f0bb8f2222124cb766af50dc8.png}} * Go to **Home> Plugins Manager> Note**, select tab Settings. the result should like this (See image) {{:documentation:3012bdfa7c47f70cc7e6dd9c115cb1f7.png}} ==== What is boot setting? ==== In mooSocial, settings will be loaded 2 times: App load and plugin load. So, boot settings are loaded in the first. Why do we distinguish it? Because each plugin can have so many settings and if we always load all settings, performance becomes slow but not really necessary. How to create a boot setting? After we have created a setting, it was saved in 'settings' table. We just set '1' value for 'is_boot' field. Which case do we need? In common, almost plugin settings needn't a boot setting, but some still have one such as 'enabled' setting which makes us know whether user can access that plugin or not. Why do we need a boot setting? Because when we want to block access, we need to check enable or not in 'routes.php' file of plugin; And importantly, 'routes.php' file is loaded before the system loads unboot settings, so if we don't make 'enabled' setting by boot, we can't check it (because that setting doesn't exist at that time). > New in version 2.2** ** > In version 2.2, the "{plugin}_enabled" setting have "is_boot" field is '1' by default when you created a new plugin ==== Create boot setting ==== To create a boot setting, we have 5 steps: - Create a setting in system settings page. - Open 'settings' table in your database, find the last setting. - Set '1' value for 'is_boot' field. - Go to plugin settings page and save (because after save, 'settings.php' file will be created again). - Check 'app/Config/settings.php' file in your project, if that setting existed in, you would succeed. ===== How to add setting guide ===== * Update **/app/Plugin/Note/NotePlugin.php** like this array('plugin' => 'note', 'controller' => 'notes', 'action' => 'admin_index'), 'Settings' => array('plugin' => 'note', 'controller' => 'note_settings', 'action' => 'admin_index'), 'Infos' => array('plugin' => 'note', 'controller' => 'notes', 'action' => 'admin_infos'), ); } /* Example for version 1.0: This function will be executed when plugin is upgraded (Optional) public function callback_1_0(){} */ } * Go to **Home> Plugins Manager> Note**, select tab Settings, and see the result (See image) {{:documentation:d3493755b384d14acc536a7e8462aa36.png}} ===== Intergration ===== ==== How to interagte plugin into search ==== === Events system === * There are certain cases where you need to cleanly communicate with other parts of an application, without having to hard code dependencies, thus losing cohesion and increasing class coupling. Using the Observer pattern, which allows objects to notify other objects and anonymous listeners about changes is a useful pattern to achieve this goal. * Listeners in the observer pattern can subscribe to events and choose to act upon them if they are relevant. If you have used JavaScript, there is a good chance that you are already familiar with event driven programming. * MooSocial emulates several aspects of how events are triggered and managed in popular JavaScript libraries such as jQuery. In the MooSocial implementation, an event object is dispatched to all listeners. The event object holds information about the event, and provides the ability to stop event propagation at any point. Listeners can register themselves or can delegate this task to other objects and have the chance to alter the state and the event itself for the rest of the callbacks. * The event subsystem is at the heart of Model, Behavior, Controller, View and Helper callbacks. If you’ve ever used any of them, you are already somewhat familiar with events in MooSocial.. === Create listener === * The first important things we want to notice are table engine must be **MyISAM** and index type of searched fields must be **FULLTEXT** * Create "**NoteListener.php**" in **/app/Plugin/Note/Lib/** 'search', 'Controller.Search.suggestion' => 'suggestion', ); } public function search($event) { $e = $event->subject(); App::import('Model', 'Note.Note'); $this->Note = new Note(); $results = $this->Note->search($e->keyword); if(isset($e->plugin) && $e->plugin == 'Note') { $e->set('notes', $results); $e->render("Note.Elements/lists/notes_list"); } else { $event->result['Note']['header'] = "Notes"; $event->result['Note']['icon_class'] = "icon-group"; $event->result['Note']['view'] = "lists/notes_list"; $e->set('notes', $results); } } public function suggestion($event) { $e = $event->subject(); App::import('Model', 'Note.Note'); $this->Note = new Note(); //search with filter if(isset($event->data['type']) && $event->data['type'] == 'note') { $notes = $this->Note->search($e->keyword); $e->set('notes', $notes); $e->set('result',1); $e->set('element_list_path',"lists/notes_list"); } //search all if(isset($event->data['type']) && $event->data['type'] == 'all') { $event->result['note'] = null; $notes = $this->Note->search($e->keyword); if(!empty($notes)){ foreach($notes as $index=>&$detail){ $event->result['note'][$index]['id'] = $detail['Note']['id'];//required if(!empty($detail['Note']['thumb'])) $event->result['note'][$index]['img'] = 'notes/'.$detail['Note']['thumb'];//the path to the thumbnail in 'uploads' folder $event->result['note'][$index]['title'] = $detail['Note']['title']; //(required) $event->result['note'][$index]['find_name'] = 'Find Notes'; //(required) $event->result['note'][$index]['icon_class'] = 'icon-edit'; // icon of the note(required) $event->result['note'][$index]['view_link'] = 'videos/view/';// link to controller action when users click on suggestion results(required) } } } } } **Note**: the event name must be "**Controller.Search.search**" and "**Controller.Search.suggestion**". * Create search view file "notes_list.php" in **/app/Plugin/Note/View/Elements/lists/** * Update **/app/Plugin/Note/Model/Note.php ** like this Note.php array( 'rule' => 'notEmpty' ), 'body' => array( 'rule' => 'notEmpty' ) ); public $belongsTo = array( 'User' => array('counterCache' => true )); public $mooFields = array('title','href','plugin','type'); public function getHref($row) { if(isset($row['id'])) { $request = Router::getRequest(); return $request->base.'/notes/view/'.$row['id']; } return ''; } public function search($keyword) { $cond = array( 'MATCH(Note.title, Note.body) AGAINST(? IN BOOLEAN MODE)' => urldecode($keyword)); $notes = $this->find( 'all', array( 'conditions' => $cond, 'limit' => RESULTS_LIMIT, 'page' => 1 ) ); return $notes; } } * Update **/app/Plugin/Note/Config/bootstrap.php ** like this attach(new NoteListener()); ==== How to integrate the Note plugin in mooSocial activity feed ==== * The Activity behavior provides a way to seamlessly integrate a model with our mooSocial Activity Feed system. * To use that behavior, you can add it to the $actsAs property of your model. When adding it to the actsAs array you choose to make the related Activity entry . Update **/app/Plugin/Note/Model/Note.php** array( 'rule' => 'notEmpty' ), 'body' => array( 'rule' => 'notEmpty' ) ); public $belongsTo = array( 'User' => array('counterCache' => true )); public $mooFields = array('title','href','plugin','type'); public function getHref($row) { if(isset($row['id'])) { $request = Router::getRequest(); return $request->base.'/notes/view/'.$row['id']; } return ''; } public function search($keyword) { $cond = array( 'MATCH(Note.title, Note.body) AGAINST(? IN BOOLEAN MODE)' => urldecode($keyword)); $notes = $this->find( 'all', array( 'conditions' => $cond, 'limit' => RESULTS_LIMIT, 'page' => 1 ) ); return $notes; } public $actsAs = array( 'Activity' => array( 'type' => APP_USER, 'action_afterCreated'=>'note_feed', 'item_type'=>'Note_Note', 'query'=>1, 'params' => 'item' ), ); } * Create a structure below into **Note/View/Elements/** {{:documentation:7b23fde8904f3361c2bd759f493f1c5b.png}} * Copy the code below into content/note_feed.ctp
Text->truncate( strip_tags( str_replace( array('
',' '), array(' ',''), $activity['Content']['Note']['body'] ) ), 160 , array('exact' => false))?>
* Copy the code below into text/note_feed.ctp * To test activity feed go to **[[http://domainname/blogs|http://domainname/blogs]]** and add a new note {{:documentation:feb3ba6f34084150bf1c038b81a50fe8.png}} * To see new feed go to **[[http://domainname/home|http://domainname/home]]** {{:documentation:a59705393ec2d0c1d460cb50d2443fbe.png}} ===== How to export plugin ===== * Copy the sql query below into **/app/Plugin/Note/Config/install/install.sql** * CREATE TABLE IF NOT EXISTS {PREFIX}notes ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id INT(11) NOT NULL, title VARCHAR(50), body TEXT, uri VARCHAR(150), created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL, like_count INT(11) DEFAULT '0', dislike_count INT(11) DEFAULT '0', comment_count INT(11) DEFAULT '0', FULLTEXT KEY title (title,body) )ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO {PREFIX}notes (user_id,title,body,created,modified,uri) VALUES ('1', 'The title', 'This is the post body.', NOW(), NOW(),'uri_1'), ('1', 'A title once again', 'And the post body follows.', NOW(), NOW(),'uri_2'), ('1', 'Title strikes back', 'This is really exciting! Not.', NOW(), NOW(),'uri_3'); INSERT INTO core_blocks(name, path_view,params,is_active) VALUES('My Notes', 'myNotes','[{"label":"Title","input":"text","value":"MyNotes","name":"title"},{"label":"plugin","input":"hidden","value":"Note","name":"plugin"}]',1); * Copy the sql query below into **/app/Plugin/Note/Config/install/uninstall.sql** DROP TABLE IF EXISTS {PREFIX}notes; DELETE FROM {PREFIX}core_blocks WHERE path_view = 'myNotes'; DELETE FROM {PREFIX}core_contents WHERE name = 'myNotes'; DELETE FROM {PREFIX}activities WHERE action = 'note_feed'; * To export the plugin, go to **Home> Plugins Manager> Manage**, click download icon to export (See image) {{:documentation:15a4370a64c6bf42195c72b4548d7afc.png}} ===== How to uninstall plugin ===== * To uninstall the plugin, go to **Home> Plugins Manager> Manage**, click recycle bin icon to uninstall (See image) {{:documentation:a067f47aeb4ac73189b728ae67051595.png}} * The basic uninsatllation process for mooSocial Plugin is illustrated below. {{:documentation:d51bcd3809d6b7d7e19440c6db846d8f.png}} * Step 1: Validate uninstallation * Check plugin exists: make sure plugin exists before uninstallation. * Step 2: Install plugin * Create plugin informations: Read plugin informations from **YourPlugin/info.xml** and store into database. * Create plugin settings (Optional): Read setting informations from **YourPlugin/info.xml** and store into database. * Register plugin: register plugin into **Config/plugins/plugins.xml**. * Install database (Optional): execute sql query in **YourPlugin/Config/install/install.txt** (sql query maybe: create new table, insert new widget etc). * Execute install function (Optional): execute install function from** YourPlugin/plugin.php** * Step 3:Set up and configure extended functionality * Load menu tabs: as default, there are twoo tabs: General and Settings. You can add, edit or remove tabs in function menu from **YourPlugin/plugin.php**. * View setting guide: if you want to have a text to guide users how to use your settings. Just add it in function settingGuide from **YourPlugin/plugin.php**. ===== How to install plugin ===== * Copy plugin source code into /app/Plugin. * Go to **Home> Plugins Manager> Manage**, select tab "not uninstalled plugins" and click install (See image). {{:documentation:98989722cb0e7f1bcbfdedf9e8252291.png}} * The basic installation process for mooSocial Plugin is illustrated below. {{:documentation:da5b1864a7ebe7c48df52c731b74ce75.png}} * Step 1: Validate installation * Check informations from **YourPlugin/info.xml**: make sure plugin informations are valid. * Step 2: Install plugin * Create plugin informations: Read plugin informations from **YourPlugin/info.xml** and store into database. * Create plugin settings (Optional): Read setting informations from **YourPlugin/info.xml** and store into database. * Register plugin: register plugin into **Config/plugins/plugins.xml**. * Install database (Optional): execute sql query in **YourPlugin/Config/install/install.txt** (sql query maybe: create new table, insert new widget etc). * Execute install function (Optional): execute install function from** YourPlugin/plugin.php** * Step 3:Set up and configure extended functionality * Load menu tabs: as default, there are twoo tabs: General and Settings. You can add, edit or remove tabs in function menu from **YourPlugin/plugin.php**. * View setting guide: if you want to have a text to guide users how to use your settings. Just add it in function settingGuide from **YourPlugin/plugin.php**. ===== How to upgrade plugin ===== ==== Create upgrade version ==== * The first important thing to create an upgrade version for plugin is change the version number between tag in "**/app/Plugin/Note/info.xml**". In this example we will the version to 2.0. Note Note 2.0 Let's begin to a Note plugin MooTechnicalTeam http://community.socialloft.com/users/view/133 1 1 * If your upgrade need to execute sql query, go to **/app/Plugin/Note/Config/install/upgrade.xml** and update xml structure like this 2.0 INSERT INTO {PREFIX}notes (user_id,title,body,created,modified,uri) VALUES ('1', 'The title 2.0', 'This is the post body.', NOW(), NOW(),'uri_4'); INSERT INTO {PREFIX}notes (user_id,title,body,created,modified,uri) VALUES ('1', 'The title 2.0', 'This is the post body.', NOW(), NOW(),'uri_5'); Note: You must implement new sql query into **/app/Plugin/Note/Config/install/install.sql** ==== Upgrade plugin ==== * Copy and replace current plugin source code with new source code. * Click upgrade icon to upgrade (See image) {{:documentation:a067f47aeb4ac73189b728ae67051595.png}} * The basic upgrade process for mooSocial Plugin is illustrated below. {{:documentation:4c1298b78a1d55de85bf44fc13a80707.png}} * Step 1: Validate upgrade * Check plugin exists: make sure plugin exists before uninstallation. * Check new version: make sure there is new version before upgrade. * Step 2: Upgrade plugin * Install database (Optional): execute sql query in **YourPlugin/Config/install/upgrade.xml** (sql query maybe: create new table, insert new widget etc). * Execute extended functions (Optional): base on version number, process will execute function named like callback_1_0 in **YourPlugin/plugin.php.** * Step 3:Set up and configure extended functionality * Load menu tabs: as default, there are twoo tabs: General and Settings. You can add, edit or remove tabs in function menu from **YourPlugin/plugin.php**. * View setting guide: if you want to have a text to guide users how to use your settings. Just add it in function settingGuide from **YourPlugin/plugin.php**. ===== Internationalizing Your Plugin ===== ===== How to create a task ===== Insert to install.sql INSERT INTO `{PREFIX}tasks` (`title`, `plugin`, `timeout`, `processes`, `semaphore`, `started_last`, `started_count`, `completed_last`, `completed_count`, `failure_last`, `failure_count`, `success_last`, `success_count`, `enable`, `class`) VALUES\\ ('Reminder', 'note', 300, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 'Note_Task_Reminder'); Insert to uninstall.sql DELETE FROM {PREFIX}tasks\\ WHERE `class` = 'Note_Task_Reminder'; Create NoteTaskReminder.php in /app/Plugin/Note/Task/ getModel('Note_Note'); $notes = $noteModel->find('all',array('conditions'=>array( 'check_reminder' => false, 'reminder>' => 0, '(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(Note.created)> Note.reminder * 3600)' ))); if (count($notes)) { $mailComponent = MooCore::getInstance()->getComponent('Mail.MooMail'); $ssl_mode = Configure::read('core.ssl_mode'); $http = (!empty($ssl_mode)) ? 'https' : 'http'; foreach ($notes as $note) { $mailComponent->send($note['Note']['user_id'],'note_reminder', array( 'note_name' => $note['Note']['moo_title'], 'note_link' => $http.'://'.$_SERVER['SERVER_NAME'].$note['Note']['moo_href'], ) ); $noteModel->id = $note['Note']['id']; $noteModel->save(array('check_reminder'=>true)); } } } } * You can manage task on Admin ACP **Home> System Admin> Tasks** ===== How to using mailsystem ===== Insert to NotePlugin.php on function install and uninstall public function install(){ $mailModel = MooCore::getInstance()->getModel('Mail_Mailtemplate'); $languageModel = MooCore::getInstance()->getModel('Language'); $langs = $languageModel->find('all'); $data['Mailtemplate'] = array( 'type' => 'note_reminder', 'plugin' => 'Note', 'vars' => '[note_name],[note_link]' ); $mailModel->save($data); $id = $mailModel->id; foreach ($langs as $lang) { $language = $lang['Language']['key']; $mailModel->locale = $language; $data_translate['subject'] = 'Note reminder'; $content = <<[header]

This is mail reminder : [note_name]

[footer]

EOF; $data_translate['content'] = $content; $mailModel->save($data_translate); } } public function uninstall(){ $mailModel = MooCore::getInstance()->getModel('Mail_Mailtemplate'); $mailModel->deleteAll(array('Mailtemplate.type'=>'note_reminder'),true,true); }
How to using it $mailComponent→send($mix,$type,$params) $mix: email or $user_id or model user, $type: type of email, $params: param for mail type. $noteModel = MooCore::getInstance()->getModel('Note_Note'); $notes = $noteModel->find('all',array('conditions'=>array( 'check_reminder' => false, 'reminder>' => 0, '(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(Note.created)> Note.reminder * 3600)' ))); if (count($notes)) { $mailComponent = MooCore::getInstance()->getComponent('Mail.MooMail'); $ssl_mode = Configure::read('core.ssl_mode'); $http = (!empty($ssl_mode)) ? 'https' : 'http'; foreach ($notes as $note) { $mailComponent->send($note['Note']['user_id'],'note_reminder', array( 'note_name' => $note['Note']['moo_title'], 'note_link' => $http.'://'.$_SERVER['SERVER_NAME'].$note['Note']['moo_href'], ) ); $noteModel->id = $note['Note']['id']; $noteModel->save(array('check_reminder'=>true)); } } ===== How to using tag system ===== When create a note: if ($this->Note->save($this->request->data)) { //Tag system $this->loadModel('Tag'); $this->Tag->saveTags($this->request->data['Note']['tags'], $this->Note->id, 'Note_Note'); // $this->Session->setFlash(__d('Note', 'Your note has been saved.')); return $this->redirect(array('action' => 'index')); } //Tag system
Form->text('tags'); ?> (?)
//
When edit a note: NotesController.php if ($this->request->is(array('post', 'put'))) { $this->Note->id = $id; $this->request->data['Note']['user_id'] = $this->Session->read('uid'); if ($this->Note->save($this->request->data)) { //Tag system $this->Tag->saveTags($this->request->data['Note']['tags'], $id, 'Note_Note'); // $this->Session->setFlash(__d('Note', 'Your note has been updated.')); return $this->redirect(array('action' => 'index')); } $this->Session->setFlash(__d('Note', 'Unable to update your note.')); } //Tag system $tags = $this->Tag->getContentTags($id, 'Note_Note'); $this->set('tags', $tags); // //Tag system
  • Form->text('tags', array('value' => $tags_value)); ?> (?)
  • //
    Helper hook //Tag system public function getTagUnionsNote($noteids) { return "SELECT i.id, i.title, i.body, i.like_count, i.created, 'Note_Note' as moo_type FROM " . Configure::read('core.prefix') . "notes i WHERE i.id IN (" . implode(',', $noteids) . ") AND i.privacy = ".PRIVACY_EVERYONE; } public function getImage($item,$options) { return $this->assetUrl('Note.noimage/note.png',$options + array('pathPrefix' => Configure::read('App.imageBaseUrl'))); return $url; } // When view note //Tag system $this->loadModel('Tag'); $tags = $this->Tag->getContentTags($id, 'Note_Note'); $this->set('tags', $tags); // //Tag system
    element( 'blocks/tags_item_block' ); ?>
    //
    ===== How to using like system ===== View note //Like system MooCore::getInstance()->setSubject($note); // //Like system
    renderLike();?>
    //
    ===== How to using comment system ===== View note //Comment system MooCore::getInstance()->setSubject($note); // //Comment system
    renderComment();?>
    //
    //Comment system public function checkPostStatus($note,$uid) { if (!$uid) return false; $friendModel = MooCore::getInstance()->getModel('Friend'); if ($uid == $note['Note']['user_id']) return true; if ($note['Note']['privacy'] == PRIVACY_EVERYONE) { return true; } if ($note['Note']['privacy'] == PRIVACY_FRIENDS) { $areFriends = $friendModel->areFriends( $uid, $note['Blog']['user_id'] ); if ($areFriends) return true; } return false; } public function checkSeeActivity($note,$uid) { return $this->checkPostStatus($note,$uid); } // ===== How to using report system ===== View note: //Report system Html->link( __d('Note', 'Report Note'), array( 'controller' => 'reports', 'action' => 'ajax_create', 'plugin' => '', $note['Note']['moo_type'], $note['Note']['id'] ), array('data-target'=>'#themeModal','class'=>'button button-action topButton button-mobi-top','data-toggle'=>'modal') ); // ====== Plugin Development Suggestions ====== * Do not hardcode the mooSocial database table prefix into your Plugins . * Use the existing database tables instead of creating new custom tables if possible. * SELECT only what you need.Naming conventions are very important in CakePHP. By naming our model Post, CakePHP can automatically infer that this model will be used in the PostsController, and will be tied to a database table called posts. ====== Themed Plugin ====== path **app/View/Themed/[ThemeName]/Plugin/[PluginName]/** {{:documentation:23fb6cd688d81fcb558c8984a443b870.jpg}}