[Solar-svn] Revision 2784

pmjones at solarphp.com pmjones at solarphp.com
Mon Sep 24 10:29:59 CDT 2007


branch: Solar_App_Bookmarks: [BRK] Updated entire app to match new Model/Record/Collection usage, and new Solar_View::partial() usage



Modified: branches/orm/Solar/App/Bookmarks/Layout/_local.php
===================================================================
--- branches/orm/Solar/App/Bookmarks/Layout/_local.php	2007-09-24 15:27:56 UTC (rev 2783)
+++ branches/orm/Solar/App/Bookmarks/Layout/_local.php	2007-09-24 15:29:58 UTC (rev 2784)
@@ -88,9 +88,10 @@
         $uri = Solar::factory('Solar_Uri_Action');
         unset($uri->query['page']);
         $tmp = array();
-        foreach ($this->tags_in_use as $tag => $count) {
-            $uri->setPath("$action/$tag");
-            $tmp[] = "<li>" . $this->action($uri, $tag) . " ($count)</li>";
+        foreach ($this->tags_in_use as $tag) {
+            $count = 'x';
+            $uri->setPath("$action/{$tag->name}");
+            $tmp[] = "<li>" . $this->action($uri, $tag->name) . " ({$tag->count})</li>";
         }
         echo implode("\n", $tmp);
     ?>

Modified: branches/orm/Solar/App/Bookmarks/View/_item.php
===================================================================
--- branches/orm/Solar/App/Bookmarks/View/_item.php	2007-09-24 15:27:56 UTC (rev 2783)
+++ branches/orm/Solar/App/Bookmarks/View/_item.php	2007-09-24 15:29:58 UTC (rev 2784)
@@ -15,58 +15,60 @@
  * 
  * @version $Id$
  * 
+ * @var Solar_Model_Nodes_Bookmarks_Record $item
+ * 
  */
 ?>
             
             <li class="bookmark-item">
                 <span><?php
-                    $title = $uri;
+                    $title = $item->uri;
                     if (strlen($title) > 72) {
                         // if longer than 72 chars, only show 64 chars, cut in the middle
                         $title = substr($title, 0, 48) . ' ... ' . substr($title, -16);
                     }
-                    echo $this->anchor($uri, $subj, array('title' => $title));
+                    echo $this->anchor($item->uri, $item->subj, array('title' => $title));
                 ?></span>
-                <ul><?php if (trim($summ) != ''): ?>
+                <ul><?php if (trim($item->summ) != ''): ?>
                     
                     <li class="summ"><?php
-                        echo nl2br(wordwrap($this->escape($summ), 72));
+                        echo nl2br(wordwrap($this->escape($item->summ), 72));
                     ?></li>
                     <?php endif ?>
                     
                     <li class="pos"><span><?php
                             echo $this->getText('LABEL_POS');
-                        ?></span> <?php echo $this->escape($pos);
+                        ?></span> <?php echo $this->escape($item->pos);
                     ?></li>
     
                     <li class="created">
                         <span><?php
                             echo $this->getText('LABEL_CREATED');
-                        ?></span> <?php echo $this->timestamp($created); ?>
+                        ?></span> <?php echo $this->timestamp($item->created); ?>
             
                         <span><?php
                             echo $this->getText('LABEL_OWNER_HANDLE');
                         ?></span> <?php echo $this->action(
-                            "bookmarks/user/$owner_handle",
-                            $owner_handle);
+                            "bookmarks/user/{$item->owner_handle}",
+                            $item->owner_handle);
                         ?>
             
                     </li>
-    
+                    
                     <li class="tags">
-                        <span><?php echo $this->getText('LABEL_TAGS');
-                        ?></span><?php
-                            $tags = explode(' ', $tags);
-                            foreach ($tags as $tag) {
-                                echo ' ' . $this->action("bookmarks/tag/$tag", $tag);
+                        <span><?php echo $this->getText('LABEL_TAGS'); ?></span>
+                        <?php if ($item->tags) {
+                            foreach ($item->tags as $tag) {
+                                echo $this->action("bookmarks/tag/{$tag->name}", $tag->name);
+                                echo "                            \n";
                             }
-                        ?>
-            
-                    </li><?php
-                    if (Solar::registry('user')->auth->handle == $owner_handle): ?>
-                    
+                        } ?>
+                        
+                    </li>
+                        
+                    <?php if (Solar::registry('user')->auth->handle == $item->owner_handle): ?>
                     <li class="edit">
-                        <?php echo $this->action("bookmarks/edit/$id", 'PROCESS_EDIT'); ?>
+                        <?php echo $this->action("bookmarks/edit/{$item->id}", 'PROCESS_EDIT'); ?>
                     </li>
                     <?php endif; ?>
                     

Modified: branches/orm/Solar/App/Bookmarks.php
===================================================================
--- branches/orm/Solar/App/Bookmarks.php	2007-09-24 15:27:56 UTC (rev 2783)
+++ branches/orm/Solar/App/Bookmarks.php	2007-09-24 15:29:58 UTC (rev 2784)
@@ -88,6 +88,15 @@
     
     /**
      * 
+     * The total number of records in the query.
+     * 
+     * @var int
+     * 
+     */
+    public $count;
+    
+    /**
+     * 
      * The requested bookmark order (subj, tags, created, etc).
      * 
      * @var string
@@ -180,6 +189,8 @@
     
     protected $_bookmarks;
     
+    protected $_tags;
+    
     /**
      * 
      * Extended setup.
@@ -208,13 +219,15 @@
                 throw $this->_exception('ERR_AREA_NOT_CREATED');
             }
         }
-        
+
         // get a user object for privileges
         $this->user = Solar::registry('user');
         
         // keep a bookmarks model
         $this->_bookmarks = Solar::factory('Solar_Model_Nodes_Bookmarks');
         
+        // keep a tags model
+        $this->_tags = Solar::factory('Solar_Model_Tags');
     }
     
     /**
@@ -224,7 +237,7 @@
      * @return string
      * 
      */
-    protected function _getOrder()
+    protected function _getSqlOrder()
     {
         $tmp = strtolower($this->_query('order'));
         
@@ -233,26 +246,20 @@
         // created timestamp
         case 'created':
         case 'created_asc':
-        case 'ts':
-        case 'ts_asc':
             $order = 'bookmarks.created ASC';
             break;
         
         case 'created_desc':
-        case 'ts_desc':
             $order = 'bookmarks.created DESC';
             break;
             
         // title
         case 'subj':
         case 'subj_asc':
-        case 'title':
-        case 'title_asc':
             $order = 'LOWER(bookmarks.subj) ASC';
             break;
         
         case 'subj_desc':
-        case 'title_desc':
             $order = 'LOWER(bookmarks.subj) DESC';
             break;
         
@@ -266,19 +273,6 @@
             $order = 'bookmarks.pos DESC';
             break;
         
-        // owner handle (username)
-        case 'owner':
-        case 'owner_asc':
-        case 'user':
-        case 'user_asc':
-            $order = 'LOWER(bookmarks.owner_handle) ASC';
-            break;
-        
-        case 'owner_desc':
-        case 'user_desc':
-            $order = 'LOWER(bookmarks.owner_handle) DESC';
-            break;
-        
         // default
         default:
             $order = 'bookmarks.created DESC';
@@ -316,7 +310,7 @@
         $this->_session->setFlash('backlink', $href);
         
         // which record cols do we want to work with?
-        $cols = array('uri', 'subj', 'summ', 'pos');
+        $cols = array('uri', 'subj', 'summ', 'tags_as_string', 'pos');
         
         // get a blank bookmark, then populate with submitted data
         $item = $this->_bookmarks->fetchNew();
@@ -328,22 +322,18 @@
         $item->owner_handle = $this->user->auth->handle;
         $item->editor_handle = $this->user->auth->handle;
         
-        // Process: save
+        // save?
         if ($this->_isProcess('save')) {
-            
-            // attempt the save; this will also run filters on it
-            if ($item->save()) {
-                // success!
-                // redirect to the "edit" page
+            try {
+                $item->save();
                 $this->_session->setFlash('add_ok', true);
-                die('success');
                 $this->_redirect("bookmarks/edit/{$item->id}");
-            } else {
-                // validation or save failed
+            } catch (Solar_Sql_Model_Record_Exception_Invalid $e) {
+                // do nothing, the item form will show invalids
             }
         }
         
-        // Process: Cancel
+        // cancel?
         if ($this->_isProcess('cancel')) {
             $this->_redirect($href);
         }
@@ -387,7 +377,10 @@
         }
         
         // must be the item owner to edit it
-        $item = $this->_bookmarks->fetch($id);
+        $item = $this->_bookmarks->fetchOneById($id, array(
+            'eager' => array('taggings', 'tags'),
+        ));
+        
         if ($this->user->auth->handle != $item->owner_handle) {
             $this->errors[] = 'ERR_NOT_OWNER';
             return $this->_forward('error');
@@ -417,11 +410,15 @@
         // 
         
         // which record cols do we want to work with?
-        $cols = array('uri', 'subj', 'summ', 'pos');
+        $cols = array('uri', 'subj', 'summ', 'tags_as_string', 'pos');
         
         // load from posted data
         $data = $this->_request->post('bookmarks', array());
+        // Solar::dump($data, 'from POST');
+        // Solar::$config['TEST']['BREAK'] = true;
+        // Solar::dump($item);
         $item->load($data, $cols);
+        // die('postload');
         
         // force these values
         $item->area_id = $this->area->id;
@@ -433,22 +430,12 @@
         // processes
         // 
         
-        // save
         if ($this->_isProcess('save')) {
-            
-            if ($item->save()) {
-                // do nothing
-            } else {
-                // // exception on save()
-                // // we should not have gotten to this point,
-                // // but need to be aware of possible problems.
-                // $form->setStatus(false);
-                // $form->feedback[] = $e->getClass() . ' -- ' . $e->getMessage();
-                //         
-                // // add bookmark[*] element feedback
-                // $form->addFeedback($e->getInfo(), 'bookmark');
+            try {
+                $item->save();
+            } catch (Solar_Sql_Model_Record_Exception_Invalid $e) {
+                // do nothing, the item form will show invalids
             }
-    
         }
         
         // cancel
@@ -481,134 +468,126 @@
         $this->backlink = $href;
     }
     
-    // /**
-    //  * 
-    //  * Handles JavaScript bookmarking requests from offsite.
-    //  * 
-    //  * @param string $uri The URI to bookmark.
-    //  * 
-    //  * @param string $subj The title for the bookmark, typically the
-    //  * page title from the bookmarked page.
-    //  * 
-    //  * @return void
-    //  * 
-    //  */
-    // public function actionQuick($uri = null, $subj = null)
-    // {
-    //     // must be logged in to proceed
-    //     if (! $this->user->auth->isValid()) {
-    //         $this->errors[] = 'ERR_NOT_LOGGED_IN';
-    //         return $this->_forward('error');
-    //     }
-    //     
-    //     // get the quickmark info from the query
-    //     $uri = $this->_query('uri');
-    //     $subj = $this->_query('subj');
-    //     
-    //     // we need to see if the user already has the same URI in
-    //     // his bookmarks so that we don't add it twice.
-    //     $existing = $this->_bookmarks->fetchByOwnerUri(
-    //         $this->user->auth->handle,
-    //         $uri
-    //     );
-    //     
-    //     // if the user *does* already have that URI bookmarked,
-    //     // redirect to the existing bookmark.
-    //     if (! empty($existing->id)) {
-    //         $this->_session->setFlash('backlink', $uri);
-    //         $this->_redirect("bookmarks/edit/{$existing['id']}");
-    //     }
-    //     
-    //     // get a blank bookmark item, build the basic form
-    //     $item = $this->_bookmarks->fetchNew();
-    //     $item->uri = $uri;
-    //     $item->subj = $subj;
-    //     $form = $this->_bookmarks->form($item);
-    //     
-    //     // overwrite form defaults with submissions
-    //     $form->populate();
-    //     
-    //     // check for a 'Save' process request
-    //     if ($this->_isProcess('save') && $form->validate()) {
-    // 
-    //         // save the data
-    //         try {
-    // 
-    //             // get the form values
-    //             $item->load($form->values('bookmark'));
-    //             $item->owner_handle = $this->user->auth->handle;
-    //             $item->editor_handle = $this->user->auth->handle;
-    //     
-    //             // save
-    //             $item->save();
-    //     
-    //             // redirect to the source URI (external)
-    //             $this->_redirect($item->uri);
-    //     
-    //         } catch (Solar_Exception $e) {
-    //     
-    //             // exception on save()
-    //             // we should not have gotten to this point,
-    //             // but need to be aware of possible problems.
-    //             $form->setStatus(false);
-    //             $form->feedback[] = $e->getClass() . ' -- ' . $e->getMessage();
-    //             echo $e;
-    //     
-    //         }
-    //     }
-    //     
-    //     // assign data for the view
-    //     $this->formdata = $form;
-    //     $this->backlink = $uri;
-    // }
+    /**
+     * 
+     * Handles JavaScript bookmarking requests from offsite.
+     * 
+     * @param string $uri The URI to bookmark.
+     * 
+     * @param string $subj The title for the bookmark, typically the
+     * page title from the bookmarked page.
+     * 
+     * @return void
+     * 
+     */
+    public function actionQuick($uri = null, $subj = null)
+    {
+        // must be logged in to proceed
+        if (! $this->user->auth->isValid()) {
+            $this->errors[] = 'ERR_NOT_LOGGED_IN';
+            return $this->_forward('error');
+        }
+        
+        // get the quickmark info from the query
+        $uri = $this->_query('uri');
+        $subj = $this->_query('subj');
+        
+        // we need to see if the user already has the same URI in
+        // his bookmarks so that we don't add it twice.
+        $existing = $this->_bookmarks->fetchOneByOwnerHandleAndUri(
+            $this->user->auth->handle,
+            $uri
+        );
+        
+        // if the user *does* already have that URI bookmarked,
+        // redirect to the existing bookmark.
+        if ($existing) {
+            $this->_session->setFlash('backlink', $uri);
+            $this->_redirect("bookmarks/edit/{$existing->id}");
+        }
+        
+        // which record cols do we want to work with?
+        $cols = array('uri', 'subj', 'summ', 'tags_as_string', 'pos');
+        
+        // get a blank bookmark, then populate with submitted data
+        $item = $this->_bookmarks->fetchNew();
+        
+        // populate first with query values
+        $item->uri = $uri;
+        $item->subj = $subj;
+        
+        // overwrite with POST values, if any
+        $data = $this->_request->post('bookmarks', array());
+        $item->load($data, $cols);
+        
+        // force these values
+        $item->area_id = $this->area->id;
+        $item->owner_handle = $this->user->auth->handle;
+        $item->editor_handle = $this->user->auth->handle;
+        
+        // save?
+        if ($this->_isProcess('save')) {
+            try {
+                $item->save();
+                $this->_session->setFlash('add_ok', true);
+                $this->_redirect("bookmarks/edit/{$item->id}");
+            } catch (Solar_Sql_Model_Record_Exception_Invalid $e) {
+                // do nothing, the item form will show invalids
+            }
+        }
+        
+        // assign data for the view, and done
+        $this->formdata = $item->form($cols);
+        $this->backlink = $uri;
+    }
     
-    // /**
-    //  * 
-    //  * Shows a list of bookmarks filtered by tag, regardless of owner.
-    //  * 
-    //  * @param string|array $tags The tags to show; either a space- (or
-    //  * plus-) separated list of tags, or a sequential array of tags.
-    //  * 
-    //  * @return void
-    //  * 
-    //  */
-    // public function actionTag($tags = null)
-    // {
-    //     // allow uri to set the "count" for each page (default 10)
-    //     $this->_bookmarks->setPaging($this->_query('paging', 10));
-    //     
-    //     // the requested owner_handle (none)
-    //     $owner_handle = null;
-    //     
-    //     // the requested ordering of list results
-    //     $order = $this->_getOrder();
-    //     
-    //     // what page-number of the results are we looking for?
-    //     // (regardless of RSS or HTML)
-    //     $page = $this->_query('page', 1);
-    //     
-    //     // get the list of results
-    //     $this->list = $this->_bookmarks->fetchAll($tags, $owner_handle, $order, $page);
-    //     
-    //     // get the total pages and row-count
-    //     $total = $this->_bookmarks->countPages($tags, $owner_handle);
-    //     
-    //     // flash forward the backlink in case we go to edit
-    //     $this->_session->setFlash('backlink', $this->_request->server('REQUEST_URI'));
-    //     
-    //     // assign everything else for the view
-    //     $this->pages         = $total['pages'];
-    //     $this->order         = $this->_request->get('order', 'created_desc');
-    //     $this->page          = $page;
-    //     $this->owner_handle  = null; // requested owner_handle
-    //     $this->tags          = $tags; // the requested tags
-    //     $this->tags_in_use   = $this->_bookmarks->fetchTags($owner_handle); // all tags
-    //     $this->feed['title'] = 'tag';
-    //     $this->feed['descr'] = $this->tags;
-    //     
-    //     // set the view
-    //     $this->_view = 'browse';
-    // }
+    /**
+     * 
+     * Shows a list of bookmarks filtered by tag, regardless of owner.
+     * 
+     * @param string|array $tags The tags to show; either a space- (or
+     * plus-) separated list of tags, or a sequential array of tags.
+     * 
+     * @return void
+     * 
+     */
+    public function actionTag($tag_list = null)
+    {
+        // params for the query
+        $params = array(
+            'where'  => array(
+                // only this area
+                'bookmarks.area_id = ?'      => $this->area->id,
+            ),
+            'order'  => $this->_getSqlOrder(),
+            'paging' => $this->_query('paging', 10),
+            'page'   => $this->_query('page', 1),
+            'eager'  => array('taggings', 'tags'),
+        );
+        
+        // get the list of bookmarks
+        $this->list = $this->_bookmarks->fetchAllByTags($tag_list, $params);
+        
+        // get the total pages and row-count
+        $total = $this->_bookmarks->countPages($tag_list, $params);
+        
+        // flash forward the backlink in case we go to edit
+        $this->_session->setFlash('backlink', $this->_request->server('REQUEST_URI'));
+        
+        // assign everything else for the view
+        $this->count         = $total['count'];
+        $this->pages         = $total['pages'];
+        $this->order         = $this->_query('order');
+        $this->page          = $params['page'];
+        $this->owner_handle  = null; // requested owner_handle
+        $this->tags          = $tag_list; // the requested tags
+        $this->tags_in_use   = $this->_tags->fetchAllWithCount(null);
+        $this->feed['title'] = 'tag';
+        $this->feed['descr'] = $tag_list;
+        
+        // set the view
+        $this->_view = 'browse';
+    }
     
     /**
      * 
@@ -616,16 +595,16 @@
      * 
      * @param string $owner_handle The owner to show bookmarks for.
      * 
-     * @param string|array $tags An optional set of tags to filter by;
+     * @param string|array $tag_list An optional set of tags to filter by;
      * either a space- (or plus-) separated list of tags, or a
      * sequential array of tags.
      * 
      * @return void
      * 
      */
-    public function actionUser($owner_handle = null, $tags = null)
+    public function actionUser($owner_handle = null, $tag_list = null)
     {
-        // must have passed an own
+        // must have passed an owner handle
         if (! $owner_handle) {
             $this->errors[] = 'ERR_NO_OWNER_HANDLE';
             return $this->_forward('error');
@@ -635,31 +614,36 @@
         $params = array(
             'where'  => array(
                 // only this area
-                'area_id = ?'      => $this->area->id,
+                'bookmarks.area_id = ?'      => $this->area->id,
                 // only the specified user
-                'owner_handle = ?' => $owner_handle,
+                'bookmarks.owner_handle = ?' => $owner_handle,
             ),
-            'order'  => $this->_getOrder(),
+            'order'  => $this->_getSqlOrder(),
             'paging' => $this->_query('paging', 10),
             'page'   => $this->_query('page', 1),
+            'eager'  => array('taggings', 'tags'),
         );
         
-        // get the list of bookmarks
-        $this->list = $this->_bookmarks->fetchAll($params);
+        // tags or no-tags?
+        if ($tag_list) {
+            $this->list = $this->_bookmarks->fetchAllByTags($tag_list, $params);
+            $total = $this->_bookmarks->countPagesByTags($params);
+        } else {
+            $this->list = $this->_bookmarks->fetchAll($params);
+            $total = $this->_bookmarks->countPages($params);
+        }
         
-        // get the total record-count and pages
-        $total = $this->_bookmarks->countPages($params);
-        
         // flash forward the backlink in case we go to edit
         $this->_session->setFlash('backlink', $this->_request->server('REQUEST_URI'));
         
         // assign view vars
+        $this->count         = $total['count'];
         $this->pages         = $total['pages'];
-        $this->order         = $this->_request->get('order', 'created_desc');
+        $this->order         = $this->_query('order');
         $this->page          = $params['page'];
         $this->owner_handle  = $owner_handle; // requested owner_handle
-        $this->tags          = $tags; // the requested tags
-        $this->tags_in_use   = array(); // $this->_bookmarks->fetchTags($owner_handle); // all tags for this user
+        $this->tags          = $tag_list; // the requested tags
+        $this->tags_in_use   = $this->_tags->fetchAllByOwnerHandle($owner_handle);
         $this->feed['title'] = 'user';
         $this->feed['descr'] = $this->owner_handle . '/' . $this->tags;
         




More information about the Solar-svn mailing list