Overview

Namespaces

  • None
  • PHP

Classes

  • Breadcrumb
  • Breadcrumbs
  • CacheFile
  • Comment
  • CommentArray
  • CommentFilter
  • CommentForm
  • CommentList
  • Config
  • Database
  • DatabaseQuery
  • DatabaseQuerySelect
  • DatabaseQuerySelectFulltext
  • DatabaseStopwords
  • Debug
  • Field
  • Fieldgroup
  • Fieldgroups
  • FieldgroupsArray
  • Fields
  • FieldsArray
  • Fieldtype
  • FieldtypeMulti
  • Fieldtypes
  • FileLog
  • FilenameArray
  • Fuel
  • HookEvent
  • ImageSizer
  • Inputfield
  • InputfieldsArray
  • InputfieldWrapper
  • Language
  • LanguageParser
  • Languages
  • LanguagesPageFieldValue
  • LanguageSupportInstall
  • LanguageTranslator
  • Markdown_Parser
  • MarkdownExtra_Parser
  • ModuleJS
  • ModulePlaceholder
  • Modules
  • Notice
  • NoticeError
  • NoticeMessage
  • Notices
  • NullPage
  • Page
  • PageArray
  • Pagefile
  • Pagefiles
  • PagefilesManager
  • PageFinder
  • Pageimage
  • Pageimages
  • PagerNav
  • PagerNavItem
  • Pages
  • PagesAccess
  • PagesSortfields
  • PagesType
  • Paths
  • Permission
  • Permissions
  • Process
  • ProcessController
  • ProcessWire
  • Role
  • Roles
  • Sanitizer
  • Selector
  • SelectorBitwiseAnd
  • SelectorContains
  • SelectorContainsLike
  • SelectorContainsWords
  • SelectorEnds
  • SelectorEqual
  • SelectorGreaterThan
  • SelectorGreaterThanEqual
  • SelectorLessThan
  • SelectorLessThanEqual
  • SelectorNotEqual
  • Selectors
  • SelectorStarts
  • Session
  • SessionCSRF
  • SmartyPants_Parser
  • SmartyPantsTypographer_Parser
  • SystemUpdate1
  • Template
  • TemplateFile
  • Templates
  • TemplatesArray
  • Textformatter
  • Textile
  • User
  • Users
  • Wire
  • WireArray
  • WireData
  • WireInput
  • WireInputData
  • WireSaveableItems
  • WireSaveableItemsLookup
  • WireUpload

Interfaces

  • CommentFormInterface
  • CommentListInterface
  • ConfigurableModule
  • FieldtypeLanguageInterface
  • FieldtypePageTitleCompatible
  • HasLookupItems
  • HasRoles
  • InputfieldHasArrayValue
  • Module
  • Saveable
  • TrackChanges

Exceptions

  • ProcessController404Exception
  • ProcessControllerPermissionException
  • Wire404Exception
  • WireDatabaseException
  • WireException
  • WirePermissionException

Functions

  • __
  • _n
  • _x
  • fuel
  • identify_modifier_markdown
  • Markdown
  • mdwp_add_p
  • mdwp_hide_tags
  • mdwp_show_tags
  • mdwp_strip_p
  • ProcessWireClassLoader
  • ProcessWireHostSiteConfig
  • ProcessWireShutdown
  • removeNewlines
  • SmartDashes
  • SmartEllipsis
  • SmartQuotes
  • smarty_modifier_markdown
  • smarty_modifier_smartypants
  • SmartyPants
  • tabIndent
  • unregisterGLOBALS
  • wire
  • wireDecodeJSON
  • wireEncodeJSON
  • wireMkdir
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: 
  3: /**
  4:  * ProcessWire WireSaveableItemsLookup
  5:  *
  6:  * Provides same functionality as WireSaveableItems except that this class includes joining/modification of a related lookup table
  7:  * 
  8:  * ProcessWire 2.x 
  9:  * Copyright (C) 2010 by Ryan Cramer 
 10:  * Licensed under GNU/GPL v2, see LICENSE.TXT
 11:  * 
 12:  * http://www.processwire.com
 13:  * http://www.ryancramer.com
 14:  *
 15:  */
 16: 
 17: abstract class WireSaveableItemsLookup extends WireSaveableItems {
 18: 
 19:     /**
 20:      * If a lookup table should be left joined, this method should return the table name
 21:      *
 22:      */
 23:     abstract public function getLookupTable();
 24: 
 25:     /**
 26:      * If a lookup table should be left joined, this method returns the name of the array field in $data that contains multiple values
 27:      * 
 28:      * i.e. roles_permissions becomes permissions_id if getTable() returns roles
 29:      * Does not need to be overridden unless the table naming structure doesn't follow existing logic.
 30:      *
 31:      */
 32:     public function getLookupField() { 
 33:         $lookupTable = $this->getLookupTable();
 34:         if(!$lookupTable) return ''; 
 35:         return preg_replace('/_?' . $this->getTable() . '_?/', '', $lookupTable) . '_id';
 36:     }
 37: 
 38:     /**
 39:      * Get the DatabaseQuerySelect to perform the load operation of items
 40:      *
 41:      * @param WireArray $items
 42:      * @param Selectors|string|null $selectors Selectors or a selector string to find, or NULL to load all. 
 43:      * @return DatabaseQuerySelect
 44:      *
 45:      */
 46:     protected function getLoadQuery($selectors = null) {
 47:         $query = parent::getLoadQuery($selectors); 
 48:         $table = $this->getTable();
 49:         $lookupTable = $this->getLookupTable(); 
 50:         $query->select("$lookupTable." . $this->getLookupField()); 
 51:         $query->leftjoin("$lookupTable ON $lookupTable.{$table}_id=$table.id ")->orderby("sort"); 
 52:         return $query; 
 53:     }
 54: 
 55:     /**
 56:      * Load items from the database table and return them in the same type class that getAll() returns
 57:      
 58:      * A selector string or Selectors may be provided so that this can be used as a find() by descending classes that don't load all items at once.  
 59:      *
 60:      * @param Selectors|string|null $selectors Selectors or a selector string to find, or NULL to load all. 
 61:      * @return WireArray Returns the same type as specified in the getAll() method.
 62:      *
 63:      */
 64:     protected function ___load(WireArray $items, $selectors = null) {
 65: 
 66:         $query = $this->getLoadQuery($selectors);
 67:         $result = $this->fuel('db')->query($query); 
 68:         $lookupField = $this->getLookupField();
 69: 
 70:         while($row = $result->fetch_assoc()) {
 71: 
 72:             $item = $this->makeBlankItem();
 73:             $lookupValue = $row[$lookupField]; 
 74:             unset($row[$lookupField]); 
 75:             $item->addLookupItem($lookupValue, $row); 
 76: 
 77:             foreach($row as $field => $value) {
 78:                 $item->$field = $value; 
 79:             }
 80: 
 81:             if($items->has($item)) {
 82:                 // LEFT JOIN is adding more elements of the same item, i.e. from lookup table
 83:                 // if the item is already present in $items, then use the existing one rather 
 84:                 // and throw out the one we just created
 85:                 $item = $items->get($item); 
 86:                 $item->addLookupItem($lookupValue, $row); 
 87:             } else {
 88:                 // add a new item
 89:                 $items->add($item); 
 90:             }
 91:         }
 92: 
 93:         if($result) $result->free();
 94:         $items->setTrackChanges(true); 
 95:         return $items; 
 96:     }
 97: 
 98:     /**
 99:      * Should the given item key/field be saved in the database?
100:      *
101:      * Template method used by ___save()
102:      *
103:      */
104:     protected function saveItemKey($key) {
105:         if($key == $this->getLookupField()) return false; 
106:         return parent::saveItemKey($key); 
107:     }
108: 
109:     /**
110:      * Save the provided item to database
111:      *
112:      */
113:     public function ___save(Saveable $item) {
114: 
115:         if(!$item instanceof HasLookupItems) throw new WireException($this->className() . "::save() requires an item that implements HasLookupItems interface"); 
116:         
117:         $lookupTable = $this->getLookupTable();
118:         $lookupField = $this->getLookupField();
119:         $table = $this->getTable();
120: 
121:         if($item->id) $this->fuel('db')->query("DELETE FROM $lookupTable WHERE {$table}_id={$item->id}"); 
122:             
123:         $result = parent::___save($item); 
124: 
125:         // if($item->id) foreach($item->get($lookupField) as $key => $value) {
126:         $sort = 0; 
127:         if($item->id) foreach($item->getLookupItems() as $key => $value) {
128:             $this->fuel('db')->query("INSERT INTO $lookupTable SET {$table}_id='{$item->id}', $lookupField='{$value->id}', sort='$sort'"); 
129:             $this->resetTrackChanges();
130:             $sort++; 
131:         }
132: 
133: 
134:         return $result; 
135:     }
136: 
137:     /** 
138:      * Delete the provided item from the database
139:      *
140:      */
141:     public function ___delete(Saveable $item) {
142:         $this->fuel('db')->query("DELETE FROM " . $this->getLookupTable() . " WHERE " . $this->getTable() . "_id={$item->id}"); 
143:         return parent::___delete($item); 
144:     }
145: }
146: 
ProcessWire API documentation generated by ApiGen 2.6.0