/**
  * Constructor - basic setup
  *
  * @param int $pagemenuid Page menu instance ID
  * @param array $links Page menu link records that belong to this page menu
  * @param array $data Link data for the links
  * @param int $firstlinkid First link ID
  * @return void
  **/
 public function __construct($pagemenuid, $links = NULL, $data = NULL, $firstlinkid = false)
 {
     $this->pagemenuid = $pagemenuid;
     if ($links === NULL) {
         $links = get_records('pagemenu_links', 'pagemenuid', $this->pagemenuid);
     }
     if (!$firstlinkid) {
         $firstlinkid = pagemenu_get_first_linkid($this->pagemenuid);
     }
     if ($data === NULL) {
         if (!empty($links)) {
             $data = pagemenu_get_link_data($links);
         } else {
             $data = array();
         }
     }
     if (!empty($links) and !empty($firstlinkid)) {
         $linkid = $firstlinkid;
         while ($linkid) {
             if (array_key_exists($linkid, $data)) {
                 $datum = $data[$linkid];
             } else {
                 $datum = NULL;
             }
             $link = $links[$linkid];
             $linkid = $link->nextid;
             $this->links[$link->id] = mod_pagemenu_link::factory($link->type, $link, $datum);
         }
     }
     $this->init();
 }
示例#2
0
/**
 * Backup all links for a given pagemenu instance
 *
 * @param object $bf Backup file
 * @param object $preferences Backup preferences
 * @param object $pagemenu A full pagemenu record object
 * @return boolean
 **/
function backup_pagemenu_links($bf, $preferences, $pagemenu)
{
    static $loaded = false;
    if (!$loaded) {
        global $CFG;
        // Backup routine has enough performance problems - only call this once
        require_once $CFG->dirroot . '/mod/pagemenu/locallib.php';
        $loaded = true;
    }
    $status = true;
    // Backup links in order - makes restore much more pleasant
    if ($linkid = pagemenu_get_first_linkid($pagemenu->id) and $links = get_records('pagemenu_links', 'pagemenuid', $pagemenu->id)) {
        fwrite($bf, start_tag('LINKS', 4, true));
        while ($linkid) {
            $link = $links[$linkid];
            fwrite($bf, start_tag('LINK', 5, true));
            fwrite($bf, full_tag('ID', 6, false, $link->id));
            fwrite($bf, full_tag('PREVID', 6, false, $link->previd));
            fwrite($bf, full_tag('NEXTID', 6, false, $link->nextid));
            fwrite($bf, full_tag('TYPE', 6, false, $link->type));
            if (!($status = backup_pagemenu_link_data($bf, $preferences, $link))) {
                debugging('Failed to backup link data');
                break;
            }
            fwrite($bf, end_tag('LINK', 5, true));
            $linkid = $link->nextid;
        }
        if ($status) {
            $status = fwrite($bf, end_tag('LINKS', 4, true));
        }
    }
    return $status;
}
/**
 * Move a link to a new position in the ordering
 *
 * @param object $pagemenu Page menu instance
 * @param int $linkid ID of the link we are moving
 * @param int $after ID of the link we are moving our link after (can be 0)
 * @return boolean
 **/
function pagemenu_move_link($pagemenu, $linkid, $after)
{
    $link = new stdClass();
    $link->id = $linkid;
    // Remove the link from where it was (Critical: this first!)
    pagemenu_remove_link_from_ordering($link->id);
    if ($after == 0) {
        // Adding to front - get the first link
        if (!($firstid = pagemenu_get_first_linkid($pagemenu->id))) {
            error('Could not find first link ID');
        }
        // Point the first link back to our new front link
        if (!set_field('pagemenu_links', 'previd', $link->id, 'id', $firstid)) {
            error('Failed to update link ordering');
        }
        // Set prev/next
        $link->nextid = $firstid;
        $link->previd = 0;
    } else {
        // Get the after link
        if (!($after = get_record('pagemenu_links', 'id', $after))) {
            error('Invalid Link ID');
        }
        // Point the after link to our new link
        if (!set_field('pagemenu_links', 'nextid', $link->id, 'id', $after->id)) {
            error('Failed to update link ordering');
        }
        // Set the next link in the ordering to look back correctly
        if ($after->nextid) {
            if (!set_field('pagemenu_links', 'previd', $link->id, 'id', $after->nextid)) {
                error('Failed to update link ordering');
            }
        }
        // Set next/prev
        $link->previd = $after->id;
        $link->nextid = $after->nextid;
    }
    if (!update_record('pagemenu_links', $link)) {
        error('Failed to update link');
    }
    return true;
}
示例#4
0
/**
 * Generates a menu
 *
 * @param int $pagemenuid ID of the instance to print
 * @param boolean $editing True if your currently editing the menu
 * @param boolean $yui Turn YUI Menu support On/Off - If On, then extra divs and classes will be added and full trees are printed
 * @param boolean $menuinfo True, returns menu information object.  False, return menu HTML
 * @param array $links All of the links used by this menu
 * @param array $data All of the data for the links used by this menu
 * @param array $firstlinkids This is an array of IDs that are the first link for a pagemenu.  Array keys are pagemenu IDs.
 * @return mixed
 **/
function pagemenu_build_menu($pagemenuid, $editing = false, $yui = false, $menuinfo = false, $links = NULL, $data = NULL, $firstlinkids = array())
{
    global $CFG;
    $info = new stdClass();
    $info->html = '';
    $info->menuitems = array();
    $info->active = false;
    // Set links if not already passed
    if ($links === NULL) {
        $links = get_records('pagemenu_links', 'pagemenuid', $pagemenuid);
    }
    // Check passed array first, otherwise go to DB
    if (array_key_exists($pagemenuid, $firstlinkids)) {
        $linkid = $firstlinkids[$pagemenuid];
    } else {
        $linkid = pagemenu_get_first_linkid($pagemenuid);
    }
    if (!empty($links) and !empty($linkid)) {
        // Get all link config data if we don't have it already
        if ($data === NULL) {
            $data = pagemenu_get_link_data($links);
        }
        if ($editing) {
            $action = optional_param('action', '', PARAM_ALPHA);
            if ($action == 'move') {
                $moveid = required_param('linkid', PARAM_INT);
                $alt = s(get_string('movehere'));
                $movewidget = "<a title=\"{$alt}\" href=\"{$CFG->wwwroot}/mod/pagemenu/edit.php?a={$pagemenuid}&amp;action=movehere&amp;linkid={$moveid}&amp;sesskey=" . sesskey() . '&amp;after=%d">' . "<img src=\"{$CFG->pixpath}/movehere.gif\" border=\"0\" alt=\"{$alt}\" /></a>";
                $move = true;
            } else {
                $move = false;
            }
            $table = new stdClass();
            $table->id = 'edit-table';
            $table->width = '90%';
            $table->tablealign = 'center';
            $table->cellpadding = '5px';
            $table->cellspacing = '0';
            $table->data = array();
            if ($move) {
                $table->head = array(get_string('movingcancel', 'pagemenu', "{$CFG->wwwroot}/mod/pagemenu/edit.php?a={$pagemenuid}"));
                $table->wrap = array('nowrap');
                $table->data[] = array(sprintf($movewidget, 0));
            } else {
                $table->head = array(get_string('linktype', 'pagemenu'), get_string('actions', 'pagemenu'), get_string('rendered', 'pagemenu'));
                $table->align = array('left', 'center', '');
                $table->size = array('*', '*', '100%');
                $table->wrap = array('nowrap', 'nowrap', 'nowrap');
            }
        }
        while ($linkid) {
            if (array_key_exists($linkid, $data)) {
                $datum = $data[$linkid];
            } else {
                $datum = NULL;
            }
            $link = $links[$linkid];
            $linkid = $link->nextid;
            $link = mod_pagemenu_link::factory($link->type, $link, $datum);
            $menuitem = $link->get_menuitem($editing, $yui);
            // Update info
            if ($link->active) {
                $info->active = true;
            }
            if ($menuitem) {
                $info->menuitems[] = $menuitem;
            }
            if ($editing) {
                if (!$menuitem) {
                    $html = get_string('linkitemerror', 'pagemenu');
                } else {
                    $html = pagemenu_menuitems_to_html(array($menuitem));
                }
                if ($move) {
                    if ($moveid != $link->link->id) {
                        $table->data[] = array($html);
                        $table->data[] = array(sprintf($movewidget, $link->link->id));
                    }
                } else {
                    $widgets = array();
                    foreach (array('move', 'edit', 'delete') as $widget) {
                        $alt = s(get_string($widget));
                        $widgets[] = "<a title=\"{$alt}\" href=\"{$CFG->wwwroot}/mod/pagemenu/edit.php?a={$pagemenuid}&amp;action={$widget}&amp;linkid={$link->link->id}&amp;sesskey=" . sesskey() . '">' . "<img src=\"{$CFG->pixpath}/t/{$widget}.gif\" height=\"11\" width=\"11\" border=\"0\" alt=\"{$alt}\" /></a>";
                    }
                    $table->data[] = array($link->get_name(), implode('&nbsp;', $widgets), $html);
                }
            }
        }
        if ($editing) {
            $info->html = print_table($table, true);
        } else {
            $info->html = pagemenu_menuitems_to_html($info->menuitems, 0, $yui);
        }
    } else {
        $info->html = print_box(get_string('nolinksinmenu', 'pagemenu'), 'generalbox boxaligncenter boxwidthnarrow centerpara', 'pagemenu-empty', true);
    }
    if ($menuinfo) {
        return $info;
    }
    return $info->html;
}