├── templates ├── _input.twig └── _settings.twig ├── composer.json ├── LICENSE ├── IncrementPlugin.php ├── README.md └── fieldtypes └── IncrementFieldType.php /templates/_input.twig: -------------------------------------------------------------------------------- 1 | {% import "_includes/forms" as forms %} 2 | 3 | {{ forms.text({ 4 | id: name, 5 | name: name, 6 | value: (value is defined) ? value : '', 7 | }) }} 8 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "boboldehampsink/increment", 3 | "description": "Increment Plugin for Craft CMS", 4 | "authors": [ 5 | { 6 | "name": "Bob Olde Hampsink", 7 | "email": "b.oldehampsink@itmundi.nl" 8 | } 9 | ], 10 | "type": "craft-plugin", 11 | "require": { 12 | "composer/installers": "~1.0" 13 | } 14 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Bob Olde Hampsink 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /IncrementPlugin.php: -------------------------------------------------------------------------------- 1 | 11 | * @copyright Copyright (c) 2015, Bob Olde Hampsink 12 | * @license MIT 13 | * 14 | * @link http://github.com/boboldehampsink 15 | */ 16 | class IncrementPlugin extends BasePlugin 17 | { 18 | /** 19 | * Get plugin name. 20 | * 21 | * @return string 22 | */ 23 | public function getName() 24 | { 25 | return Craft::t('Increment'); 26 | } 27 | 28 | /** 29 | * Get plugin version. 30 | * 31 | * @return string 32 | */ 33 | public function getVersion() 34 | { 35 | return '0.3.4'; 36 | } 37 | 38 | /** 39 | * Get plugin developer. 40 | * 41 | * @return string 42 | */ 43 | public function getDeveloper() 44 | { 45 | return 'Bob Olde Hampsink'; 46 | } 47 | 48 | /** 49 | * Get plugin developer url. 50 | * 51 | * @return string 52 | */ 53 | public function getDeveloperUrl() 54 | { 55 | return 'http://github.com/boboldehampsink'; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /templates/_settings.twig: -------------------------------------------------------------------------------- 1 | {% import "_includes/forms" as forms %} 2 | 3 | {{ forms.lightswitchField({ 4 | label: "Recalculate on save"|t, 5 | id: 'recalculate', 6 | name: 'recalculate', 7 | instructions: 'Recalculate max number on save. This will make the number always unique.'|t, 8 | on: settings.recalculate, 9 | first: true, 10 | errors: settings.getErrors('recalculate') 11 | }) }} 12 | 13 | {{ forms.textField({ 14 | label: 'Prefix'|t, 15 | id: 'prefix', 16 | name: 'prefix', 17 | instructions: 'Insert the prefix you want to use, if any. This can also be a variable like {postDate.year}.'|t, 18 | value: settings.prefix, 19 | errors: settings.getErrors('prefix') 20 | }) }} 21 | 22 | {{ forms.lightswitchField({ 23 | label: 'Yearly reset'|t, 24 | id: 'yearlyreset', 25 | name: 'yearlyreset', 26 | instructions: 'Reset the increment yearly. This will make the increment start with 1 again at the beginning of each year.'|t, 27 | on: settings.yearlyreset, 28 | errors: settings.getErrors('yearlyreset') 29 | }) }} 30 | 31 | {{ forms.textField({ 32 | label: 'Next Number'|t, 33 | id: 'increment', 34 | name: 'increment', 35 | instructions: 'Insert the number you want to use for the next incremented value. The number can must be higher than the highest incremented value or Increment will base its next number off of existing highest number.'|t, 36 | value: settings.increment, 37 | size: 5, 38 | errors: settings.getErrors('increment') 39 | }) }} 40 | 41 | {{ forms.textField({ 42 | label: 'Zero Padding'|t, 43 | id: 'padding', 44 | name: 'padding', 45 | instructions: 'Insert the number of zeroes you want to pad. E.g. "4" will pad your number to 0001, "5" to 00001 etc.'|t, 46 | value: settings.padding, 47 | size: 5, 48 | errors: settings.getErrors('padding') 49 | }) }} 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DEPRECATED - Increment plugin for Craft CMS 2 | ================= 3 | 4 | Plugin that automatically ups each increment field by one number. 5 | 6 | Features: 7 | - Able to output the value in the CP and in the Site (no other plugin has this feature) 8 | - Ability to have the value prefixed with text or variables (like {id} or {{now.year}}) 9 | - Ability to reset increment yearly 10 | - Ability to zero pad the value 11 | - This plugin checks if the calculated next value is still unique on save 12 | 13 | Important: 14 | The plugin's folder should be named "increment" 15 | 16 | Credits: 17 | This plugin's idea is derived from the Sprout Incremental Plugin by Barrel Strength Design 18 | 19 | Deprecated 20 | ================= 21 | With the release of Craft 3 on 4-4-2018, this plugin has been deprecated. You can still use this with Craft 2 but you are encouraged to use (and develop) a Craft 3 version. At this moment, I have no plans to do so. 22 | 23 | Changelog 24 | ================= 25 | ### 0.3.4 ### 26 | - Fixed yearly reset for non-entry elements 27 | 28 | ### 0.3.3 ### 29 | - Fixed setting of postDate on fresh entries 30 | 31 | ### 0.3.2 ### 32 | - Fixed bug where yearly reset looked at the wrong date (dateCreated in stead of postDate) 33 | 34 | ### 0.3.1 ### 35 | - Fixed bug where padding was not a number (thanks to @steverowling) 36 | - Fixed bug where postDate was assumed for all Element Types (thanks to @steverowling) 37 | 38 | ### 0.3.0 ### 39 | - Refactor to only recalculate numbers on save when entry is a new record 40 | - Added the ability to reset increment yearly 41 | 42 | ### 0.2.2 ### 43 | - Added the ability to control number recalculation on save 44 | 45 | ### 0.2.1 ### 46 | - Added the ability to use the then yet non-existing postDate in prefix like in Craft's Live Preview 47 | 48 | ### 0.2 ### 49 | - Added the ability to add a prefix to each value, like {id} or {{now.year}} 50 | - Added zero padding options 51 | - Prevent duplicate values when elements are created at the same time. 52 | - Many small improvements and code cleanups 53 | - Added MIT License 54 | 55 | ### 0.1 ### 56 | - Initial push to GitHub 57 | -------------------------------------------------------------------------------- /fieldtypes/IncrementFieldType.php: -------------------------------------------------------------------------------- 1 | 11 | * @copyright Copyright (c) 2015, Bob Olde Hampsink 12 | * @license MIT 13 | * 14 | * @link http://github.com/boboldehampsink 15 | */ 16 | class IncrementFieldType extends BaseFieldType 17 | { 18 | /** 19 | * Get fieldtype name. 20 | * 21 | * @return string 22 | */ 23 | public function getName() 24 | { 25 | return Craft::t('Increment'); 26 | } 27 | 28 | /** 29 | * Define fieldtype as number. 30 | * 31 | * @return string 32 | */ 33 | public function defineContentAttribute() 34 | { 35 | return AttributeType::Number; 36 | } 37 | 38 | /** 39 | * Define fieldtype settings. 40 | * 41 | * @return array 42 | */ 43 | protected function defineSettings() 44 | { 45 | return array( 46 | 'recalculate' => array(AttributeType::Bool, 'default' => true), 47 | 'prefix' => AttributeType::String, 48 | 'yearlyreset' => array(AttributeType::Bool, 'default' => false), 49 | 'increment' => AttributeType::Number, 50 | 'padding' => AttributeType::Number, 51 | ); 52 | } 53 | 54 | /** 55 | * Render settings template. 56 | * 57 | * @return string 58 | */ 59 | public function getSettingsHtml() 60 | { 61 | return craft()->templates->render('increment/_settings', array( 62 | 'settings' => $this->getSettings(), 63 | )); 64 | } 65 | 66 | /** 67 | * Prepares value after posting, removing the prefix and checking if its still unique. 68 | * 69 | * @param string $value 70 | * 71 | * @return string 72 | */ 73 | public function prepValueFromPost($value) 74 | { 75 | $this->setPostDate(); 76 | 77 | // Get settings 78 | $settings = $this->getSettings(); 79 | 80 | // If value is not yet set 81 | if (!isset($value) || ($this->isFresh() && $settings->recalculate)) { 82 | $value = $this->_getMaxNumber(); 83 | } else { 84 | $value = $this->getIncrementNumber($value); 85 | } 86 | 87 | return $value; 88 | } 89 | 90 | /** 91 | * Prepares value for output, adding the prefix. 92 | * 93 | * @param string $value 94 | * 95 | * @return string 96 | */ 97 | public function prepValue($value) 98 | { 99 | $this->setPostDate(); 100 | 101 | // Get settings 102 | $settings = $this->getSettings(); 103 | 104 | // If value is not yet set 105 | if (!isset($value)) { 106 | $value = $this->_getMaxNumber(); 107 | } 108 | 109 | // Pad zeroes 110 | $value = str_pad($value, (int) $settings->padding, '0', STR_PAD_LEFT); 111 | 112 | // Add prefix 113 | $value = craft()->templates->renderObjectTemplate($settings->prefix, $this->element).$value; 114 | 115 | // Return this value 116 | return $value; 117 | } 118 | 119 | /** 120 | * Render input template. 121 | * 122 | * @param string $name 123 | * @param string $value 124 | * 125 | * @return string 126 | */ 127 | public function getInputHtml($name, $value) 128 | { 129 | return craft()->templates->render('increment/_input', array( 130 | 'name' => $name, 131 | 'value' => $value, 132 | )); 133 | } 134 | 135 | /** 136 | * Get value without prefix 137 | * @param string $value 138 | * @return int 139 | */ 140 | public function getIncrementNumber($value) 141 | { 142 | $settings = $this->getSettings(); 143 | return (int)str_replace(craft()->templates->renderObjectTemplate($settings->prefix, $this->element), '', $value); 144 | } 145 | 146 | /** 147 | * Get current max number from db. 148 | * 149 | * @return string 150 | */ 151 | private function _getMaxNumber() 152 | { 153 | $settings = $this->getSettings(); 154 | $minValue = $settings->increment; 155 | 156 | $query = craft()->db->createCommand()->select('MAX(`field_'.$this->model->handle.'`)')->from('content'); 157 | 158 | if ($settings->yearlyreset) { 159 | if ($this->element instanceof EntryModel) { 160 | $query = $query 161 | ->join('entries', 'craft_content.elementId = craft_entries.id') 162 | ->where('year(craft_entries.postDate) = year(CURDATE())'); 163 | } else { 164 | $query = $query->where('year(dateCreated) = year(CURDATE())'); 165 | } 166 | } 167 | $maxValue = $query->queryScalar(); 168 | 169 | return $minValue > $maxValue ? $minValue : ($maxValue + 1); 170 | } 171 | 172 | /** 173 | * Craft sets postDate for Live Preview. We do the same. 174 | */ 175 | private function setPostDate() 176 | { 177 | if (($this->element instanceof EntryModel) && is_null($this->element->postDate)) { 178 | $this->element->postDate = new DateTime(); 179 | } 180 | } 181 | } 182 | --------------------------------------------------------------------------------