Changeset 13408

Show
Ignore:
Timestamp:
11/13/07 13:22:29 (1 year ago)
Author:
adrenalin
Message:

Sorting uses now jQuery instead of ProtoType? and Scriptaculous. It's
now possible to sort all the navigation items that have a GUID.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/midcom.admin.folder/documentation/CHANGES

    r12902 r13408  
    66  which might even break existing functionality. 
    77- All items marked with "+" represent completly new features. 
     8 
     92007-11-13 adrenalin 
     10  ! Sorting uses now jQuery instead of ProtoType and Scriptaculous 
     11  ! It's now possible to sort all the navigation items that have a GUID 
    812 
    9132007-10-19 adrenalin 
  • trunk/midcom/midcom.admin.folder/handler/order.php

    r13110 r13408  
    3131     *  
    3232     * @access private 
    33      */ 
    34     private function _process_order_form() 
    35     { 
    36         // If the navigation order is changed, it will be saved first. After this it is possible 
    37         // again to organize the folder 
    38         if ($_POST['f_navorder'] != $this->_topic->parameter('midcom.helper.nav', 'navorder')) 
    39         { 
    40             $this->_topic->set_parameter('midcom.helper.nav', 'navorder', $_POST['f_navorder']); 
     33     * @return boolean Indicating success 
     34     */ 
     35    function _process_order_form() 
     36    { 
     37        if (isset($_POST['f_navorder'])) 
     38        { 
     39            $this->_topic->set_parameter('midcom.helper.nav', 'nav_order', $_POST['f_navorder']); 
     40        } 
     41         
     42        // Form has been handled if cancel has been pressed 
     43        if (isset($_POST['f_cancel'])) 
     44        { 
     45            $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('midcom.admin.folder'), $_MIDCOM->i18n->get_string('cancelled')); 
     46            $_MIDCOM->relocate($_MIDCOM->permalinks->create_permalink($this->_topic->guid)); 
     47            exit; 
     48            // This will exit 
     49        } 
     50         
     51        // If the actual score list hasn't been posted, return false 
     52        if (!isset($_POST['f_submit'])) 
     53        { 
    4154            return false; 
    42             // This will exit. 
    43         } 
    44          
    45         if (array_key_exists('midcom_admin_content_page_score', $_POST)) 
    46         { 
    47             $count = count($_POST['midcom_admin_content_page_score']); 
     55        } 
     56         
     57        // Success tells whether the update was successful or not. On default everything goes fine, 
     58        // but when any errors are encountered, there will be a uimessage that will be shown. 
     59        $success = true; 
     60         
     61        // Loop through the sortables and store the new score 
     62        foreach ($_POST['sortable'] as $key => $array) 
     63        { 
     64            // Total number of the entries 
     65            $count = count($array); 
    4866             
    49             foreach ($_POST['midcom_admin_content_page_score'] as $key => $id
     67            foreach ($array as $guid => $i
    5068            { 
    51                 $article = new midcom_db_article($id); 
    52                 $article->score = (int) $key; 
    53                 $article->metadata->score = (int) $count - $key; 
     69                // Set the score reversed: the higher the value, the higher the rank 
     70                $score = $count - $i; 
     71                 
     72                // Use the DB Factory to resolve the class and to get the object 
     73                $object = $_MIDCOM->dbfactory->get_object_by_guid($guid); 
     74                 
     75                // Skip the pages that cannot be ordered 
     76                if (   !$object 
     77                    || !isset($object->guid) 
     78                    || $object->guid !== $guid) 
     79                { 
     80                    continue; 
     81                } 
    5482                 
    5583                // Get the original approval status 
    56                 $metadata =& midcom_helper_metadata::retrieve($article); 
     84                $metadata =& midcom_helper_metadata::retrieve($guid); 
    5785                $approval_status = false; 
    5886                 
     
    6492                } 
    6593                 
    66                 if (!$article->update()) 
    67                 { 
    68                     debug_push_class(__CLASS__, __FUNCTION__); 
    69                     debug_add("Updating the article with id '{$id}' failed. Reason: ". mgd_errstr(), MIDCOM_LOG_ERROR); 
    70                     debug_pop(); 
     94                // Store the old-fashioned score as well 
     95                if (isset($object->score)) 
     96                { 
     97                    $object->score = $score; 
     98                } 
     99                 
     100                $object->metadata->score = $score; 
     101                 
     102                // Show an error message on an update failure 
     103                if (!$object->update()) 
     104                { 
     105                    // Some heuristics for the update logging 
     106                    if (   isset($object->title) 
     107                        && $object->title) 
     108                    { 
     109                        $title = $object->title; 
     110                    } 
     111                    elseif (isset($object->extra) 
     112                        && $object->extra) 
     113                    { 
     114                        $title = $object->extra; 
     115                    } 
     116                    elseif (isset($object->name) 
     117                        && $object->name) 
     118                    { 
     119                        $title = $object->name; 
     120                    } 
     121                    else 
     122                    { 
     123                        $title = sprintf("{$object->guid} %s", get_class($object)); 
     124                    } 
    71125                     
    72                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Saving the order failed, see error level log for details'); 
    73                     // This will exit 
    74                 } 
    75                  
    76                 // Maintain the approval status - if the object had been approved before 
    77                 // it should still be kept as approved 
    78                 if ($approval_status) 
    79                 { 
    80                     $metadata =& midcom_helper_metadata::retrieve($article); 
     126                    $_MIDCOM->uimessages->add($this->_l10n->get('midcom.admin.folder'), sprintf($this->_l10n->get('failed to update %s due to: %s'), $title, mgd_errstr()), 'error'); 
     127                    $success = false; 
     128                    continue; 
     129                } 
     130                 
     131                // Approve if possible 
     132                if (   $approval_status 
     133                    && $object->can_do('midgard:approve')) 
     134                { 
     135                    $metadata =& midcom_helper_metadata::retrieve($guid); 
    81136                    $metadata->approve(); 
    82137                } 
     
    84139        } 
    85140         
    86         if (array_key_exists('midcom_admin_content_folder_score', $_POST)) 
    87         { 
    88             $count = count($_POST['midcom_admin_content_folder_score']); 
    89              
    90             foreach ($_POST['midcom_admin_content_folder_score'] as $key => $id) 
    91             { 
    92                 $topic = new midcom_db_topic($id); 
    93                 $topic->score = (int) $key; 
    94                 $topic->metadata->score = (int) $count - $key; 
    95                  
    96                 // Get the original approval status 
    97                 $metadata =& midcom_helper_metadata::retrieve($topic); 
    98                 $approval_status = false; 
    99                  
    100                 // Get the approval status if metadata object is available 
    101                 if (   is_object($metadata) 
    102                     && $metadata->is_approved()) 
    103                 { 
    104                     $approval_status = true; 
    105                 } 
    106                  
    107                 if (!$topic->update()) 
    108                 { 
    109                     debug_push_class(__CLASS__, __FUNCTION__); 
    110                     debug_add("Updating the topic with id '{$id}' failed. Reason: ". mgd_errstr(), MIDCOM_LOG_ERROR); 
    111                     debug_pop(); 
    112                      
    113                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Saving the order failed, see error level log for details'); 
    114                     // This will exit 
    115                 } 
    116                  
    117                 // Maintain the approval status - if the object had been approved before 
    118                 // it should still be kept as approved 
    119                 if ($approval_status) 
    120                 { 
    121                     $metadata =& midcom_helper_metadata::retrieve($topic); 
    122                     $metadata->approve(); 
    123                 } 
    124             } 
    125         } 
    126          
    127         if (array_key_exists('midcom_admin_content_mixed_score', $_POST)) 
    128         { 
    129             $count = count($_POST['midcom_admin_content_mixed_score']); 
    130              
    131             foreach ($_POST['midcom_admin_content_mixed_score'] as $key => $id) 
    132             { 
    133                 $type = explode('_', $id); 
    134                 if ($type[2] === 'folder') 
    135                 { 
    136                     $object = new midcom_db_topic($type[1]); 
    137                 } 
    138                 else 
    139                 { 
    140                     $object = new midcom_db_article($type[1]); 
    141                 } 
    142                  
    143                 if (!is_object($object)) 
    144                 { 
    145                     debug_push_class(__CLASS__, __FUNCTION__); 
    146                     debug_add("Could not get the {$type[1]} with id '{$id}'. Reason: ". mgd_errstr(), MIDCOM_LOG_ERROR); 
    147                     debug_pop(); 
    148                      
    149                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Saving the order failed, see error level log for details'); 
    150                 } 
    151                  
    152                 // Get the original approval status 
    153                 $metadata =& midcom_helper_metadata::retrieve($object); 
    154                 $approval_status = false; 
    155                  
    156                 $object->metadata->score = (int) $count - $key; 
    157                  
    158                 // Get the approval status if metadata object is available 
    159                 if (   is_object($metadata) 
    160                     && $metadata->is_approved()) 
    161                 { 
    162                     $approval_status = true; 
    163                 } 
    164                  
    165                 $object->score = (int) $key; 
    166                  
    167                 if (!$object->update()) 
    168                 { 
    169                     debug_push_class(__CLASS__, __FUNCTION__); 
    170                     debug_add("Updating the {$type[1]} with id '{$id}' failed. Reason: ". mgd_errstr(), MIDCOM_LOG_ERROR); 
    171                     debug_pop(); 
    172                      
    173                     $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Saving the order failed, see error level log for details'); 
    174                     // This will exit 
    175                 } 
    176                  
    177                 // Maintain the approval status - if the object had been approved before 
    178                 // it should still be kept as approved 
    179                 if ($approval_status) 
    180                 { 
    181                     $metadata =& midcom_helper_metadata::retrieve($object); 
    182                     $metadata->approve(); 
    183                 } 
    184             } 
    185         } 
    186          
    187         return true; 
     141        if ($success) 
     142        { 
     143            $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('midcom.admin.folder'), $_MIDCOM->i18n->get_string('order saved')); 
     144            $_MIDCOM->relocate($_MIDCOM->permalinks->create_permalink($this->_topic->guid)); 
     145            exit; 
     146            // This will exit 
     147        } 
    188148    } 
    189149     
     
    193153    function _handler_order($handler_id, $args, &$data) 
    194154    { 
    195         // Include Scriptaculous JavaScript library to headers 
    196         // Scriptaculous/scriptaculous.js 
    197         $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/Pearified/JavaScript/Prototype/prototype.js'); 
    198         $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/Pearified/JavaScript/Scriptaculous/scriptaculous.js'); 
     155        // Include Scriptaculous JavaScript libraries to headers 
     156        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/jQuery/jquery.dimensions-1.1.2.js'); 
     157        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/jQuery/jquery.form-1.0.3.js'); 
     158        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/jQuery/ui/ui.mouse.js'); 
     159        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/jQuery/ui/ui.draggable.js'); 
     160        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/jQuery/ui/ui.droppable.js'); 
     161        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/jQuery/ui/ui.sortable.js'); 
     162        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL.'/midcom.admin.folder/jquery-postfix.js'); 
     163         
     164        // These pages need no caching 
     165        $_MIDCOM->cache->content->no_cache(); 
     166         
     167        // Custom styles 
    199168        $_MIDCOM->add_link_head 
    200169        ( 
     
    209178        $this->_topic->require_do('midgard:update'); 
    210179         
    211         if (array_key_exists('f_cancel', $_REQUEST)) 
    212         { 
    213             $_MIDCOM->relocate($_MIDCOM->permalinks->create_permalink($this->_topic->guid)); 
    214             // This will exit 
    215         } 
    216          
    217         if (array_key_exists('f_submit', $_REQUEST)) 
    218         { 
    219             if ($this->_process_order_form()) 
    220             { 
    221                 $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('midcom.admin.folder'), $_MIDCOM->i18n->get_string('order saved')); 
    222                 $_MIDCOM->relocate($_MIDCOM->permalinks->create_permalink($this->_topic->guid)); 
    223                 // This will exit 
    224             } 
    225         } 
     180        // Process the form 
     181        $this->_process_order_form(); 
    226182         
    227183        // Add the view to breadcrumb trail 
     
    249205        $_MIDCOM->style->prepend_component_styledir('midcom.admin.folder'); 
    250206         
     207        // Skip the page style on AJAX form handling 
     208        if (isset($_GET['ajax'])) 
     209        { 
     210            $_MIDCOM->skip_page_style = true; 
     211        } 
     212         
    251213        return true; 
    252214    } 
     
    259221    function _show_order($handler_id, &$data) 
    260222    { 
    261         $this->_request_data['navorder'] = (int) $this->_topic->parameter('midcom.helper.nav', 'navorder'); 
    262          
    263         $this->_request_data['navorder_list'] = array 
     223        $data['navigation'] = array(); 
     224        $data['navorder'] = $this->_topic->get_parameter('midcom.helper.nav', 'nav_order'); 
     225         
     226        // Navorder list for the selection 
     227        $data['navorder_list'] = array 
    264228        ( 
    265229            MIDCOM_NAVORDER_DEFAULT => $_MIDCOM->i18n->get_string('default sort order', 'midcom.admin.folder'), 
     
    269233        ); 
    270234         
    271         $this->_request_data['sort_order_header'] = $this->_request_data['navorder_list'][$this->_request_data['navorder']]; 
    272  
    273         $qb = midcom_db_topic::new_query_builder(); 
    274         $qb->add_constraint('up', '=', $this->_topic->id); 
    275         $qb->add_constraint('metadata.navnoentry', '=', 0); 
    276         $qb->add_order('metadata.score', 'DESC'); 
    277         //$qb->add_order('name'); 
    278         //$qb->add_order('extra'); 
    279          
    280         $this->_request_data['folders'] = $qb->execute(); 
    281          
    282         $qb = midcom_db_article::new_query_builder(); 
    283         $qb->add_constraint('topic', '=', $this->_topic->id); 
    284         if (!$this->_config->get('indexinnav')) 
    285         { 
    286             $qb->add_constraint('name', '<>', 'index'); 
    287         } 
    288         $qb->add_constraint('up', '=', 0); 
    289         $qb->add_order('metadata.score', 'DESC'); 
    290         //$qb->add_order('name'); 
    291         //$qb->add_order('title'); 
    292          
    293         $this->_request_data['pages'] = $qb->execute(); 
    294          
    295         // Show the header element, which allows to change the sorting order 
    296         // and displays headers for the user 
    297         midcom_show_style('midcom-admin-show-order-header'); 
    298          
    299         switch ($this->_topic->parameter('midcom.helper.nav', 'navorder')) 
    300         { 
    301             // If the sort order is 'Pages first' 
    302             case MIDCOM_NAVORDER_ARTICLESFIRST: 
    303                 if (count($this->_request_data['pages']) > 1) 
    304                 { 
    305                     midcom_show_style('midcom-admin-show-order-pages'); 
    306                 } 
    307                  
    308                 if (count($this->_request_data['folders']) > 1) 
    309                 { 
    310                     midcom_show_style('midcom-admin-show-order-folders'); 
     235        if (!isset($_GET['ajax'])) 
     236        { 
     237            midcom_show_style('midcom-admin-folder-order-start'); 
     238        } 
     239         
     240        // Initialize the midcom_helper_nav or navigation access point 
     241        $nap = new midcom_helper_nav(); 
     242         
     243        switch ((int) $this->_topic->get_parameter('midcom.helper.nav', 'nav_order')) 
     244        { 
     245            case MIDCOM_NAVORDER_DEFAULT: 
     246                $data['navigation']['nodes'] = array(); 
     247                $nodes = $nap->list_nodes($this->_topic->id); 
     248                 
     249                foreach ($nodes as $id => $node_id) 
     250                { 
     251                    $node = $nap->get_node($node_id); 
     252                    $node[MIDCOM_NAV_TYPE] = 'node'; 
     253                    $data['navigation']['nodes'][$id] = $node; 
    311254                } 
    312255                break; 
    313256             
    314             // If the sort order is 'Folders first' 
    315257            case MIDCOM_NAVORDER_TOPICSFIRST: 
    316                 if (count($this->_request_data['folders']) > 1) 
    317                 { 
    318                     midcom_show_style('midcom-admin-show-order-folders'); 
    319                 } 
    320                  
    321                 if (count($this->_request_data['pages']) > 1) 
    322                 { 
    323                     midcom_show_style('midcom-admin-show-order-pages'); 
     258                // Sort the array to have the nodes first 
     259                $data['navigation'] = array 
     260                ( 
     261                    'nodes' => array(), 
     262                    'leaves' => array(), 
     263                ); 
     264                // Fall through 
     265                 
     266            case MIDCOM_NAVORDER_ARTICLESFIRST: 
     267                // Sort the array to have the leaves first 
     268                 
     269                if (!isset($data['navigation']['leaves'])) 
     270                { 
     271                    $data['navigation'] = array 
     272                    ( 
     273                        'leaves' => array(), 
     274                        'nodes' => array(), 
     275                    ); 
     276                } 
     277                 
     278                // Get the nodes 
     279                $nodes = $nap->list_nodes($this->_topic->id); 
     280                 
     281                foreach ($nodes as $id => $node_id) 
     282                { 
     283                    $node = $nap->get_node($node_id); 
     284                    $node[MIDCOM_NAV_TYPE] = 'node'; 
     285                    $data['navigation']['nodes'][$id] = $node; 
     286                } 
     287                 
     288                // Get the leafs 
     289                $leaves = $nap->list_leaves($this->_topic->id); 
     290                 
     291                foreach ($leaves as $id => $leaf_id) 
     292                { 
     293                    $leaf = $nap->get_leaf($leaf_id); 
     294                    $leaf[MIDCOM_NAV_TYPE] = 'leaf'; 
     295                    $data['navigation']['leaves'][$id] = $leaf; 
    324296                } 
    325297                break; 
    326298             
    327             // If the sort order is 'by score' 
    328299            case MIDCOM_NAVORDER_SCORE: 
    329                 $this->_request_data['mixed'] = array (); 
    330                  
    331                 foreach ($this->_request_data['folders'] as $topic) 
    332                 { 
    333                     $score = $this->_get_score($topic->score); 
    334                     $this->_request_data['mixed'][$score . '_' . $topic->id . '_folder'] = $topic->extra; 
    335                 } 
    336                  
    337                 foreach ($this->_request_data['pages'] as $article) 
    338                 { 
    339                     $score = $this->_get_score($article->score); 
    340                     $this->_request_data['mixed'][$score . '_' . $article->id . '_page'] = $article->title; 
    341                 } 
    342                  
    343                 ksort($this->_request_data['mixed']); 
    344                  
    345                 midcom_show_style('midcom-admin-show-order-mixed'); 
     300            default: 
     301                $data['navigation']['mixed'] = array(); 
     302                 
     303                // Get the navigation items 
     304                $items = $nap->list_child_elements($this->_topic->id); 
     305                 
     306                foreach ($items as $id => $item) 
     307                { 
     308                    if ($item[MIDCOM_NAV_TYPE] === 'node') 
     309                    { 
     310                        $element = $nap->get_node($item[MIDCOM_NAV_ID]); 
     311                    } 
     312                    else 
     313                    { 
     314                        $element = $nap->get_leaf($item[MIDCOM_NAV_ID]); 
     315                    } 
     316                     
     317                    // Store the type information 
     318                    $element[MIDCOM_NAV_TYPE] = $item[MIDCOM_NAV_TYPE]; 
     319                     
     320                    $data['navigation']['mixed'][] = $element; 
     321                } 
    346322                break; 
    347              
    348             // If the sort order is 'Default component sort order' 
    349             case MIDCOM_NAVORDER_DEFAULT: 
    350             default: 
    351                 if (count($this->_request_data['folders']) > 1) 
    352                 { 
    353                     midcom_show_style('midcom-admin-show-order-folders'); 
    354                 } 
    355                 else 
    356                 { 
    357                     midcom_show_style('midcom-admin-show-order-empty'); 
    358                 } 
    359                  
    360                 midcom_show_style('midcom-admin-show-order-default'); 
    361                 break; 
    362              
    363         } 
    364          
    365         if (   count($this->_request_data['folders']) < 2 
    366             && count($this->_request_data['pages']) < 2) 
    367         { 
    368             midcom_show_style('midcom-admin-show-order-empty'); 
    369         } 
    370          
    371         midcom_show_style('midcom-admin-show-order-footer'); 
     323        } 
     324         
     325        // Loop through each navigation type (node, leaf and mixed) 
     326        foreach ($data['navigation'] as $key => $array) 
     327        { 
     328            $data['navigation_type'] = $key; 
     329            $data['navigation_items'] = $array; 
     330            midcom_show_style('midcom-admin-folder-order-type'); 
     331        } 
     332         
     333        if (!isset($_GET['ajax'])) 
     334        { 
     335            midcom_show_style('midcom-admin-folder-order-end'); 
     336        } 
    372337    } 
    373338     
  • trunk/midcom/midcom.admin.folder/static/midcom-admin-order.css

    r6026 r13408  
    1 #midcom_admin_folder_order_form div.midcom-admin-order ul.sortable li 
    2 { 
    3   list-style: none; 
    4   cursor: move; 
    5   font-weight: normal; 
    6 } 
    7  
    8 #midcom_admin_folder_order_form div.midcom-admin-order ul.sortable li:hover 
    9 { 
    10   font-weight: bold; 
    11 } 
    12  
    131#midcom_admin_folder_show_save_info 
    142{ 
     
    2513#midcom_admin_folder_order_form img 
    2614{ 
    27   display: inline !important
     15  display: inline
    2816} 
    2917 
     
    3321  white-space: nowrap; 
    3422} 
     23 
     24#midcom_admin_folder_order_form ul.sortable 
     25{ 
     26  padding: 0; 
     27  margin: 1em 0; 
     28} 
     29 
     30#midcom_admin_folder_order_form ul.sortable li.node 
     31{ 
     32  background-image: url('../stock-icons/16x16/folder.png'); 
     33  background-repeat: no-repeat; 
     34  background-position: left center; 
     35  padding-left: 20px; 
     36} 
     37 
     38#midcom_admin_folder_order_form ul.sortable li.leaf 
     39{ 
     40  background-image: url('../stock-icons/16x16/stock_news.png'); 
     41  background-repeat: no-repeat; 
     42  background-position: left center; 
     43  padding-left: 20px; 
     44} 
     45 
     46#midcom_admin_folder_order_form ul.sortable li 
     47{ 
     48  list-style: none; 
     49  cursor: move; 
     50  font-weight: normal; 
     51} 
     52 
     53#midcom_admin_folder_order_form ul.sortable li:hover 
     54{ 
     55  font-weight: bold; 
     56}